Why does "A".send('!='.to_sym, "B")
raises a NoMethodError in Ruby 1.8.7 while "A" != "B"
does not - and how would the correct syntax for Object.send
look like?
Asked
Active
Viewed 210 times
2

Andrew Grimm
- 78,473
- 57
- 200
- 338

user1196609
- 33
- 1
- 5
2 Answers
5
!=
isn't a method in ruby 1.8 It's hardwired to be the negation of calling ==

Frederick Cheung
- 83,189
- 8
- 152
- 174
-
Any idea where != is defined in 1.9? I can't find it neither in Object nor in String... – user1196609 Mar 08 '12 at 13:22
-
@user1196609 You probably missed it because it was written in C, not Ruby. It's in [object.c](https://github.com/ruby/ruby/blob/6d6b4569fc153149ce648268e4d1df00f7dfa1bc/object.c#L151-156) and defined via `rb_define_method(rb_cBasicObject, "!=", rb_obj_not_equal, 1);` – brymck Mar 08 '12 at 14:04
-
@Bryan: This depends entirely on the Ruby implementation. In [Rubinius](http://Rubini.us/), it's written in Ruby and lives in [kernel/bootstrap/basicobject.rb](https://GitHub.Com/Rubinius/Rubinius/blob/master/kernel/bootstrap/basicobject.rb#L20-22) and looks like this: `def !=(other) self == other ? false : true end`. – Jörg W Mittag Mar 09 '12 at 01:02
3
Since the second half of the question hasn't been answered yet:
'A'.send(:==, 'B').send(:!) # Ruby 1.9
!'A'.send(:==, 'B') # Ruby 1.8

Jörg W Mittag
- 363,080
- 75
- 446
- 653