"class" return value 
Author Message
 "class" return value

All,

I noticed the following while playing in irb:

  class X
    def foo
      4
    end
  end
                  => nil

  class Y
    include Enumerable
  end
                  => Y

The rule appears to be that a class definition returns nil unless it includes a
mixin.  Is this true, and if so, why?

Gavin

--
Gavin Sinclair                                       Software Engineer
Sydney, Australia                            Soyabean Software Pty Ltd



Sat, 09 Apr 2005 20:26:17 GMT  
 "class" return value

G> The rule appears to be that a class definition returns nil unless it includes a
G> mixin.  Is this true, and if so, why?

 Well, in 1.6.7 `class' is a void expression, for example

pigeon% ruby -e 'a = class A; 12; end'
-e:1: void value expression
pigeon%

 but

pigeon% ruby -e 'a = eval "class A; 12; end"; p a'
12
pigeon%

 The class definition return the value of the last expression, `def' return
 nil, `include' return the class

Guy Decoux



Sat, 09 Apr 2005 20:38:02 GMT  
 "class" return value


Quote:

> G> The rule appears to be that a class definition returns nil unless it
includes a
> G> mixin.  Is this true, and if so, why?

>  The class definition return the value of the last expression, `def' return
>  nil, `include' return the class

> Guy Decoux

Thanks for the explanation, Guy.

Gavin



Sat, 09 Apr 2005 22:37:48 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. history of "ignoring return value"

2. "out of band" return values

3. pid and return value from "exec"

4. incorrect return value for "scan" command

5. Returning values with "if"

6. string.join(["Tk 4.2p2", "Python 1.4", "Win32", "free"], "for")

7. coding "else return nothing", "else return blank space" ??

8. Receiver of "return"

9. SubclassOfDate today "returns aDate"

10. Return us "Cancel" button during compilation

11. GET without "Return"

12. scan string for "returns"

 

 
Powered by phpBB® Forum Software