Forum: Ruby use not equal(!=) operator as a Symbol.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7caba80fca2ac4ddbb7f55957e5c0dec?d=identicon&s=25 Jun Young Kim (Guest)
on 2008-12-29 07:59
(Received via mailing list)
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
  from :0
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
  from :0
irb(main):009:0>

Is any idea?
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2008-12-29 08:55
(Received via mailing list)
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)
B09f4659460545e38ece34ddd0d96b46?d=identicon&s=25 Yaser Sulaiman (Guest)
on 2008-12-29 09:00
(Received via mailing list)
Hi.

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

Regards,
Yaser Sulaiman
7caba80fca2ac4ddbb7f55957e5c0dec?d=identicon&s=25 Jun Young Kim (Guest)
on 2008-12-29 09:17
(Received via mailing list)
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).

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 작성:
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2008-12-29 13:17
(Received via mailing list)
Hi --

On Mon, 29 Dec 2008, Ryan Davis wrote:

> 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
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2008-12-30 01:37
(Received via mailing list)
On Dec 29, 2008, at 04:16 , David A. Black wrote:

>>>   from (irb):8
> 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
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2008-12-30 01:37
(Received via mailing list)
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.
7caba80fca2ac4ddbb7f55957e5c0dec?d=identicon&s=25 Jun Young Kim (Guest)
on 2008-12-30 01:53
(Received via mailing list)
Hi, David.

This is a really good news for me :)

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

anyway, thank you~ :)

2008. 12. 29, ¿ÀÈÄ 9:16, David A. Black ÀÛ¼º:
7caba80fca2ac4ddbb7f55957e5c0dec?d=identicon&s=25 Jun Young Kim (Guest)
on 2008-12-30 01:56
(Received via mailing list)
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 ÀÛ¼º:
This topic is locked and can not be replied to.