method def in method vs method def in block

Discussion in 'Ruby' started by Kyung won Cheon, Nov 21, 2008.

  1. class A
    def aaa
    puts "called aaa by #{self}"
    def bbb
    puts "called bbb by #{self}"
    end
    end
    end

    a = A.new
    p a.respond_to?:)bbb) # => false
    a.aaa
    p a.respond_to?:)bbb) # => true
    a.bbb

    p A.public_instance_methods.grep(/bbb/) # => ["bbb"]

    a2 = A.new
    a2.bbb

    p a.respond_to?:)ccc) # => false

    a.instance_eval do
    def ccc
    puts "called ccc by #{self}"
    end
    end

    p a.respond_to?:)ccc) # => true
    a.ccc

    p A.public_instance_methods.grep(/ccc/) # => []

    a2.ccc rescue puts $! # undefined method

    #####################
    # What's the diff ??
    # Help Me^^
    #####################
    --
    Posted via http://www.ruby-forum.com/.
    Kyung won Cheon, Nov 21, 2008
    #1
    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. Jiong Feng
    Replies:
    0
    Views:
    813
    Jiong Feng
    Nov 19, 2003
  2. morrell
    Replies:
    1
    Views:
    946
    roy axenov
    Oct 10, 2006
  3. Sean Ross
    Replies:
    3
    Views:
    119
    Aredridel
    Dec 25, 2003
  4. Replies:
    7
    Views:
    155
  5. planetthoughtful

    Newbie: def must come before call to def?

    planetthoughtful, Mar 12, 2007, in forum: Ruby
    Replies:
    4
    Views:
    125
    Pit Capitain
    Mar 12, 2007
Loading...

Share This Page