Why Doesn't Ruby Support I++ or I-- (Increment/Decrement Operators)

Increment variable in ruby

From the documentation,

Ruby has no pre/post increment/decrement operator. For instance, x++ or x-- will fail to parse

So, you can do

i += 1

which is equivalent of i = i + 1

Why is x-- a valid ruby statement but it doesn't do anything?

In Ruby, operators are methods. --x, x++, x==, etc all can do wildly different things. -- and ++ are not themselves valid operators. They are combinations of operators.

In the case of your provided code, --x is the same as -(-x).

If x = 5, then -x == -5 and --x == 5.

---x would be -(-(-x)), and so on.

Similarly, x-- alone on a line is technically valid, depending on what the next line of code contains.

For example, the following method is valid:

  def foo
x = 1
y = 10
x--
y
end

The last two lines of that method get interpreted as x - (-y) which calculates to 1 - (-10).

The result doesn't get assigned to any value, so the x-- line would appear to do nothing and the function would just return the result: 11.

You could even have nil on the last line of the function instead of y, and you wouldn't get a syntax error, but you would get a runtime error when the function is called. The error you would receive from x--nil is:

NoMethodError: undefined method `-@' for nil:NilClass

That means that -nil is invalid since nil does not define the method -@. The @ indicates that - works as a unary operator. Another way to express --x by invoking the unary operators manually is x.-@.-@

x-- just on its own is not valid. It requires a Numeric object to follow it (or any object which implemented -@). That object can be on the next line. x== would work the same way.

Does Ruby/Rails have a ++ equivalent?

Try this:

x += 1

Why does hash[:symbol]++ not increase the value of :symbol by one?

In Ruby there is no ++ operation.

If you look carefully after you do likedict[:linux]++ it still expects more for your statement, and then you entered likedict[:linux], so 3 + 3 = 6.

How to increment an integer in Ruby

Ruby doesn't have an ++ operator. You can do puts 1.next though. Note that for your second example this would not change the value of x, in that case you'd have to use x += 1.

Ruby Parenthesis syntax exception with i++ ++i

There's no ++ operator in Ruby. Ruby is taking your foo++ + ++foo and taking the first of those plus signs as a binary addition operator, and the rest as unary positive operators on the second foo.

So you are asking Ruby to add 5 and (plus plus plus plus) 5, which is 5, hence the result of 10.

When you add the parentheses, Ruby is looking for a second operand (for the binary addition) before the first closing parenthesis, and complaining because it doesn't find one.

Where did you get the idea that Ruby supported a C-style ++ operator to begin with? Throw that book away.

Does anyone know why IRB gives the syntax error in block?

Because counter++ is not a valid ruby expression. You should replace it with counter += 1.



Related Topics



Leave a reply



Submit