Skip to content

Instantly share code, notes, and snippets.

@saragibby
Last active August 7, 2016 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saragibby/c0fdedcf67d6c74634c6073d7e80a7b8 to your computer and use it in GitHub Desktop.
Save saragibby/c0fdedcf67d6c74634c6073d7e80a7b8 to your computer and use it in GitHub Desktop.
# --- BEGIN EXAMPLE 1 ---
class MyClass
end
# Terminal
my_object = MyClass.new
my_object.class
my_object.superclass
MyClass.class
MyClass.superclass
# --- END EXAMPLE 1 ---
# --- BEGIN EXAMPLE 2 ---
class Bacon
def fry
puts "I'm being fried!"
end
end
# Terminal
b = Bacon.new
b.fry
# --- END EXAMPLE 2 ---
# --- BEGIN EXAMPLE 3 ---
class Food
def bake
puts "400 degrees is where it's at"
end
end
class Bacon < Food
def fry
puts "I'm being fried!"
end
end
# Terminal
b = Bacon.new
b.bake
Bacon.ancestors
# --- END EXAMPLE 3 ---
# --- BEGIN EXAMPLE 4 ---
class Book
def contents
self
end
end
b = Book.new
# Terminal
b.contents == b
b == Book.new
b == Book
# --- END EXAMPLE 4 ---
# --- BEGIN EXAMPLE 5 ---
module LibraryItem
def contents
self
end
end
class Book
include LibraryItem
end
b = Book.new
# Terminal
b.contents == b
b == Book.new
b == Book
# --- END EXAMPLE 5 ---
# --- BEGIN EXAMPLE 6 ---
class Book
def self.contents
self
end
end
b = Book.new
# Terminal
b.contents
Book.contents
Book.contents == Book
# --- END EXAMPLE 6 ---
# --- BEGIN EXAMPLE 7 ---
foo = Array.new
def foo.size
"Hello World!"
end
# Terminal
foo.size
foo.class
bar = Array.new
bar.size
bar.class
foo.singleton_methods
foo.singleton_class
bar.singleton_methods
bar.singleton_class
# --- END EXAMPLE 7 ---
# --- BEGIN EXAMPLE 8 ---
foo = Array.new
class << foo
def size
puts "Hello World!"
end
def uniq
puts "There can be only one"
end
end
# Terminal
foo.size
foo.uniq
foo.class
bar = Array.new
bar.size
bar.uniq
bar.class
foo.singleton_methods
foo.singleton_class
bar.singleton_methods
bar.singleton_class
# --- END EXAMPLE 8 ---
# --- BEGIN EXAMPLE 9 ---
class Book
class << self
def table_of_contents
puts "where to find a topic"
end
def index
puts "index of all those cool new terms"
end
def copyright
puts "copyrighted yo"
end
end
end
# Terminal
b = Book.new
b.copyright
Book.copyright
# --- END EXAMPLE 9 ---
# --- BEGIN EXAMPLE 10 ---
class Food
def bake
puts "400 degrees is where it's at"
end
end
class Bacon < Food
def fry
puts "I'm being fried!"
end
def method_missing(m, *args)
puts "Fry + Enjoy. There is nothing else."
end
end
# Terminal
b = Bacon.new
b.cook
f = Food.new
f.cook
# --- END EXAMPLE 10 ---
# --- BEGIN EXAMPLE 11 ---
class Food
def bake
puts "400 degrees is where it's at"
end
end
class Bacon < Food
def fry
puts "I'm being fried!"
end
def method_missing(m, *args)
puts "No method: #{m}"
puts "No use for args: #{args}"
puts "Fry + Enjoy. There is nothing else."
end
end
# Terminal
b = Bacon.new
b.cook(degrees: 400)
# --- END EXAMPLE 11 ---
# --- BEGIN EXAMPLE 12 ---
class Food
define_method(:ymm) { puts "Did you say bacon?" }
end
# Terminal
f = Food.new
f.ymm
# --- END EXAMPLE 12 ---
# --- BEGIN EXAMPLE 13 ---
class Food
def bake
puts "400 degrees is where it's at"
end
end
class Bacon < Food
def fry
puts "I'm being fried!"
end
def method_missing(m, *args)
puts "No method: #{m}"
puts "No use for args: #{args}"
puts "Fry + Enjoy. There is nothing else."
end
end
class Strawberry < Food
['bake', 'fry'].each do |method_name|
define_method(method_name) do
puts "Forget that! Strawberries are delish as is"
end
end
end
# Terminal
berry = Strawberry.new
b.fry
# --- END EXAMPLE 13 ---
# --- BEGIN EXAMPLE 14 ---
class Food
def bake
puts "400 degrees is where it's at"
end
5.times do |i|
define_method("buy_#{i}") { puts "#{i} #{self.class} added to shopping list" }
end
end
# Terminal
b = Food.new
b.buy_1
# --- END EXAMPLE 14 ---
# --- BEGIN EXAMPLE 15 ---
# Terminal
bacon = “Bacon”
bacon.define_singleton_method(:ymm) { puts “more please!” }
bacon.methods
bacon.singleton_methods
jam = “Jam”
jam.methods
jam.singleton_methods
# --- END EXAMPLE 15 ---
# --- BEGIN EXAMPLE 16 ---
class Person
def initialize(args)
define_singleton_method(args[:name]) { puts "heyo" }
end
end
# Terminal
p = Person.new(name: ‘jane’)
p.jane
# --- END EXAMPLE 16 ---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment