Skip to content

Instantly share code, notes, and snippets.

class Module
def delegate(*methods, to:)
methods.each do |method_name|
define_method(method_name) do |*args|
to.send(method_name, *args)
end
end
end
end
class Director
attr_reader :training_team, :fellows
delegate :train_new_fellow, :train_new_fellow, to: training_team
delegate :size, :[], :<<, to: fellows
def initialize
@training_team = TrainingTeam.new
@fellows = []
end
class Router
[:get, :post, :put, :patch, :delete].each do |method_name|
define_method(method_name) do
"This is a #{method_name} method"
end
end
end
router = Router.new
router.get #=> "This is a get method"
class Person
attr_accessor :name
def book_reading
"Elixir Cookbook"
end
end
Person = Class.new do
attr_accessor :name
def book_reading
"Elixir Cookbook"
end
end
Book = "Eloquent Ruby"
Module.const_get(:Book) #=> "Eloquent Ruby"
Module.const_set(:Book, "Ruby Science") #=> "Ruby Science"
Module.const_get(:Book) #=> "Ruby Science"
class Person
def name
"Sword Master"
end
class Module
def const_missing(constant)
"Constant #{constant} is really missing"
end
end
class House
end
Module.const_get(:House) #=> House
class Person
def initialize(n)
@name = n
end
def issues
family = "No Money"
relationship = "Complex"
friends = "No Friends"
binding
class Person
def name
"Name is Aboki"
end
alias_method :aboki_name, :name
def name
"Ikem Okonkwo"
end
class Person
def name
@name || "person"
end
def name=(val)
return if val == "person"
@name = val
end
end