Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > method def in method vs method def in block

Reply
Thread Tools

method def in method vs method def in block

 
 
Kyung won Cheon
Guest
Posts: n/a
 
      11-21-2008
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie: def must come before call to def? planetthoughtful Ruby 4 03-12-2007 11:36 AM
Fo:Block can you check to see if a block contains any text by using the block id? morrell XML 1 10-10-2006 07:18 PM
"def self.method" vs "class << self; def method" joevandyk@gmail.com Ruby 7 10-10-2006 08:46 AM
Is there a way to use "def self.new" to do the job of "def initialize"? Sean Ross Ruby 3 12-25-2003 04:59 AM
HttpModule -- how to intercept urls like http://localhost/abc/def or http://localhost/abc/def/ where abc, def are non virtual dir Jiong Feng ASP .Net 0 11-19-2003 05:29 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57