Вопрос

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