Ruby chaining methods with if-statement [duplicate] - ruby

This question already has an answer here:
One line if statement in Ruby
(1 answer)
Closed 8 years ago.
I have the following:
def method(integer)
a = 3+integer
a += 10 if "one"<"another"
end
Can I write it in one line somehow with chaining methods?
Something like a = 3+f += 10 if "one"<"another"?

You could do it in one line using the ternary operator:
def method(integer)
a = integer + ("one"<"another" ? 13 : 3)
end
Make sure you don't hurt the readability of the code when you do that, though.

Since and or && both use short-circuit evaluation, you could use:
(a = 3+integer) and ("one"<"another") and (a += 10)
It says in 'Using “and” and “or” in Ruby':
and is useful for chaining related operations together until one of them returns nil or false
Another way of thinking about and is as a reversed if statement modifier

a= 3+ integer + ("one"<"another" ? 10 : 0)
3+ integer will add 3 to integer value and ("one"<"another" ? 10 : 0) will return 10 if condition is true otherwise will return 0.

Related

Ruby question mark operator, what does this mean? [duplicate]

This question already has answers here:
How do I use the conditional operator (? :) in Ruby?
(7 answers)
Closed 1 year ago.
this might be a very vague question. But i am wondering if someone could translate this into pseudocode:
a = (1 + (bool ? rand(13) : 0)
Does it mean that a will become any value between 0-13 + 1? what is the purpose of the boolean value and the question mark?
(true ? rand(13) : 0) mean (if true then rand(13) else 0 end)
if you have directly "true" in condition, the "else" is never called (is useless), you can write : a = 1 + rand(13) directly ;)
rand(13) give random int between 0 and 12 ;)
if you want "13" put rand(14)
personally I always use range like this (all range is include, it's easier to understand) : rand(0..13)

What the does the line if a = b mean in ruby code? [duplicate]

This question already has answers here:
Why does single `=` work in `if` statement?
(5 answers)
Closed 6 years ago.
I am trying to understand a particular line in the following piece of code:
def roman_to_integer(roman_string)
prev = nil
roman_string.to_s.upcase.split(//).reverse.inject(0) do
|running_sum, digit|
if digit_value = DIGITS[digit]
if prev && prev > digit_value
running_sum -= digit_value
else
running_sum += digit_value
end
prev = digit_value
end
running_sum
end
end
Can someone please help me understand when the line if digit_value = DIGITS[digit] means? are we assigning the value corresponding to the key 'DIGIT' from the hash to the digit_value here?
are we assigning the value
Yes, we are. And we also check the truthiness of the operation. Assignment operator returns the value that was assigned. Now, if it was a digit, it will be a truthy result and control will enter the if.
If DIGITS[digit] returns nil or false, it will be assigned to digit_value and also it will also become result of the assignment operation. Those values are falsey, so we would enter the else, if we had one there. But we don't, so we just skip the if.
are we assigning the value corresponding to the key 'DIGIT' from the hash to the digit_value here?
Yes that is exactly what is happening. The temporary variable is slightly easier to read than the extraction from the hash. In similar circumstances, obtaining the value might be more expensive (think of a database read for example instead of a Hash lookup), so it is not a bad practice to get into.
The assignment operator also returns the value assigned for the if statement to work.
Alternative equivalent syntax is a bit more verbose:
digit_value = DIGITS[digit]
if digit_value
# .... etc
so this is also a common style choice when assigning a value to a variable and wanting to check its truthiness immediately.
if digit_value = DIGITS[digit] will return true if DIGITS[digit] has value other than nil or false. This is because in Ruby nil and false are the only values that are considered falsy.
Ruby will first assign the value to variable and than evaluate if the value is falsy.

What does || exactly Mean? [duplicate]

This question already has answers here:
Understanding the "||" OR operator in If conditionals in Ruby
(9 answers)
Closed 7 years ago.
Recently I saw code like
i < 0 ||
I wonder what is "||" exactly mean? How to say that in english?
Logical "or"
A || B is true when either A is true or B is true, or when both A and B are true.
http://www.tutorialspoint.com/ruby/ruby_operators.htm
The || operator is similar to the keyword or but is different from the keyword or in extremely important ways. Below are two great write-ups on the topic, comparing the two and showing you how to use either one:
http://devblog.avdi.org/2010/08/02/using-and-and-or-in-ruby/
New version, with video: http://devblog.avdi.org/2014/08/26/how-to-use-rubys-english-andor-operators-without-going-nuts/
The most important thing to note in what Avdi says, is that || cannot be used for flow control, whereas or can be.
For example...
a = :value
c = b || a
#de Since `b` is undefined/null, `c` will be set to `:value`
c = b || puts("Failure!") #de This will raise an exception!
c = b or puts("Failure!") #de Will set `c` to `NilClass` and output "Failure!"

Ruby ++ not work? [duplicate]

This question already has answers here:
No increment operator (++) in Ruby? [duplicate]
(3 answers)
Closed 9 years ago.
Using Ruby, I can't seem to get the following to work:
a = 1
a++
The above line works in irb but doesn't work when I compile from file.
Is there anything I missed out? I'm using Ruby 2.0.
Ruby has no pre/post increment/decrement operator. For instance, x++ or x-- will fail to parse. More importantly, ++x or --x will do nothing! In fact, they behave as multiple unary prefix operators: -x == ---x == -----x == ...... To increment a number, simply write x += 1
Ruby doesn't have ++ or -- operators but += and -= accomplish the same thing. Try using the += notation like this:
a = 1
a+= 1
#=> 2
Here is a good reference list of valid ruby operators.

What is the difference between 'and, or' and '&&, ||'? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
PHP - and / or keywords
Ruby: difference between || and 'or'
What is the difference between this:
a=20
b=30
if (a > 10 && b > 10)
puts a
end
and this:
if (a > 10 and b > 10)
puts a
end
and also explain difference between "||" and "or"
The answer relates to the PHP tag that has been assigned to this question, but has been deleted afterwards
They are the same with one small difference. The and and or operator have a lower precedence compared to && and ||.
Take this example from the manual:
// The result of the expression (true && false) is assigned to $g
// Acts like: ($g = (true && false))
$g = true && false;
// The constant true is assigned to $h and then false is ignored
// Acts like: (($h = true) and false)
$h = true and false;
You can read about operators in PHP's documentation.
Only the difference in precedence
As you can see from the precedence table for Ruby, the difference is also precedence, as it is in PHP: and and or have lower precedence than && and ||.
But there is a slight difference from PHP: in Ruby, and and or have the same precedence.
I got the answer. Please go and read it. this is what i'm expect it.
http://avdi.org/devblog/2010/08/02/using-and-and-or-in-ruby/
Thanks all of your efforts

Resources