0

So can I make this happen in ruby? 4++

My initial googling showed I can redefine plus but when I try and define ++ it will error on me.

test.rb:2: syntax error, unexpected '+', expecting ';' or '\n'
def ++()
       ^
test.rb:5: syntax error, unexpected keyword_end, expecting end-of-input
user2864740
  • 60,010
  • 15
  • 145
  • 220
Rumel
  • 917
  • 1
  • 9
  • 21
  • 2
    This should probably be marked as a duplicate of "[No increment operator (++) in Ruby?](http://stackoverflow.com/questions/3717519/no-increment-operator-in-ruby)" and "[Why doesn't Ruby support i++ or i— (increment/decrement operators)?](http://stackoverflow.com/questions/3660563/why-doesnt-ruby-support-i-or-i-increment-decrement-operators)", but it's a tiny bit different question, so I'm linking to the common answers to the real problem. – the Tin Man Sep 10 '14 at 21:25
  • 1
    You could always petition [Matz](http://en.wikipedia.org/wiki/Yukihiro_Matsumoto) to support it, but I doubt it will happen. Also, what is `4++` supposed to mean? That's nonsense in most languages as `4 = 4 + 1` is not going to work. – tadman Sep 10 '14 at 22:10

1 Answers1

5

You cannot change it because there is no operator ++ in Ruby to begin with. That's why you get a syntax error.

See Why doesn't Ruby support i++ or i— (increment/decrement operators)?

Community
  • 1
  • 1
awendt
  • 13,195
  • 5
  • 48
  • 66
  • 2
    It should be noted that `++x` is still valid (although not a pre-increment), being a double application of the unary-plus. – user2864740 Sep 10 '14 at 21:06