D
Dave Howell
I'm trying to set up an object with overridable defaults, so it will act =
like this:
=3D> #<Smee:0x102007618 @middle=3Dnil, @front=3Dnil, =
@back=3Dnil>=3D> #<Smee:0x102007618 @middle=3D"hi", @front=3Dnil, =
@back=3Dnil>=3D> #<Smee:0x101ffdbb8 @middle=3Dnil, @front=3D"O", =
@back=3Dnil>=3D> "utu"
So I can set the properties of an instance of Smee, but if I don't set =
them, or if I set them back to nil, then the corresponding values that I =
set on the class will take their place.
This is the closest thing I've gotten so far and it seems ludicrously =
clunky.
class Smee
attr_writer :front, :middle, :back
class << self
attr_accessor :front, :middle, :back
end
=09
def front
@front || Smee.front
end
def middle
@middle || Smee.middle
end
def back
@back || Smee.back
end
end
I'd like to have other methods that worked the same on the class and the =
instance, but the only way I can figure out how is to type every single =
method twice. There has *got* to be some terribly clever ruby-esque way =
of including instance methods as class methods or vice versa, no?
like this:
=3D> #<Smee:0x102007618 @middle=3Dnil, @front=3Dnil, =
@back=3Dnil>=3D> #<Smee:0x102007618 @middle=3D"hi", @front=3Dnil, =
@back=3Dnil>=3D> #<Smee:0x101ffdbb8 @middle=3Dnil, @front=3D"O", =
@back=3Dnil>=3D> "utu"
So I can set the properties of an instance of Smee, but if I don't set =
them, or if I set them back to nil, then the corresponding values that I =
set on the class will take their place.
This is the closest thing I've gotten so far and it seems ludicrously =
clunky.
class Smee
attr_writer :front, :middle, :back
class << self
attr_accessor :front, :middle, :back
end
=09
def front
@front || Smee.front
end
def middle
@middle || Smee.middle
end
def back
@back || Smee.back
end
end
I'd like to have other methods that worked the same on the class and the =
instance, but the only way I can figure out how is to type every single =
method twice. There has *got* to be some terribly clever ruby-esque way =
of including instance methods as class methods or vice versa, no?