Ruby-on-rails Rails 可选参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9710303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails optional argument
提问by Akshat
I have a class
我有一堂课
class Person
attr_accessor :name,:age
def initialize(name,age)
@name = name
@age = age
end
end
I'd like to make the age optional so its 0 if its not passed, or the name to be blank if not passed
我想将年龄设为可选,因此如果未通过则为 0,如果未通过则名称为空
Ive researched a bit on it but its a bit confusing as to what i've found (having to pass variables in another variable { }).
我对它进行了一些研究,但对我发现的内容有点困惑(必须在另一个变量 { } 中传递变量)。
回答by Sergio Tulentsev
It's as simple as this:
就这么简单:
class Person
attr_accessor :name, :age
def initialize(name = '', age = 0)
self.name = name
self.age = age
end
end
Person.new('Ivan', 20)
Person.new('Ivan')
However, if you want to pass only age, the call would look pretty ugly, because you have to supply blank string for name anyway:
但是,如果您只想传递年龄,则调用看起来会非常难看,因为无论如何您都必须为 name 提供空白字符串:
Person.new('', 20)
To avoid this, there's an idiomatic way in Ruby world: optionsparameter.
为了避免这种情况,Ruby 世界中有一个惯用的方法:options参数。
class Person
attr_accessor :name, :age
def initialize(options = {})
self.name = options[:name] || ''
self.age = options[:age] || 0
end
end
Person.new(name: 'Ivan', age: 20)
Person.new(age: 20)
Person.new(name: 'Ivan')
You can put some required parameters first, and shove all the optional ones into options.
你可以先把一些必需的参数放进去,然后把所有可选的都塞进options.
Edit
编辑
It seemsthat Ruby 2.0 will support real named arguments.
Ruby 2.0似乎将支持真正的命名参数。
def example(foo: 0, bar: 1, grill: "pork chops")
puts "foo is #{foo}, bar is #{bar}, and grill is #{grill}"
end
# Note that -foo is omitted and -grill precedes -bar
example(grill: "lamb kebab", bar: 3.14)
回答by bodacious
If you want both arguments to be optional but also set default values when nil then you could go with:
如果您希望两个参数都是可选的,但还希望在 nil 时设置默认值,那么您可以使用:
class Person
班级人物
def initialize(name = nil, age = 0)
@name ||= "Default name"
@age = age
end
end
结尾
This gets over the issue of passing nil as the first option but still getting a useable default value.
这克服了将 nil 作为第一个选项传递但仍然获得可用默认值的问题。
@person = Person.new nil, 30
@person.name # => "Default name"
@person.age # => 30
回答by Ryan Bigg
This is more of a Ruby thing. You can define optional argumentsfor a method like this:
这更像是 Ruby 的事情。您可以为这样的方法定义可选参数:
def initialize(name="", age=0)
@name = name
@age = age
end
By doing it this way, you will be able to call Person.newand then have name default to a blank string if it's not passed and age default to 0. If you want age to be something but name to be blank you'll need to pass an empty string anyway:
通过这样做,您将能够调用Person.new然后将 name 默认为空字符串,如果它没有传递,并且 age 默认为 0。空字符串无论如何:
Person.new("", 24)
回答by S.M.Mousavi
Use hash for ruby 1.8/1.9 as follow:
对 ruby 1.8/1.9 使用哈希如下:
def myMethod(options={})
@email = options[:email]
@phone = options[:phone]
end
# sample usage
myMethod({:email => "[email protected]", :phone => "0098-511-12345678"})
Also on ruby 2.0/2.1 you can use keyword arguments as follow:
同样在 ruby 2.0/2.1 上,您可以使用关键字参数如下:
def myMethod(email: '[email protected]', phone: '0098-000-00000000')
@email = email
@phone = phone
end
# sample usage
myMethod(email: "[email protected]", phone: "0098-511-12345678")

