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?

···

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

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]

···

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

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?

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 Sep 20, 2006, at 6:32 AM, Hussachai Puripunpinyo wrote:

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

<snip>

"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

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 Sep 20, 2006, at 3:37 AM, Hussachai Puripunpinyo wrote:

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]

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

···

On 9/20/06, Hussachai Puripunpinyo <siberhus@hotmail.com> wrote:

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

No he found something really strange
look at this

puts "a" +"b"

Do you get his point?
Robert

···

On 9/20/06, Morton Goldberg <m_goldberg@ameritech.net> wrote:

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

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

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

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

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?

···

On 9/20/06, Morton Goldberg <m_goldberg@ameritech.net> wrote:

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

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

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.

Ryan Davis 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 3:37 AM, Hussachai Puripunpinyo wrote:

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

Thank you Dober.
That's make sense.

···

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

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

right?

Robert

···

On 9/20/06, Jan Svitok <jan.svitok@gmail.com> wrote:

On 9/20/06, Morton Goldberg <m_goldberg@ameritech.net> wrote:
> On Sep 20, 2006, at 6:32 AM, Hussachai Puripunpinyo wrote:
>
> > 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?
>
> 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".

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

[...]

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

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, Jan Svitok wrote:

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?

No. The two cases aren't comparable. "a" is an object; 'super' is a pseudo-method.

Regards, Morton

···

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

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

No he found something really strange
look at this

puts "a" +"b"

Do you get his point?

Robert Dober 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

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

Cheers
Robert

···

On 9/20/06, Morton Goldberg <m_goldberg@ameritech.net> wrote:

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

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

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