Difference between Class.new and class

Discussion in 'Ruby' started by Levin Alexander, Jul 14, 2005.

  1. Hi,

    why do class definitions with the keyword "class" return nil and not the =
    =20
    class itself?

    >> class Foo
    >> def foo
    >> puts "in Foo"
    >> end
    >> end

    =3D> nil

    and what is the difference to:

    >> Bar =3D Class.new do
    >> define_method:)bar) do
    >> puts "in Bar"
    >> end
    >> end

    =3D> Bar

    Thank you,
    Levin
     
    Levin Alexander, Jul 14, 2005
    #1
    1. Advertising

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

    => #<Class:0x10189d98>

    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
     
    Robert Klemme, Jul 14, 2005
    #2
    1. Advertising

  3. Levin Alexander

    Pete Elmore Guest

    Levin Alexander wrote:
    > Hi,
    >
    > why do class definitions with the keyword "class" return nil and not
    > the class itself?
    >
    > >> class Foo
    > >> def foo
    > >> puts "in Foo"
    > >> end
    > >> end

    > => nil

    irb(main):001:0> class X
    irb(main):002:1> def y
    irb(main):003:2> puts "z"
    irb(main):004:2> end
    irb(main):005:1> self
    irb(main):006:1> end
    => X
    The class definition returns the result of the last statement, so you
    can put 'self' just before the end of the definition to get it to return
    the class. It's returning nil because def returns nil.
     
    Pete Elmore, Jul 14, 2005
    #3
  4. Robert Klemme wrote:

    > 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)}.


    (flori@lambda:~ 0)$ irb
    irb(main):001:0> A = Class.new { define_method:)foo) {|&b| b.call(1)} }
    => A
    irb(main):002:0> A.new.foo { |x| 2 * x }
    => 2
    irb(main):003:0> RUBY_VERSION
    => "1.9.0"

    --
    Florian Frank
     
    Florian Frank, Jul 15, 2005
    #4
  5. "Robert Klemme" <> writes:

    > 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)}.


    As Florian Frank pointed out, this is fixed in Ruby 1.9.

    > Also, you cannot have default values for method arguments
    > with [define_method].


    I just wanted to point out that this is not a show-stopping problem.
    In Ruby 1.9, `define_method' can do everything `def' can.

    def foo(bar, baz = QUUX, *rest)
    ...
    end

    can be written as

    define_method :foo do |bar, *rest|
    baz = rest.empty? and QUUX or rest.shift
    ...
    end

    For longer parameter lists, you could refactor a bit:

    class Array
    def shift_or default
    if empty? then default else shift end
    end
    end

    define_method :foo do |bar, *rest|
    baz1 = rest.shift_or QUUX1
    baz2 = rest.shift_or QUUX2
    baz3 = rest.shift_or QUUX3
    ...
    end

    But of course, you already know this.

    As I said, I just wanted to point out that `def' and `define_method'
    are essentially equal in power as of Ruby 1.9.

    --
    Daniel Brockman <>

    So really, we all have to ask ourselves:
    Am I waiting for RMS to do this? --TTN.
     
    Daniel Brockman, Jul 15, 2005
    #5
  6. Daniel Brockman <> writes:

    > def foo(bar, baz = QUUX, *rest)
    > ...
    > end
    >
    > can be written as
    >
    > define_method :foo do |bar, *rest|
    > baz = rest.empty? and QUUX or rest.shift
    > ...
    > end


    My apologies, that should have been

    define_method :foo do |bar, *rest|
    baz = rest.empty? && QUUX || rest.shift
    ...
    end

    --
    Daniel Brockman <>
     
    Daniel Brockman, Jul 15, 2005
    #6
  7. Robert Klemme wrote:

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


    But you can use def in Class.new blocks:

    Bar = Class.new do
    def two_times(&block)
    2.times(&block)
    end
    end

    Bar.new.two_times { puts "Hello World!"}

    This is on 1.8.2.
     
    Florian Groß, Jul 15, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,510
  2. Mario Fratelli
    Replies:
    32
    Views:
    1,443
    Phlip
    Dec 9, 2003
  3. Aarti
    Replies:
    3
    Views:
    373
    Neelesh Bodas
    Jul 20, 2007
  4. Anarki
    Replies:
    1
    Views:
    301
    Alf P. Steinbach
    Aug 12, 2007
  5. Brandon Dimcheff
    Replies:
    2
    Views:
    160
    Brandon Dimcheff
    Oct 7, 2008
Loading...

Share This Page