Difference between Foo = Class.new and class Foo

Discussion in 'Ruby' started by Brandon Dimcheff, Oct 7, 2008.

  1. I'm playing around with classes and discovered that

    class Foo
    ...
    end

    and

    Foo = Class.new do
    ...
    end

    seem to behave slightly differently. I assumed that "class Foo" was
    just a shortcut that got translated into "Foo = Class.new", but when I
    muck with class in the following way:

    class Class
    class << self
    def new
    puts "new called"
    super
    end
    end
    end

    and then do "Foo = Class.new", I get "new called" printed to stdout.
    If I do "class Foo..." I get nothing. So it seems that the "class"
    keyword does not end up calling new on Class when you define a new
    class. Does anybody know what's going on here? Is something else
    called instead?

    Thanks,
    Brandon
    Brandon Dimcheff, Oct 7, 2008
    #1
    1. Advertising

  2. 2008/10/7 Brandon Dimcheff <>:
    > and then do "Foo = Class.new", I get "new called" printed to stdout. If I
    > do "class Foo..." I get nothing. So it seems that the "class" keyword does
    > not end up calling new on Class when you define a new class. Does anybody
    > know what's going on here? Is something else called instead?


    Looks like there is a shortcut:

    16:55:57 bas$ ruby -e 'set_trace_func lambda {|*a|p a}; class Foo;end'
    ["line", "-e", 1, nil, #<Binding:0x1002f0d4>, false]
    ["c-call", "-e", 1, :inherited, #<Binding:0x1002ef80>, Class]
    ["c-return", "-e", 1, :inherited, #<Binding:0x1002ef44>, Class]
    ["class", "-e", 1, nil, #<Binding:0x1002ed64>, false]
    ["end", "-e", 1, nil, nil, false]
    16:56:13 bas$ ruby -e 'set_trace_func lambda {|*a|p a}; Foo=Class.new'
    ["line", "-e", 1, nil, #<Binding:0x1002f0e8>, false]
    ["c-call", "-e", 1, :new, #<Binding:0x1002f0ac>, Class]
    ["c-call", "-e", 1, :initialize, #<Binding:0x1002eeb8>, Class]
    ["c-call", "-e", 1, :inherited, #<Binding:0x1002edb4>, Class]
    ["c-return", "-e", 1, :inherited, #<Binding:0x1002ed78>, Class]
    ["c-return", "-e", 1, :initialize, #<Binding:0x1002ec88>, Class]
    ["c-return", "-e", 1, :new, #<Binding:0x1002eb98>, Class]
    16:56:44 bas$

    If you need hooks for class creation look closely at ":inherited" above.

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    Robert Klemme, Oct 7, 2008
    #2
    1. Advertising

  3. hmm, interesting. I'm definitely going to have to use set_trace_func
    more often. Do you know if there's any way to tell which c function
    is being called for the c-calls? Like how does the ruby method
    "inherited" map to rb_... in MRI?

    Thanks,
    Brandon

    On Oct 7, 2008, at 10:56, Robert Klemme wrote:

    > 2008/10/7 Brandon Dimcheff <>:
    >> and then do "Foo = Class.new", I get "new called" printed to
    >> stdout. If I
    >> do "class Foo..." I get nothing. So it seems that the "class"
    >> keyword does
    >> not end up calling new on Class when you define a new class. Does
    >> anybody
    >> know what's going on here? Is something else called instead?

    >
    > Looks like there is a shortcut:
    >
    > 16:55:57 bas$ ruby -e 'set_trace_func lambda {|*a|p a}; class Foo;end'
    > ["line", "-e", 1, nil, #<Binding:0x1002f0d4>, false]
    > ["c-call", "-e", 1, :inherited, #<Binding:0x1002ef80>, Class]
    > ["c-return", "-e", 1, :inherited, #<Binding:0x1002ef44>, Class]
    > ["class", "-e", 1, nil, #<Binding:0x1002ed64>, false]
    > ["end", "-e", 1, nil, nil, false]
    > 16:56:13 bas$ ruby -e 'set_trace_func lambda {|*a|p a}; Foo=Class.new'
    > ["line", "-e", 1, nil, #<Binding:0x1002f0e8>, false]
    > ["c-call", "-e", 1, :new, #<Binding:0x1002f0ac>, Class]
    > ["c-call", "-e", 1, :initialize, #<Binding:0x1002eeb8>, Class]
    > ["c-call", "-e", 1, :inherited, #<Binding:0x1002edb4>, Class]
    > ["c-return", "-e", 1, :inherited, #<Binding:0x1002ed78>, Class]
    > ["c-return", "-e", 1, :initialize, #<Binding:0x1002ec88>, Class]
    > ["c-return", "-e", 1, :new, #<Binding:0x1002eb98>, Class]
    > 16:56:44 bas$
    >
    > If you need hooks for class creation look closely at ":inherited"
    > above.
    >
    > Kind regards
    >
    > robert
    >
    > --
    > remember.guy do |as, often| as.you_can - without end
    >
    Brandon Dimcheff, Oct 7, 2008
    #3
    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. Replies:
    3
    Views:
    375
    John Roth
    Jul 29, 2005
  2. Manuel
    Replies:
    6
    Views:
    293
    Russell Wood
    Jan 7, 2006
  3. Surgeon
    Replies:
    135
    Views:
    925
  4. PerlFAQ Server
    Replies:
    0
    Views:
    100
    PerlFAQ Server
    Feb 15, 2011
  5. Replies:
    4
    Views:
    134
    Thomas 'PointedEars' Lahn
    Dec 23, 2007
Loading...

Share This Page