Skip to content

Instantly share code, notes, and snippets.

@btaitelb
Created November 14, 2017 16:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save btaitelb/6ed18e90ebab3d4571637e79618b8564 to your computer and use it in GitHub Desktop.
class Animal
attr_reader :legs
attr_accessor :name
def initialize(legs = 4)
@legs = legs
puts "Animal#initialize in #{__FILE__}"
end
def self.classname_from_filename
File.basename(__FILE__).sub(/\.\w+$/, '')
end
define_method "is_a_#{self.classname_from_filename}?" do
true
end
def speak
puts "I don't know how to speak"
@just_ate = false
self
end
def eat
puts "chomp chomp"
@just_ate = true
self
end
def upright?
@legs == 2 && !@just_ate
end
def to_s
"Animal [#{@name}]"
end
end
[:Dog, :Cat].each do |animal|
eval("class #{animal}; end")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment