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
Ive researched a bit on it but its a bit confusing as to what i’ve found (having to pass variables in another variable { }).
It’s as simple as this:
However, if you want to pass only age, the call would look pretty ugly, because you have to supply blank string for name anyway:
To avoid this, there’s an idiomatic way in Ruby world: options parameter.
You can put some required parameters first, and shove all the optional ones into
options.Edit
It seems that Ruby 2.0 will support real named arguments.