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?

有帮助吗?

解决方案

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);
    }

其他提示

No, it is not. It is a hash. Your last question is undefined due to presupposition failure.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top