fearless_fool wrote:
> This is more a Ruby than a Rails question -- feel free to redirect me
> as appropriate -- but either this is a Ruby 1.9 bug or I have some
> serious misunderstanding.  Essentially, I've caught the <=> operator
> returning nil -- not -1, 0, or 1, but nil.  Schematically:
> 
> class A
>   attr_accessor :slot
>   fun matches?(s)
>     p "s <=> @slot = #{s <=> @slot}"
>     return (s <=> @slot) == 0
>   end
> end
> 
> Driven from a script, I get something like this:
> 
> # Case 1: Notice that <=> evaluates to nil when it should eval to 0
> a.slot = "dog"
> a.matches?("dog")
> "s <=> @slot = "
> => false
> 
> # Case 2: When I pass a String.new("dog") everything works
> a.slot = "dog"
> a.matches?(String.new("dog"))
> "s <=> @slot = 0"
> => true
> 
> What's really odd is that called interactively, Case 1 works as
> expected.  When called from a script, it exhibits the oddity above.
> 
> Am I missing something fundamental about Ruby and string comparison?
> 
> - ff

Can't reproduce with what you've given.

ruby-1.9.1-p376 > class A
ruby-1.9.1-p376 ?>  attr_accessor :slot
ruby-1.9.1-p376 ?>  def matches? s
ruby-1.9.1-p376 ?>    puts "s <=> @slot = #{s <=> @slot}"
ruby-1.9.1-p376 ?>    (s <=> @slot) == 0
ruby-1.9.1-p376 ?>    end
ruby-1.9.1-p376 ?>  end
 => nil
ruby-1.9.1-p376 > a = A.new
 => #<A:0x000000041915b8>
ruby-1.9.1-p376 > a.slot = "dog"
 => "dog"
ruby-1.9.1-p376 > a.matches? "dog"
s <=> @slot = 0
 => true
ruby-1.9.1-p376 >


String comparison via <=> to nil will return nil. Are you entirely sure 
that in your IRB session your object's @slot isn't nil for some reason?
-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to