This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# see Metaprogramming Ruby, p. 108 | |
class C | |
def a_method | |
"C#a_method" | |
end | |
def eigenclass | |
class << self; self; end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# see Metaprogramming Ruby, p. 108 | |
s1, s2 = "abc", "def" | |
s1.instance_eval do | |
def swoosh!; reverse; end | |
end | |
s1.swoosh! # cba | |
s2.respond_to?(:swoosh!) # false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# see Metaprogramming Ruby, p. 107 | |
obj = Object.new | |
eigenclass = class << obj | |
self | |
end | |
eigenclass.class # Class | |
def obj.my_singleton_method; end | |
eigenclass.instance_methods.grep(/my_/) # ["my_singleton_method"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class C; end | |
class << C | |
def my_macro(arg) | |
"my_macro(#{arg}) called" | |
end | |
end | |
class C | |
my_macro :x # my_macro(x) called |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# see Metaprogramming Ruby, p. 103 | |
class MyClass | |
def self.my_class_method; end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# see Metaprogramming Ruby, p. 101 | |
str = "just a regular string" | |
def str.title? | |
self.upcase == self | |
end | |
str.title? # false | |
str.methods.grep(/title?/) # ["title?"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# see Metaprogramming Ruby, p. 93 | |
def add_method_to(a_class) | |
a_class.class_eval do | |
def m; 'Hello!'; end | |
end | |
end | |
add_method_to String | |
puts "foo".m # Hello! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# see Metaprogramming Ruby, p. 92 | |
result = class MyClass | |
self | |
end | |
puts result # MyClass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# see Metaprogramming Ruby, p. 80 | |
def another_double | |
p = Proc.new { return 10 } | |
result = p.call | |
return result * 2 # will not pass here | |
end | |
puts another_double |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# see Metaprogramming Ruby, p. 80 | |
def double(callable_object) | |
callable_object.call * 2 | |
end | |
l = lambda { return 10 } | |
puts double(l) # 20 |