Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Koans about_array_assignment - Nonparallel vs. Parallel assignment Discrimination

Tags:

arrays

ruby

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
like image 940
ZenBalance Avatar asked Dec 23 '12 21:12

ZenBalance


1 Answers

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).

like image 55
Aasmund Eldhuset Avatar answered Sep 28 '22 21:09

Aasmund Eldhuset