-
-
Save gunn/780809 to your computer and use it in GitHub Desktop.
A large executable list of methods of define singleton methods in ruby
This file contains 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
############################################## | |
# Different ways to define singleton methods # | |
############################################## | |
# Being able to reference external instance variables when creating a method | |
# enables useful meta-programming techniques in ruby. The styles which are | |
# capable of this will define methods that "Moof!" (like Clarus the Dogcow). | |
moof = "Moof!" | |
def test_singleton_method desc="#?" | |
animal = 'dog' | |
yield animal | |
puts "#{desc}: \t#{animal.speak}" | |
end | |
#============================================= | |
test_singleton_method "#1" do |dog| | |
def dog.speak | |
defined?(moof)? moof : "Woof" | |
end | |
end | |
test_singleton_method "#2" do |dog| | |
dog.instance_eval do | |
def speak | |
defined?(moof)&&moof||"Woof" | |
end | |
end | |
end | |
test_singleton_method "#3" do |dog| | |
class << dog | |
def speak | |
defined?(moof)? moof : "Woof" | |
end | |
end | |
end | |
test_singleton_method "#4" do |dog| | |
class << dog | |
define_method(:speak) do | |
defined?(moof)? moof : "Woof" | |
end | |
end | |
end | |
test_singleton_method "#5" do |dog| | |
class << dog | |
define_method(:speak, lambda { defined?(moof)? moof : "Woof" }) | |
end | |
end | |
test_singleton_method "#6" do |dog| | |
module SpeakableDog | |
def speak | |
defined?(moof)? moof : "Woof" | |
end | |
end | |
class << dog | |
include SpeakableDog | |
end | |
end | |
test_singleton_method "#7" do |dog| | |
module SpeakableDog | |
def speak | |
defined?(moof)? moof : "Woof" | |
end | |
end | |
dog.extend SpeakableDog | |
end | |
test_singleton_method "#8" do |dog| | |
klass = class << dog; self; end | |
klass.send :define_method, :speak do | |
defined?(moof)? moof : "Woof" | |
end | |
end | |
#================ Ruby 1.9.2 ==================== | |
test_singleton_method "#9" do |dog| | |
module SpeakableDog | |
def speak | |
defined?(moof)? moof : "Woof" | |
end | |
end | |
dog.singleton_class.send(:include, SpeakableDog) | |
end | |
test_singleton_method "#10" do |dog| | |
dog.singleton_class.class_eval do | |
def speak | |
defined?(moof)? moof : "Woof" | |
end | |
end | |
end | |
test_singleton_method "#11" do |dog| | |
dog.singleton_class.send(:define_method, :speak) do | |
defined?(moof)? moof : "Woof" | |
end | |
end | |
test_singleton_method "#12" do |dog| | |
dog.define_singleton_method(:speak) do | |
defined?(moof)? moof : "Woof" | |
end | |
end | |
test_singleton_method "#13" do |dog| | |
dog.singleton_class.send(:define_method, :speak, lambda { defined?(moof)? moof : "Woof" }) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment