Sorry for this newbie question.
For arguments to ruby methods, what's difference between these two?
method(x:y)
method(x=>y)
Thanks
···
______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org
Quoting Henry R via ruby-talk (ruby-talk@ml.ruby-lang.org):
Sorry for this newbie question.
For arguments to ruby methods, what's difference between these two?
method(x:y)
method(x=>y)
x:y is a shorthand for :x=>y. That is: a hash with one element, that
has as key *symbol* :x, and as value whatever is in variable y. In the
second case, the key of the hash is whatever you have currently in
variable x.
If you have this short script:
--8<----8<----8<----8<--
def m(h)
pp h
pp h.class
pp h.keys[0]
pp h.keys[0].class
end
x=1
y=2
m(x=>y)
print("\n")
m(x:y)
--8<----8<----8<----8<--
and run it, you obtain
{1=>2}
Hash
1
Integer
{:x=>2}
Hash
:x
Symbol
HTH
Carlo
···
Subject: [ruby-talk:444061] A simple question about arguments
Date: Sat 17 Dec 22 06:34:17AM +0000
--
* Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
* di parlare tanto di amore e di rettitudine? (Chuang-Tzu)
______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org
def tt (x=123)
puts x
end
=> :tt
tt
123
=> nil
tt(456)
456
=> nil
The code above is the default argument I know.
But this one,
def tt2 (x:123)
puts x
1> end
=> :tt2
tt2
123
=> nil
tt2(456)
Traceback (most recent call last):
5: from /usr/bin/irb:23:in `<main>'
4: from /usr/bin/irb:23:in `load'
3: from /Library/Ruby/Gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
2: from (irb):10
1: from (irb):6:in `tt2'
ArgumentError (wrong number of arguments (given 1, expected 0))
As you see it fails.
But the following calling could work.
tt2(x:456)
456
=> nil
tt2(:x=>456)
456
=> nil
what's the trick behind it?
Thanks & regards,
Henry
···
______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org
"x" in tt2 is a keyword argument:
https://docs.ruby-lang.org/en/3.0/syntax/methods_rdoc.html#label-Keyword+Arguments
···
On December 18, 2022 1:09:51 AM UTC, Henry R via ruby-talk <ruby-talk@ml.ruby-lang.org> wrote:
def tt2 (x:123)
puts x
end
tt2
123
tt2(456)
ArgumentError (wrong number of arguments (given 1, expected 0))
tt2(x:456)
456
tt2(:x=>456)
456
what's the trick behind it?
______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org