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

J

Jun Young Kim

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?
 
R

Ryan Davis

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)
 
Y

Yaser Sulaiman

[Note: parts of this message were removed to make it a legal post.]

Hi.

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

Regards,
Yaser Sulaiman
 
J

Jun Young Kim

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

As you see, ruby can recognize a !=3D 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, =BF=C0=C8=C4 4:54, Ryan Davis =C0=DB=BC=BA:
 
D

David A. Black

Hi --

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
 
R

Ryan Davis

Hi --

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:
=> :horrible
 
R

Ryan Davis

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.
 
J

Jun Young Kim

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, =BF=C0=C8=C4 9:16, David A. Black =C0=DB=BC=BA:
Hi --

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

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

In 1.9 !=3D becomes a method:

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


David

--=20
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)
 
J

Jun Young Kim

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 =20=

just to update ruby's version :).

2008. 12. 30, =BF=C0=C0=FC 9:37, Ryan Davis =C0=DB=BC=BA:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top