Skip to content

Instantly share code, notes, and snippets.

@keithrbennett
Created November 5, 2010 21:28
Show Gist options
  • Select an option

  • Save keithrbennett/664901 to your computer and use it in GitHub Desktop.

Select an option

Save keithrbennett/664901 to your computer and use it in GitHub Desktop.
class/self Example
module C
class << self
def f
end
end
end
module D
def self.f
end
end
puts "f is a C class method?: #{C.methods.include?('f') ? 'Yes' : 'No' }"
puts "f is a D class method?: #{D.methods.include?('f') ? 'Yes' : 'No' }"
puts
puts "C.singleton_methods = #{C.singleton_methods.join(', ')}"
puts "D.singleton_methods = #{D.singleton_methods.join(', ')}"
puts
s = 'foo'
class << s
def twice
self * 2
end
end
puts "Call twice: #{s.twice}"
puts "s.singleton_methods = #{s.singleton_methods}"
=begin
Output is:
f is a C class method?: Yes
f is a D class method?: Yes
C.singleton_methods = f
D.singleton_methods = f
Call twice: foofoo
s.singleton_methods = twice
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment