Skip to content

Instantly share code, notes, and snippets.

@vincent178
Last active December 21, 2015 16:09
Show Gist options
  • Save vincent178/6331938 to your computer and use it in GitHub Desktop.
Save vincent178/6331938 to your computer and use it in GitHub Desktop.
Different class definitions in Ruby
class Logger
def self.add_logging
def log(msg)
STDERR.puts Time.now.strftime("%H:%M:%S: ") + "#{self} (#{msg})"
end
end
end
class Example < Logger
add_logging
end
ex = Example.new
ex.log("hello")
class Logger
def self.add_logging(id_string)
define_method(:log) do |msg|
now = Time.now.strftime("%H:%M:%S")
STDERR.puts "#{now}-#{id_string}: #{self} (#{msg})"
end
end
end
class Song < Logger
add_logging "Tune"
end
class Album < Logger
add_logging "CD"
end
song = Song.new song.log("rock on")
class AttrLogger
def self.attr_logger(name)
attr_reader name
define_method("#{name}=") do |val|
puts "Assigning #{val.inspect} to #{name}"
instance_variable_set("@#{name}", val)
end
end
end
class Example < AttrLogger
attr_logger :value
end
ex = Example.new
ex.value = 123
puts "Value is #{ex.value}"
ex.value = "cat"
puts "Value is now #{ex.value}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment