Ruby ++ not work? [duplicate] - ruby

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.

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)

Ruby what does star prefix means in a loop variable? [duplicate]

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".

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 chaining methods with if-statement [duplicate]

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.

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