Something strange in ruby or I'm a newbie?

Question 1:

#IS THIS A BUG
class TestBug
def test
“Hello Bug”
end
end
class FoundBug < TestBug
def test
super +" ,I found you !" #Notice here
end
end
test = FoundBug.new()
puts test.test

It blame me that
“undefined method `+@’ for " ,I found you !”:String (NoMethodError)
from C:/DOCUME~1/HUSSAC~1/LOCALS~1/Temp/rb1F.tmp:12"
Why?
This is bug or I miss something?

If I replace the line in ‘#Notice here’ to

super+" ,I found you !" #With no space between super and my string
#OR
super + " ,I found you !" #With space both side of ‘+’
#I found that no error

Why?

Question 2:

nil in ruby can put in condition statement
such as if(nil) it will assume that’s false

ex.
if(nil)
else
puts “nil is false”
end

sure it show me the string
Becuase I’m curious,I want to test more

puts nil==nil #Yeah that’s true

puts !nil #true

puts !nil==true #true

puts nil==false #FALSE !!!

What happen in the last statement? [or I miss something]

On Sep 20, 2006, at 3:37 AM, Hussachai P. wrote:

puts nil==nil #Yeah that’s true
puts !nil #true
puts !nil==true #true
puts nil==false #FALSE !!!

What happen in the last statement? [or I miss something]

nil isn’t false. Both nil and false are objects in ruby. Compare:

% irb

[nil, false].map { |o| o.object_id }
=> [4, 0]

On 9/20/06, Hussachai P. [email protected] wrote:

end
puts nil==false #FALSE !!!

What happen in the last statement? [or I miss something]

That is a very interesting way you are looking at things, I would not
necessarily think Ruby’s behavior as bad.
For myself I think rubies semantics can be expressed as follows

class Object
def true?
true
end
end

class NilClass
def true?
false.true?
end
end

class FalseClass
def true?
self
end
end

p true.true?
p “”.true?
p nil.true?

Now - in the classical terms of thinking you have invoked above, if an
object evaluates to true or false just replace it by #true?. (you can
imagine the interpreter doing that e.g.)

You see somehow comparing
nil == false does not make too much sense in that context, as an analogy
you
have

puts “42 is true” if 42
puts “42**2 is true 2” if 1764
and yet
42 == 1764 => false :wink:

Cheers
Robert


Posted via http://www.ruby-forum.com/.


Deux choses sont infinies : l’univers et la bêtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.

  • Albert Einstein

Thank you Dober.
That’s make sense.

Ryan D. wrote:

On Sep 20, 2006, at 3:37 AM, Hussachai P. wrote:

puts nil==nil #Yeah that’s true
puts !nil #true
puts !nil==true #true
puts nil==false #FALSE !!!

What happen in the last statement? [or I miss something]

nil isn’t false. Both nil and false are objects in ruby. Compare:

% irb

[nil, false].map { |o| o.object_id }
=> [4, 0]

Thank you Davis.
I assume you told me that “==” mean "compare with object_id that’s
right?
Uhmm… it’s OK in other lang use this idea such as Java

negative of every object(I’m not sure) in ruby except nil and false
is false;
negative of nil and false is true

How about 1 and 1.0 both are the same object?
the rule that I just assume above was broken?

and why every object except nil and false return false
what’s the reason? there have already exist in the FAQ?

On Sep 20, 2006, at 6:32 AM, Hussachai P. wrote:

super +" ,I found you !" #Notice here

If I replace the line in ‘#Notice here’ to

super+" ,I found you !" #With no space between super and my string
#OR
super + " ,I found you !" #With space both side of ‘+’
#I found that no error

Why?

No, I’m afraid it’s a feature ;). if you rewrite

super +" ,I found you !" #Notice here

to

super + " ,I found you !" #Notice space after '+'.

it will work. By leaving out the space, you have invoked the unary
‘+’ operator. But, of course, you wanted the binary ‘+’ operator. In
Ruby there are times when spaces and parentheses are critical.

Regards, Morton

On 9/20/06, Morton G. [email protected] wrote:

end
from C:/DOCUME~1/HUSSAC~1/LOCALS~1/Temp/rb1F.tmp:12"
Why?
Ruby there are times when spaces and parentheses are critical.

Regards, Morton

No he found something really strange
look at this

puts “a” +“b”

Do you get his point?
Robert


Deux choses sont infinies : l’univers et la bêtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.

  • Albert Einstein

On 9/20/06, Morton G. [email protected] wrote:

class FoundBug < TestBug
Why?

No, I’m afraid it’s a feature ;). if you rewrite

super +" ,I found you !" #Notice here

to

super + " ,I found you !" #Notice space after '+'.

it will work. By leaving out the space, you have invoked the unary
‘+’ operator. But, of course, you wanted the binary ‘+’ operator. In
Ruby there are times when spaces and parentheses are critical.

In other words,

super +“whatever” is the same as super(+“whatever”) while
super+“whatever” or super + “whatever” is the same as super(*args) +
“whatever”.

right?

[…]

In other words,

super +“whatever” is the same as super(+“whatever”) while
super+“whatever” or super + “whatever” is the same as
super(*args) + “whatever”.

right?

Yep:

def test *args; 41; end

puts test+1 #=> 42
puts test + 1 #=> 42
#but
puts test 1 #=> 41
#and
puts test +1 #=> 41

cheers

Simon

On Sep 20, 2006, at 7:43 AM, Jan S. wrote:

right?
That’s the way I see it. In Ruby I think its best to consider ‘super’
as a pseudo-method call and not as a pseudo-variable as it is most
other OO languages.

Regards, Morton

On Sep 20, 2006, at 7:43 AM, Robert D. wrote:

puts “a” +“b”

Do you get his point?

No. The two cases aren’t comparable. “a” is an object; ‘super’ is a
pseudo-method.

Regards, Morton

On 9/20/06, Jan S. [email protected] wrote:

end
“undefined method `+@’ for " ,I found you !”:String (NoMethodError)

‘+’ operator. But, of course, you wanted the binary ‘+’ operator. In
Ruby there are times when spaces and parentheses are critical.

In other words,

super +“whatever” is the same as super(+“whatever”) while
super+“whatever” or super + “whatever” is the same as super(*args) +
“whatever”.

Impressive, well I kind of dislike it, there is a warning from ruby
though

right?

Robert


Deux choses sont infinies : l’univers et la bêtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.

  • Albert Einstein

On 9/20/06, Morton G. [email protected] wrote:

No he found something really strange
look at this

puts “a” +“b”

Do you get his point?

No. The two cases aren’t comparable. “a” is an object; ‘super’ is a
pseudo-method.

Regards, Morton

It was me who got confused, thx I got the point.

Cheers
Robert


Deux choses sont infinies : l’univers et la bêtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.

  • Albert Einstein
super +" ,I found you !" #Notice here
> "undefined method `+@' for " ,I found you !":String (NoMethodError)

Congratulations! You stumped the parser! (Actually, it’s really easy.)

Your code is attempting to call super, passing one argument:
+" ,I found you !"
That + is the unary + operator, which exists for Integers (and does
nothing), but doesn’t have a definition for strings – hence the error.

In fact, I’m not really sure why it exists. Might make for some
interesting DSLage:
ThingPrinter.run do
+verbose
-coloring

end

Devin

Robert D. wrote:

No he found something really strange
look at this

puts “a” +“b”

Do you get his point?
Robert

Morton is right. Do you get his point?

Hal