Wednesday, May 13, 2015

RubyKoan: test_parallel_assignments_with_splat_operator

Today's RubyKoan is all about that Splat.


def test_parallel_assignments_with_splat_operator
    first_name, *last_name = ["John", "Smith", "III"]
    assert_equal "John", first_name
    assert_equal ["Smith", "III"], last_name
end

You can use a splat in a method definition to gather up any remaining arguments. So if an array has three arguments and you have called the first argument, a splat will allow you to call the rest of the arguments. To check this I added a third last name just before "Smith" and asserted_equal to include that third last name and it worked.

Hope you have been enlightened.

If you find this insightful, please comment below.

If you don't find this helpful, go ahead and comment anyway. Let me know if there is any way I can write these to make them more clear.

Brought to you by KENYACODE. Can you code?

No comments:

Post a Comment