ruby function takes dictionary pointer as argument, why? -
def to_json(*opts) { 'json_class' => self.class.name, 'data' => { 'term' => @term, 'command' => @command, 'client' => client } }.to_json(*opts) end why ruby function takes dictionary pointer *opts argument, instead of opts? what's benefit here?
the asterisk * in *opts not pointer (as in c/c++). there's no such concept of pointer in ruby.
when defining method, it's used splatting. example:
def foo(first, *rest) "first=#{first}. rest=#{rest.inspect}" end puts foo("1st", "2nd", "3rd") # => first=1st. rest=["2nd", "3rd"] when calling method, it's used expand argument. example:
arr = ["2nd", "3rd"] bar("1st", *arr) is equivalent to:
bar("1st", "2nd", "3rd")
Comments
Post a Comment