Working through rubykoans.com I came across these two pieces of code in about_array_assignment.rb
How can you tell that the first is nonparallel assignment and the second is parallel assignment with one variable? To me, it looks like the code is pretty much identical except for the naming differences.
4 def test_non_parallel_assignment
5 names = ["John", "Smith"]
6 assert_equal ["John", "Smith"], names
7 end
45 def test_parallel_assignment_with_one_variable
46 first_name, = ["John", "Smith"]
47 assert_equal 'John', first_name
48 end
In the second example, there is a comma after the variable. A parallel assignment normally lists several comma-separated variables, but using only one variable is allowed (but this still requires a comma in order to separate it from regular assignment).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With