What should "class ... end" and "def ... end" return?

Are they expressions? If not, why not?

It would be nice if they returned a Class and an (Unbound)Method
respectively. It makes it possible to do various modifier-like things
at the point of definition, instead of using symbol lookups
subsequently.

Just a newbie…

Hi,

Are they expressions? If not, why not?

“class” returns last value, and “def” returns nil in 1.8.

$ ruby -ve ‘p(class X;true;end, def x;true;end)’
ruby 1.8.0 (2003-06-06) [i686-linux]
-e:1: warning: void value expression
-e:1: warning: void value expression
true
nil

Hmmm, the warning for “class” doesn’t seem appropriate.

It would be nice if they returned a Class and an (Unbound)Method
respectively. It makes it possible to do various modifier-like things
at the point of definition, instead of using symbol lookups
subsequently.

If you mean Module#public, #private and so on, it should be a
Symbol for “def”.

···

At Sat, 7 Jun 2003 13:08:40 +0900, you CAN teach an old dog … wrote:


Nobu Nakada

Just FYI, for classes under 1.8.0, you can do:

myClass = Class::new( MySuperclass ) {
def foo
puts "Foo!"
end
}

You can then assign it to a constant if you want it to look like you
did ‘class MyClass…’:

MyClass = myClass
myClass.name

=> “MyClass”

For methods, you can use Module#define_method:

irb(main):002:0> class Foo
irb(main):003:1> rval = define_method( :foo ) { puts “Foo!” }
irb(main):004:1> p rval
irb(main):005:1> end
#Proc:0x40806c4c@:3(irb)

=> nil

Some or all of these things may work under 1.6.8 with modifications; I
don’t know.

···


Michael Granger ged@FaerieMUD.org
Rubymage/Believer/Architect
The FaerieMUD Consortium

Question. In 1.6.8, this doesn’t work:

myklass = class Foo
self
end # Error: “void value expression”
p myklass

But this does:

myklass = class <<self
self
end
p myklass

What’s wrong with the first example?

Regards,

Brian.

···

On Sat, Jun 07, 2003 at 01:19:45PM +0900, nobu.nokada@softhome.net wrote:

“class” returns last value, and “def” returns nil in 1.8.

Michael Granger ged@FaerieMUD.org wrote in message news:1A475B69-98F0-11D7-B8FE-000A959D1A74@FaerieMUD.org

Just FYI, for classes under 1.8.0, you can do:

myClass = Class::new( MySuperclass ) {
def foo
puts “Foo!”
end
}

You can then assign it to a constant if you want it to look like you
did ‘class MyClass…’:

MyClass = myClass
myClass.name

=> “MyClass”

For methods, you can use Module#define_method:

irb(main):002:0> class Foo
irb(main):003:1> rval = define_method( :foo ) { puts “Foo!” }
irb(main):004:1> p rval
irb(main):005:1> end
#Proc:0x40806c4c@:3(irb)

=> nil

Some or all of these things may work under 1.6.8 with modifications; I
don’t know.

Thanks for explaining the rules. But I am curious about why these work
this way.

a) X = 55 # assigns 55 to X; returns 55
b) class X … end # should be consistent with a)
c) def … end # should be consistent with a)

So, what is the reason why (b) does not return the class, and (c) does
not return the method? [There is probably a good reason]