describe "should_not == vs. should !=" it do 5.should_not == 6 end # passes it do 5.should != 6 end # fails end # I''m running the rspec 1.1.2 gem with the corresponding Textmate bundle # The second failure surprises me. # Is != not supported? # I''d like to hear what you all think. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20080118/e36d50ca/attachment.html
On Jan 18, 2008 10:47 PM, David James <davidj503 at gmail.com> wrote:> describe "should_not == vs. should !=" > it do > 5.should_not == 6 > end # passes > > it do > 5.should != 6 > end # fails > end > > # I''m running the rspec 1.1.2 gem with the corresponding Textmate bundle > # The second failure surprises me. > # Is != not supported?Sadly, yes. It is not supported. Because Ruby does not support it. When you say 5 == 3, what that is really saying is 5.==(3), which is how we''re able to support 5.should == 3 (becomes 5.should.==(3)). No such luck w/ !=. C''est la vie. Cheers, David> # I''d like to hear what you all think. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On 19/01/2008, at 17:36 , David Chelimsky wrote:> When you say 5 == 3, what that is really saying is 5.==(3), which is > how we''re able to support 5.should == 3 (becomes 5.should.==(3)).Would it be true to say that the reason "5.should != 3" won''t work is that somewhere inside Ruby the x != y comparison is remapped to !(x == y), and thus the "5.should != 3" is remapped to "not (5.should.== 3)", with rspec generating a failure when "should" sees false coming back from the "==" method? Or am I barking up the wrong tree and potentially misleading dozens of programmers down the garden path? Alex
On Jan 20, 2008 8:01 PM, Alex Satrapa <grail at goldweb.com.au> wrote:> On 19/01/2008, at 17:36 , David Chelimsky wrote: > > > When you say 5 == 3, what that is really saying is 5.==(3), which is > > how we''re able to support 5.should == 3 (becomes 5.should.==(3)). > > Would it be true to say that the reason "5.should != 3" won''t work is > that somewhere inside Ruby the x != y comparison is remapped to !(x > == y), and thus the "5.should != 3" is remapped to "not (5.should.=> 3)", with rspec generating a failure when "should" sees false coming > back from the "==" method? > > Or am I barking up the wrong tree and potentially misleading dozens > of programmers down the garden path?I''m 99 44/100% sure that you have it right. The expression x != y is syntactic sugar for !(x == y) much like x += y is syntactic sugar for x = (x +y) The parser turns these into an internal representation (abstract syntax tree for 1.8, byte-codes for 1.9) which is identical to the second form. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/
> I''m 99 44/100% sure that you have it right. The expression x != y is > syntactic sugar for !(x == y) much like x += y is syntactic sugar for > x = (x +y) > > The parser turns these into an internal representation (abstract > syntax tree for 1.8, byte-codes for 1.9) which is identical to the > second form. >In 1.8 it''s just syntactic sugar. But 1.9 provides actual != and !~ methods so that you can override it in situations like this. (from Ruby-Core: http://www.ruby-forum.com/topic/134608 )
On Jan 21, 2008 8:22 AM, Jim Lindley <jimlindley at gmail.com> wrote:> > I''m 99 44/100% sure that you have it right. The expression x != y is > > syntactic sugar for !(x == y) much like x += y is syntactic sugar for > > x = (x +y) > > > > The parser turns these into an internal representation (abstract > > syntax tree for 1.8, byte-codes for 1.9) which is identical to the > > second form. > > > > In 1.8 it''s just syntactic sugar. But 1.9 provides actual != and !~ > methods so that you can override it in situations like this.Oooooh. Good to know. We''re not 1.9 compatible yet, but when we get there we''ll definitely add this!> > (from Ruby-Core: http://www.ruby-forum.com/topic/134608 ) > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >