Why doesn't assigning a variable to an array yield expected results in Ruby? -
using irb, tested following:
c:\pickaxe>irb irb(main):001:0> list_of_strings = %w{ list of strings in array } => ["a", "list", "of", "strings", "in", "an", "array"] irb(main):002:0> a, b, c = list_of_strings => ["a", "list", "of", "strings", "in", "an", "array"] irb(main):003:0> => "a" irb(main):004:0> b => "list" irb(main):005:0> c => "of" irb(main):006:0>
in other languages i've developed in setting a, b, c = d
sets values of a
, b
, c
equal d
in entirety. here set successive elements in array.
i don't understand how or why works. shed light on issue?
that's how array assignment works in ruby. it's trying clever, little, assigning each indexed item of array variable on left-hand side of assignment =
Comments
Post a Comment