101.ror
gem install rails
bundle install
bundle exec rake db:migrate
rails s
Dynamic Dispatch
example 1
class Dog
def bark
puts "Woof, woof!"
end
def greet(greeting)
puts greeting
end
end
so:
dog = Dog.new
dog.bark # => Woof, woof!
dog.send("bark") # => Woof, woof!
dog.send(:bark) # => Woof, woof!
method_name = :bark
dog.send method_name # => Woof, woof!
dog.send(:greet, "hello") # => hello
example 2
irb
props = { name: "John", age: 15 }
=> {:name=>"John", :age=>15}
class Person
attr_accessor :name, :age
end
=> nil
person = Person.new
=> #<Person:0x007f8d1c24b908>
props.each { |key, value| person.send("#{key}=", value)}
=> {:name=>"John", :age=>15}
person
=> #<Person:0x007f8d1c24b908 @name="John", @age=15>
Dynamic Methods
class Whatever
define_method :make_it_up do
puts "Whatever..."
end
end
whatever = Whatever.new
whatever.make_it_up # => Whatever...
Related posts by tag
- 07 Nov 2024 daisyUI — Tailwind CSS Components
- 04 Mar 2024 Using personal gems with Bundle without bothering your colleagues.
- 12 Jul 2023 Protomaps - A serverless system for planet-scale maps