From: Robert Klemme Date: 2005-07-15T07:10:51+09:00 Subject: Re: Difference between Class.new and class Levin Alexander wrote: > Hi, > > why do class definitions with the keyword "class" return nil and not > the class itself? Because the last expression in the block is returned: >> class Foo; self end => Foo >> class Bar; 123 end => 123 Method definitions ("def ...") return nil. > >> class Foo > >> def foo > >> puts "in Foo" > >> end > >> end > => nil > > and what is the difference to: > > >> Bar = Class.new do > >> define_method(:bar) do > >> puts "in Bar" > >> end > >> end > => Bar The former is the standard syntax for defining classes. The latter can be used to create anonymous classes. >> Irks = Class.new => Irks >> ui = Class.new => # A disadvantage of the latter approach is that you cannot define methods that receive a block. There is no equivalent (yet) for "def foo(&b) b.call(1) end" with define_method, because you cannot do define_method(:foo) {|&b| b.call(1)}. Also, you cannot have default values for method arguments with define_value. The bottom line is: use the block form only if you need anonymous classes or have to otherwise. HTH Kind regards robert