Skip to content

Instantly share code, notes, and snippets.

@psahni
Last active August 29, 2015 13:57
Show Gist options
  • Save psahni/9739500 to your computer and use it in GitHub Desktop.
Save psahni/9739500 to your computer and use it in GitHub Desktop.
Custom Accessor
module Trait
def traits(*arr)
arr.each do |trait|
class_eval do
define_method trait.to_s + "=" do |val|
@trait||={}
@trait[trait] = val
end
end
end
class_eval do
define_method "traits" do
@trait
end
end
end
end
class TraitImplement
extend Trait
traits :message, :status
end
trait = TraitImplement.new
trait.message = ("Ny Being")
trait.status = '404'
# Message and Status methods are available as hash keys and values.
p trait.traits
#AasanHai
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment