Ruby Methods and Optional parameters
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I'm playing with Ruby on Rails and I'm trying to create a method with optional parameters. Apparently there are many ways to do it. I trying naming the optional parameters as hashes, and without defining them. The output is different. Take a look: # This functions works fine! def my_info ( name , options = {}) age = options [: age ] || 27 weight = options [: weight ] || 160 city = options [: city ] || "New York" puts "My name is #{name}, my age is #{age}, my weight is #{weight} and I live in {city}" end my_info "Bill" -> My name is