Probably I’m trying something impossible, but when giving default
values for arguments to a method call, do I have to give default-values
for all arguments?
irb(main):001:0> class A
irb(main):002:1> def a(b = 0, c, d = ‘’, e)
irb(main):003:2> puts b, c, d, e
irb(main):004:2> end
irb(main):005:1> end
SyntaxError: compile error
(irb):2: syntax error
def a(b = 0, c, d = ‘’, e)
^
(irb):2: syntax error
(irb):5: syntax error
from (irb):5
And also when giving default-values to all arguments, omitting
arguments when calling the method doesn’t seem to be a succes:
irb(main):001:0> class B
irb(main):002:1> def b(c = 0, d = 1, e = 2)
irb(main):003:2> puts c, d, e
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> B.new.b(1,2,3)
1
2
3
=> nil
irb(main):007:0> B.new.b(1,3)
SyntaxError: compile error
(irb):7: syntax error
B.new.b(1,3)
^
from (irb):7
Probably I’m trying something impossible, but when giving default
values for arguments to a method call, do I have to give default-
values
for all arguments?
No, but all the arguments that don’t have defaults must come before
(to the left of) those that do.
The Simulated Keyword Arguments you talk about are a little abstract to
me, but I think I get the idea. I suppose in the example above I would
have to add a couple of lines to define the default values…
This way, the general idea is the same as the following?
An additional question: do you know a link where I can find more on
those Simulated Keyword Arguments? I did some googling, but it turned
up with nothing on:
ruby “Simulated Keyword Arguments”
Those colons you use before the variable-names are a little like magic
to me. (though I checked, without those colons, you’re example above
wouldn’t have worked…)
The Simulated Keyword Arguments you talk about are a little
abstract to
me, but I think I get the idea.
My fault, I did not describe them very well. Here are the critical
bits of information:
If a method call ends with some arguments that look like a Hash
definition (using the fat arrows =>), Ruby collects those and passes
a single Hash to the method in their place. You can then work with
the Hash normally.
defaultB
someC
=> nil
Another way to set defaults:
def test(args)
args = { “a” => “default a”,
?> “b” => “default b”,
?> “c” => “default c” }.merge(args)
puts args.values_at(“a”, “b”, “c”)
end
=> nil
test “a” => “some a”, “c” => “some c”
some a
default b
some c
=> nil
An additional question: do you know a link where I can find more on
those Simulated Keyword Arguments?
Those colons you use before the variable-names are a little like magic
to me. (though I checked, without those colons, you’re example above
wouldn’t have worked…)
Those are just Symbol objects. Don’t lose too much sleep over them
yet, as you can see from the new example Strings work fine too.
more readable. Preferrably extracting the default values into constants
before the method definition and (deep?)cloning the hash.
Also, your code clobbers the hash put into args by putting in values.
This might not cause bugs most of the case, but might in case someone
passes in an actual hash, so I’d just stay on the safe side.
An additional question: do you know a link where I can find more on
those Simulated Keyword Arguments? I did some googling, but it turned
up with nothing on:
ruby “Simulated Keyword Arguments”
Those colons you use before the variable-names are a little like magic
to me. (though I checked, without those colons, you’re example above
wouldn’t have worked…)
The colons make them into symbol objects.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.