views:

171

answers:

4

I guess I just got used to saying things like:

x++

in PHP and Java land. But when I tried this in my Rails code it had a fit:

compile error
/users/gshankar/projects/naplan/app/views/answers/new.html.haml:19: syntax error, unexpected ';'
/users/gshankar/projects/naplan/app/views/answers/new.html.haml:23: syntax error, unexpected kENSURE, expecting kEND
...}\n", 0, false);_erbout;ensure;@haml_buffer = @haml_buffer.u...
                              ^
/users/gshankar/projects/naplan/app/views/answers/new.html.haml:26: syntax error, unexpected $end, expecting kEND

I googled around a bit for Ruby/Rails operators for a reference to ++ but couldn't find anything. For a language which focuses on writing less I find it a bit strange that there wouldn't be a ++ equivalent. Am I missing something?

+1  A: 

You have to use x+=1 instead.

http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Operators

thebretness
+4  A: 

Try this:

x += 1
Trevor
+3  A: 

x+=1 is the best you can do in Ruby.

For a detailed explanation, see http://redmine.ruby-lang.org/issues/show/1432

igul222
Thanks for the explanation. It's good to know WHY I can't do it :)
Ganesh Shankar
+4  A: 

Really you should be asking yourself why you need increment/decrement. I've been using Ruby for close to two years now, and hadn't noticed that increment and decrement operators were missing. Might be because the Ruby way to do operations where increment and decrement operators are commonly used is with an iterator.

Such as each_with_index which executes a given block for each element/index pair in the array.

For example, this code will iterate through an array outputting the string representation fo each element and the parity of its index.

array = ["first", "second", "third","fourth","last"]
array.each_with_index do |object,index|
   puts index % 2 ? "even" : "odd"
   puts object.to_s
end

Edit: Removed Fixnum extension providing postfix increment because it doesn't work. Upon closer inspection there's nothing stopping you from adding it, but it would involve writing your feature in C, and a recompile.

EmFi
But what about `++x`? (Not that it should *ever* be used that way. I have to pause everytime I see something like `while(--x)` and think about what it's actually doing (will x ever be 0 in the body?))
Wallacoloo
This will not work at all. First, you can't just define arbitrary operators, and "++" won't even parse in Ruby — it would have to be called as `x.send('++')`. Second, you can't write `self =` anything. Remember, doing `x = something` just makes the name `x` refer to a new object — it doesn't change the value of the object that was there before.
Chuck
Thanks Chuck, that will teach me to post code without testing it. I'm sure there's a way to write a ++, but, upon closer inspection it would probably involve recompiling Ruby from C.
EmFi
@EmFi, that's a great point about questing one's use of incrementing variables. Anytime I've wanted to do something like `x+=1`, I've found a more elegant approach.
Beerlington
Well the case here is where I just need a simple counter to give me row numbers in a list of items I'm displaying... if there's a way to do it without having a variable and incrementing it I'm keen to hear about it. I am using a .each loop so is there a way to know which iteration I'm in?
Ganesh Shankar
@gshankar Try Enumerable#each_with_index, I've updated my solution to contain an example using it as well as a link to its documentation.
EmFi
By changing the Ruby parser and adding a new operator to the language, you'd end up with something that isn't really valid Ruby anymore. (Not that that's necessarily a bad thing — it's just not Ruby.)
Chuck