Skip to content

Instantly share code, notes, and snippets.

@wijet
Created June 10, 2010 22:23
Show Gist options
  • Save wijet/433725 to your computer and use it in GitHub Desktop.
Save wijet/433725 to your computer and use it in GitHub Desktop.
class A
def set_foo(foo)
@foo = foo
end
def get_foo
@foo
end
def set_bar(bar)
@bar = bar
end
def get_bar
@bar
end
def set_boo(boo)
@boo = boo
end
def get_boo
@boo
end
def to_s; "#{@foo}, #{@bar}, #{@boo}" end
end
a = A.new
a.set_foo 'Foo!!'
a.set_boo 'Boo!!'
a.set_bar 'Bar!!'
puts a
class A
[:foo, :bar, :boo].each do |attr|
define_method("set_#{attr}") do |value|
instance_variable_set("@#{attr}", value)
end
define_method("get_#{attr}") do
instance_variable_get("@#{attr}")
end
end
def to_s; "#{@foo}, #{@bar}, #{@boo}" end
end
a = A.new
a.set_foo 'Foo!!'
a.set_boo 'Boo!!'
a.set_bar 'Bar!!'
puts a
class A
[:foo, :bar, :boo].each do |attr|
define_method("#{attr}=") do |value|
instance_variable_set("@#{attr}", value)
end
define_method("#{attr}") do
instance_variable_get("@#{attr}")
end
end
def to_s; "#{@foo}, #{@bar}, #{@boo}" end
end
a = A.new
a.foo = 'Foo!!'
a.boo = 'Boo!!'
a.bar = 'Bar!!'
puts a
class A
attr_accessor :foo, :bar, :boo
def to_s; "#{@foo}, #{@bar}, #{@boo}" end
end
a = A.new
a.foo = 'Foo!!'
a.boo = 'Boo!!'
a.bar = 'Bar!!'
puts a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment