Use not equal(!=) operator as a Symbol

Hi, all

I try to use comparision operator as a symbol type.

but, I met the following problems.

Here is my example.

irb(main):001:0> a = 1
=> 1
irb(main):002:0> a.send(:==, 1)
=> true
irb(main):003:0> a.send(:==, 2)
=> false
irb(main):004:0> a.send(:>, 0)
=> true
irb(main):005:0> a.send(:>, 2)
=> false
irb(main):006:0> a.send(:!=, 2)
SyntaxError: compile error
(irb):6: syntax error, unexpected tNEQ, expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END
a.send(:!=, 2)
           ^
  from (irb):6
irb(main):007:0> a.send('==', 2)
=> false
irb(main):008:0> a.send('!=', 2)
NoMethodError: undefined method `!=' for 1:Fixnum
  from (irb):8:in `send'
  from (irb):8
irb(main):009:0>

Is any idea?

···

from :0
  from :0

there is no '!=' method, there is only '=='. you should send '==' and not/! the result:

! a.send('==', 2)

···

On Dec 28, 2008, at 22:58 , Jun Young Kim wrote:

irb(main):008:0> a.send('!=', 2)
NoMethodError: undefined method `!=' for 1:Fixnum
  from (irb):8:in `send'
  from (irb):8
  from :0

Hi.

AFAIK, != gets converted by Ruby when your program is read. For example, foo
!= bar becomes !(foo == bar).

Regards,
Yaser Sulaiman

irb(main):006:0> a.send(:!=, 2)
SyntaxError: compile error
(irb):6: syntax error, unexpected tNEQ, expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END
a.send(:!=, 2)
          ^
  from (irb):6

As you see, ruby can recognize a != as a tNEG(not equals).

so , I convinced a method is existed for this type of a comparison.

I have to use a workaround way, ;-<

2008. 12. 29, 오후 4:54, Ryan Davis 작성:

···

from :0

On Dec 28, 2008, at 22:58 , Jun Young Kim wrote:

irb(main):008:0> a.send('!=', 2)
NoMethodError: undefined method `!=' for 1:Fixnum
  from (irb):8:in `send'
  from (irb):8
  from :0

there is no '!=' method, there is only '=='. you should send '==' and not/! the result:

! a.send('==', 2)

Hi --

···

On Mon, 29 Dec 2008, Ryan Davis wrote:

On Dec 28, 2008, at 22:58 , Jun Young Kim wrote:

irb(main):008:0> a.send('!=', 2)
NoMethodError: undefined method `!=' for 1:Fixnum
  from (irb):8:in `send'
  from (irb):8
  from :0

there is no '!=' method, there is only '=='. you should send '==' and not/! the result:

! a.send('==', 2)

In 1.9 != becomes a method:

$ ruby19 -ve 'p 1.send("!=", 2)'
ruby 1.9.1 (2008-12-14 revision 20738) [i386-darwin9.5.0]
true

David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2\)

Hi --

irb(main):008:0> a.send('!=', 2)
NoMethodError: undefined method `!=' for 1:Fixnum
  from (irb):8:in `send'
  from (irb):8
  from :0

there is no '!=' method, there is only '=='. you should send '==' and not/! the result:

! a.send('==', 2)

In 1.9 != becomes a method:

$ ruby19 -ve 'p 1.send("!=", 2)'
ruby 1.9.1 (2008-12-14 revision 20738) [i386-darwin9.5.0]
true

that's a bug, == and != shouldn't be able to disagree:

>> class X; def == o; :great; end; def != o; :horrible; end; end
=> nil
>> x = X.new
=> #<X:0x419948>
>> x == 0
=> :great
>> x != 0
=> :horrible

···

On Dec 29, 2008, at 04:16 , David A. Black wrote:

On Mon, 29 Dec 2008, Ryan Davis wrote:

On Dec 28, 2008, at 22:58 , Jun Young Kim wrote:

no. you're getting a syntax error because :!= is not a valid symbol, but :"!=" is. Try that and you'll see that there is no such method.

···

On Dec 29, 2008, at 00:17 , Jun Young Kim wrote:

irb(main):006:0> a.send(:!=, 2)
SyntaxError: compile error
(irb):6: syntax error, unexpected tNEQ, expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END
a.send(:!=, 2)
        ^
  from (irb):6
  from :0

As you see, ruby can recognize a != as a tNEG(not equals).

Hi, David.

This is a really good news for me :slight_smile:

but, the only option I can take is just a version 1.8.7 ;-<

anyway, thank you~ :slight_smile:

2008. 12. 29, 오후 9:16, David A. Black 작성:

···

Hi --

On Mon, 29 Dec 2008, Ryan Davis wrote:

On Dec 28, 2008, at 22:58 , Jun Young Kim wrote:

irb(main):008:0> a.send('!=', 2)
NoMethodError: undefined method `!=' for 1:Fixnum
  from (irb):8:in `send'
  from (irb):8
  from :0

there is no '!=' method, there is only '=='. you should send '==' and not/! the result:

! a.send('==', 2)

In 1.9 != becomes a method:

$ ruby19 -ve 'p 1.send("!=", 2)'
ruby 1.9.1 (2008-12-14 revision 20738) [i386-darwin9.5.0]
true

David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2\)

Hi, Ryan

This is still unavailable way on version 1.8.7.

According to previous responses, I think the way to use what I want is just to update ruby's version :).

2008. 12. 30, 오전 9:37, Ryan Davis 작성:

···

On Dec 29, 2008, at 00:17 , Jun Young Kim wrote:

irb(main):006:0> a.send(:!=, 2)
SyntaxError: compile error
(irb):6: syntax error, unexpected tNEQ, expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END
a.send(:!=, 2)
       ^
  from (irb):6
  from :0

As you see, ruby can recognize a != as a tNEG(not equals).

no. you're getting a syntax error because :!= is not a valid symbol, but :"!=" is. Try that and you'll see that there is no such method.