Skip to content

Instantly share code, notes, and snippets.

@externvoid
Created March 8, 2018 10:26
Show Gist options
  • Select an option

  • Save externvoid/2b5c9ddd6d13a35ed37d57623af097d4 to your computer and use it in GitHub Desktop.

Select an option

Save externvoid/2b5c9ddd6d13a35ed37d57623af097d4 to your computer and use it in GitHub Desktop.
making attr_* method by Ruby code
#内部DSLのソースコード
class Module
def attr_reader(*symbols)
symbols.each do |symbol|
class_eval %(
def #{symbol}
@#{symbol}
end
)
end
end
def attr_writer(*symbols)
symbols.each do |symbol|
class_eval %{
def #{symbol}= (value)
@#{symbol} = value
end
}
end
end
def attr_accessor(*symbols)
attr_reader(*symbols)
attr_writer(*symbols)
end
end
class A
attr_accessor :id, :color
end
a = A.new
a.id = 1
p a.id
class B
def foo(*s)
p s
end
end
foo = B.new.foo(1, :id)
# https://www.xmisao.com/2014/02/10/ruby-attr-accessor-attr-reader-attr-writer.h
tml
# http://gihyo.jp/admin/feature/01/dsl/0001?page=2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment