Ruby n, 1 = gets.split.map &:to_i [duplicate] - ruby

This question already has an answer here:
Ruby x,y = gets.split.map &:to_i
(1 answer)
Closed 7 years ago.
I am looking at some ruby code.
But am unsure what is happening on this line:
n,1 = gets.split.map &:to_i
I understand some input array of strings is being mapped to an array of integers but why is that being assigned to n,1?

That line of code doesn't do anything, it's simply a SyntaxError. You can't assign to 1, that's not a legal name for a local variable.

Related

How do I iterate through every 2nd element of an array in Ruby? [duplicate]

This question already has answers here:
Looping through an array with step
(7 answers)
Closed 2 years ago.
I would like to do the equivalent of this Java code in Ruby. What is the best way in Ruby to do the below ? I dont want to use the Ruby for loop , want to follow idiomatic Ruby.
for (int i = 0; i < nums.length; i += 2) {
// Do something.
}
Use the "step" method to iterate every 2nd element in the array.
(0..array.length).step(2).each do |i|
puts i
end

Why a[3] returns nil, but a[3,0] returns [] in Ruby? [duplicate]

This question already has answers here:
Array slicing in Ruby: explanation for illogical behaviour (taken from Rubykoans.com)
(10 answers)
Slicing of arrays in ruby returns different result - nil vs. empty array
(1 answer)
Closed 8 years ago.
Given an array like this:
a = [1,2,3]
When I do a[3] it returns nil. And that makes sense because there is no more than 3 elements. However, when I do a slicing with a[3,0] or a[3,1], I get []
Why is that?

Why are there slight different behaviors with array accesses? [duplicate]

This question already has answers here:
Array slicing in Ruby: explanation for illogical behaviour (taken from Rubykoans.com)
(10 answers)
Trying to understand Ruby arrays [duplicate]
(1 answer)
Closed 9 years ago.
My array is:
array = [:peanut, :butter, :and, :jelly]
array[4,0] gives []
But:
array[5,0] gives nil
I was suspecting array[4,0] should also give nil as the array has only a third index which is :jelly.
Can any Ruby punters out there clarify this behaviour?
It's all in the documentation:
Additionally, an empty array is returned when the starting index for an element range is at the end of the array.
Returns nil if the index (or starting index) are out of range.

Meaning of ||= symbol [duplicate]

This question already has answers here:
'||=' operator in Ruby
(7 answers)
Closed 9 years ago.
I have a simple question about the meaning of a symbol (I think). What's the mean of ||= in ruby? I have a code snippet that say:
... ||= [nil]
Is as "<<" ? ordinary method?
x ||= y
means (almost) the same thing as
x = x || y
(it only evaluates x once, though.)
It is used mostly for checking if a variable is falsy (nil or false), and if so, setting it to a default value.

Ruby Array Indicies [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Array slicing in Ruby: looking for explanation for illogical behaviour (taken from Rubykoans.com)
Running this code on my ruby interpreter it ends up with 7..4 outputting nil, and 6..4 outputting nothing
arr = [1, 2, 3, 4, 5, 6]
p arr[7..4]
p arr[6..4]
According to the ruby doc if the initial index is out of range it returns a nil, and in this case both (6 & 7) are out of range of the array, but only the first returns a nil.
Edit for clairification: The output is as follows:
nil
[]
Why would the first return nil and the second []?
Well, probably the interpreter doesn't understand the range "decreasing", trying
puts arr[3..1]
also returned nothing, maybe because [3..1] is not a range.
[]s

Resources