Ruby lexical scope inside iterator block [duplicate] - ruby

This question already has answers here:
Why can I refer to a variable outside of an if/unless/case statement that never ran?
(3 answers)
Closed 5 years ago.
In Ruby (v2.5.0)...
[1,2,3].map do |i|
if i.eql?(3)
a = 123
end
defined?(a)
end
=> ["local-variable", "local-variable", "local-variable"]
Can someone please explain to me how a can be a local-variable (equal to nil) in the first and second iteration, if it's not set until the third iteration?
Thanks in advance!

I will answer quoting a book by A.Black: Well Grounded Rubyist, Chapter 6, p. 158. (second edition 2014):
When the Ruby parser sees the sequence identifier, equal-sign, and value, as in this expression,
a = 123
it allocates space for a local variable a. The creation of the variable - not the assignment of a value to it, but the internal creation of a variable - always takes place as a result of this kind of expression, event if the code isn't executed.

Related

What does this operator #{var} do in ruby? [duplicate]

This question already has answers here:
Ruby question about # Signs
(2 answers)
Closed 7 years ago.
This is from a coderbyte solution to a problem that asks you to look at two integers and determine if there is any digit that occurs three times consecutively in the first number and twice consecutively in the second number. A user posted this solution (partial below), and I understand intuitively what their code is doing, but I'm not sure exactly how these #{i}'s work or what that operator is even called. Looking for more info.
It makes sense to me that you couldn't just say:
string.include?(iii) because that's just silly.
But what is the #{} doing exactly?
arr = num1.to_s.split("").uniq
arr.each do |i|
if num1.to_s.include?("#{i}#{i}#{i}") && num2.to_s.include?("#{i}#{i}")
return 1
end
end
The #{} allows you to execute ruby code within a string. For example,
puts "two plus two is #{2+2}. will give out the output "two plus two is 4." Be careful though, #{} will only work between double quotes and will not work between single quotes. In your example the #{i} will evaluate to the var i from arr.each do |i|
It's the string interpolation syntax: https://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#Interpolation

What does question mark in ".includes?" mean in ruby? [duplicate]

This question already has answers here:
What does the question mark at the end of a method name mean in Ruby?
(9 answers)
Closed 8 years ago.
I ran across this piece of code and was wondering what the ? means in this case? It is part of an if statement condition
if user_input.include? "s"
what does the "?" mean?
sorry, i'm new to ruby
The ? is part of the method name.
In Ruby, method names are allowed to end in a ? or an !. Typically, ? indicates a predicate (a method that returns a Boolean), and ! indicates a destructive operation (something that modifies the receiver object).

How does ruby keep track of variables [duplicate]

This question already has answers here:
Confusion with the assignment operation inside a falsy `if` block [duplicate]
(3 answers)
Closed 9 years ago.
I am confused with the way Ruby keeps track of variables. For example:
case 1:
if true
a
end
will give you an error saying undefined local variable or method a.
case 2:
if false
a
end
a
will give you same the error for the second a, not for the first a.
case 3:
if false
a=2
end
a #=> nil
defined? a #=> 'local-variable'
If you compare case 2 and case 3, in case 2 it ignored the error first a. I think its because of ruby's execution path has not reached the variable a due to false in condition. Same thing when I do with assignment in case 3. It gives me variable a defined but with nil value. Can someone explain the way it works?
In parse time if Ruby found any assignment such a=2,then local variable is created at that moment.It does not matter if you put in inside of any false conditional expression or not. Otherwise a legitimate error will be thrown as undefined local variable or method a,if you try to use the variable such as a here,before it's creation with the assignment(=) operator.
Look Confusion with the assignment operation inside the fallacy if block

Assigning an undefined variable in a one line condition [duplicate]

This question already has answers here:
Ruby if vs end of the line if behave differently?
(5 answers)
Closed 8 years ago.
In Ruby, why can you write :
# b is not defined yet.
#
if b = true
a = b
end
# => a = true
But not a one-liner :
a = b if b = true
# => NameError: undefined local variable or method `b' for main:Object
Because the Ruby interpreter "creates" a local variable when it sees an assignment.
In the second case, it hasn't yet seen the assignment, so the variable doesn't exist when the expression is parsed.
To be more precise, a method is first parsed into an internal representation, and then, perhaps, the code will eventually be called and actually executed.
Variables are "created" in that parsing pass. It's really more a matter of declaration, it just means that the interpreter becomes aware of them. They won't be created in the sense of being given space or a value until the surrounding method is called by someone.

What does a single splat/asterisk in a Ruby argument list mean? [duplicate]

This question already has an answer here:
naked asterisk as parameter in method definition: def f(*)
(1 answer)
Closed 10 years ago.
I was poking through the Rails 3 ActiveRecord source code today and found a method where the entire parameter list was a single asterisk.
def save(*)
I couldn't find a good description of what this does (though I have some ideas based on what I know about splat arguments).
What does it do, and why would you use it?
It means it can have any number of arguments (including zero) and it discards all those arguments.

Resources