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)
Related
This question already has answers here:
What does the (unary) * operator do in this Ruby code?
(3 answers)
Closed 5 years ago.
I've ecountered this today and I have no idea what it means. I tried to google it but I had no luck. Can someone explain this to me?
combinations.each do |combination|
messages = EventNotification.where('user_id = ? AND message_template = ?', *combination)
...
end
It's called the splat operator, and it unpacks an array into single method arguments. In this case, because the function presumably expects two more arguments after the format string, it's equivalent to:
messages = EventNotification.where('user_id = ? AND message_template = ?',
combination[0], combination[1])
In other languages, this feature is often called "varargs".
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!"
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.
I am working on a home assignment and I don't understand one thing about it.. Maybe some of you guys can help me with that.
(a < 0) ? 1 : -1
What does this mean?
This is basically saying is the variable a less then 0? if it is 1 if it is not then -1 it is like this
if(a<0)
1
else
-1
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.