What is the difference between '||=' and '=' in Ruby [duplicate] - ruby-on-rails

This question already has answers here:
What does ||= (or-equals) mean in Ruby?
(23 answers)
Closed 8 years ago.
I've been recently allocated to a new Rails project in which I could see assignments to variables are made using ||= instead of =. Can any one help me understand if this is a correct way or a good practice in Rails and the advantages/disadvantages of using it?
e.g.
a ||= b + c
(b and c are integers)
Thanks for any help :)

With:
a = b + c
a gets set to the sum of b and c no matter what.
With:
a ||= b + c
it only gets set to the sum if it's currently set to nil or false.
It's a subtle difference but one that Ruby bods should learn. People coming from C-like languages often see a ||= x as:
a = a || x
but that's not actually the case. Instead, it's:
a || a = x
(no assignment is actually done if a is already set to a non-nil/false value).
Ruby Inside goes into some more depth on the matter here.

Related

Some lines ending with ";1" in Rails legacy code

Hey Stack Overflow fellows,
I need your help on this one. For a few months now I'm dealing with Ruby on Rails application that is mostly Legacy. Today, I noticed the weirdest thing about the codebase. Some files, not many but the significant ones, contain a few lines of code that would end up with ; 1. Like for example: Users.find(id); 1. Occurrence of those suffixes does not create any form of the pattern. Sometimes is ; 1 appears after puts or after expression that will always return value e.g. nil || 'default_value'; 1.
Does it make any sense to use the suffix? Is there any reason behind this? Maybe there used to be a tool that worked with Ruby code and ; 1 was form of annotation. I would gladly remove the suffix but I want to make sure that it's 100% safe.
Here is a code sample from the project added in the same commit:
times = events.map{|x| [x.time, x.time_from_impression_id]};1
times = times.map{|x| (x.first - x.last) / 1.day}.sort;1
time_to_event_success = times[(times.length.to_f * 0.95).to_i]
events = events.select{|e| e.time_from_impression_id < time_to_event_success.days.ago};1
Semi-colons in ruby terminate statements in the same manner as a line break. The ; 1 really isn't doing anything useful.
Logically the code you posted is equivalent to:
times = events.map{|x| [x.time, x.time_from_impression_id]}
1
times = times.map{|x| (x.first - x.last) / 1.day}.sort
1
time_to_event_success = times[(times.length.to_f * 0.95).to_i]
events = events.select{|e| e.time_from_impression_id <
time_to_event_success.days.ago}
1
The only thing I can think of is that if someone was testing the code out in IRB adding the ; 1 to the end of a line would prevent the return value of the previous statement from echoing. That or they didn't quite understand how implicit return and truthy and falsy values work in ruby.

Concatenate table sequences in lua [duplicate]

This question already has answers here:
Concatenation of tables in Lua
(13 answers)
Closed 7 years ago.
Is there an easy way to concatenate two tables which are sequences? For example
a = {1, 2, 3}
b = {5, 6, 7}
c = cat(a,b)
where c would be the table {1,2,3,5,6,7}?
function cat(t, ...)
local new = {unpack(t)}
for i,v in ipairs({...}) do
for ii,vv in ipairs(v) do
new[#new+1] = vv
end
end
return new
end
It uses iteration to add the elements of each array to a new one.
It's also worth noting that {unpack(t)} will only work if you have less than a specific number of elements, due to how tuples work in Lua. This varies across versions and depending on what you're doing, but if it's small you probably have nothing to worry about.

What is the "||=" construct in Ruby? [duplicate]

This question already has answers here:
What does ||= (or-equals) mean in Ruby?
(23 answers)
Closed 8 years ago.
In my readings about structuring methods with options hashes for ruby, I've run into a coding "motif" a few times that I can't explain. Since I don't know what it's called, I'm having a lot of difficulty looking it up to learn more about it.
Here's an example:
1 def example_method(input1, options={})
2
3 default_options = {
4 :otherwise => "blue",
5 :be_nil => nil
6 }
7
8 options[:be_nil] ||= options[:otherwise]
9
10 # other code goes down here
11 end
So above, on line 8, you can see what I'm talking about. From what I can put together, the line of code acts similarly to a tertiary operator. Under one condition, it sets a variable to one value... under a different condition, it sets the variable to a different value. In this case, however, the code updates a hash that's stored in the "options" variable. Is that a correct assumption? Furthermore, what is this style/operator/functionality called?
THis is a conditional assignment. The variable on the left will be assigned a value if it is nil or false. This is the short for of saying:
unless options[:be_nil]
options[:be_nil] = options[:otherwise]
end

Table elements executing a function [duplicate]

This question already has answers here:
Search for an item in a Lua list
(12 answers)
Lua find a key from a value
(3 answers)
Closed 9 years ago.
I have this table:
maps = {4707191, 4747722, 1702169, 3994471, 4708958, 4008546, 4323335, 4516043, 4612295, 3469987, 4337892, 238378, 3088188, 329627, 3526384, 433483}
How can I make a script so if 1702169 (for example) is picked from the table, it prints ''That's the number''?
The easiest way to do what (i think) you want is with pairs() function. This is a stateless iterator which you can read more about here: http://www.lua.org/pil/7.3.html
If you simply want to scan through the entire table and see if it contains a value, then you can use this simple code:
local maps = {4707191, 4747722, 1702169, 3994471, 4708958, 4008546, 4323335, 4516043, 4612295, 3469987, 4337892, 238378, 3088188, 329627, 3526384, 433483}
local picked = 1702169
for i, v in pairs(maps) do
if v == picked then
print("That's the number")
break
end
end
The above code will iterate through the whole table where i is the key and v is the value of the table[key]=value pairs.
I am slightly unclear about your end goal, but you could create this into a function and/or modify it to your actual needs. Feel free to update your original post with more information and I can provide you with a more specific answer.

Are there shorter ways to perform checks on the same variable? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Elegant chained 'or's for tests on same variable in Ruby
I am using Ruby on Rails 3.2.2 and I would like to know if there are shorter ways to perform same checks as in the following:
if (variable == 'A') or (variable == 'B')
...
end
if (variable == 'A') or (variable == 'B') or ... or (variable == 'Z')
...
end
Note that I have to repeat statements related to the variable == "part" more than once and one time for each check. I would not repeat that "part" and make things shorter. Is it possible in Ruby/Ruby on Rails? If so, how can I make that?
Here's one way:
if %w[A B C].include? variable
or if you have an actual range of values:
if ('A'..'Z').include? variable

Resources