Sunday, 15 September 2013

Ruby x,y = gets.split.map &:to_i -


I am confused about this line of Ruby Code. Why is not assignment X, Y and a single variable given?

  x, y = gets.split.map and: to_i  

This returns the code before the array on x , and unlike the y specified in a variable, in that case the array is assigned to that variable Will go

By inserting multiple variables on the left hand of the assignment, there is a way to unpack a variable in different variables. You can try it in irb:

  irb (main): 001: 0> Numbers = [1, 2, 3] = & gt; [1, 2, 3] IRB (Main): 002: 0> First, second = numbers = & gt; [1, 2, 3] IRB (Main): 003: 0> First = & gt; 1 IRB (Main): 004: 0> Second = & gt; 2  

Check that I have written which gives more details and is a good example of where it can be useful.


No comments:

Post a Comment