Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ruby 1.9.3 have keyword arguments?

All the documentation I can find says that keyword arguments weren't introduced until Ruby 2.0.

But Array#shuffle looks like it takes a keyword argument called 'random': http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-shuffle

Is this a keyword argument? If not, what is it? What other uses of keyword arguments are in ruby 1.9.3?

like image 205
archbishop Avatar asked May 17 '13 19:05

archbishop


1 Answers

Ruby 1.9.3 doesn't have named parameters, but added extra sugar for hashes. So {:key => 'val'} is equivalent to {key: 'val'}. What you see there is a hash being passed as parameter.

If you look at the source of the method you pointed, you will see this:

rb_ary_shuffle(int argc, VALUE *argv, VALUE ary)
{
    ary = rb_ary_dup(ary);
    rb_ary_shuffle_bang(argc, argv, ary);
    return ary;
}

and in the shuffle! method, you can confirm it is a hash by looking at this part:

 if (OPTHASH_GIVEN_P(opts)) {
        randgen = rb_hash_lookup2(opts, sym_random, randgen);
    }
like image 111
fotanus Avatar answered Sep 20 '22 18:09

fotanus