On 2010-06-16 11:01:47 -0700, Josh Cheek said:

> [Note:  parts of this message were removed to make it a legal post.]
> 
> On Wed, Jun 16, 2010 at 4:31 AM, Ralph Shnelvar <ralphs / dos32.com> wrote:
> 
>> Consider
>> 
>> 
>> ---
>> 
>> def initialize(attributes = nil)
>> 
>> super attributes
>> 
>> self.step                = 0
>> 
>> self.national_debt       = 0
>> 
>> self.sum_of_income       = 0
>> 
>> self.sum_of_taxes        = 0
>> 
>> self.sum_of_happiness    = 0
>> 
>> self.sum_of_investments  = 0
>> 
>> self
>> 
>> end
>> 
>> ---
>> 
>> Please focus on the repeated use of self.
>> 
>> Is there some way to set the, uh, environment so that "self" need not be
>> repeated?
>> 
> 
> I don't think so, but if you wanted to dry it up a bit, you could
> potentially do something like this.
> 
> class Example
> 
>   METHODS =   [ :step, :national_debt, :sum_of_income, :sum_of_taxes,
> :sum_of_happiness, :sum_of_investments ]
>   DEFAULTS =  [ 1    , 2             , [1,2,3,4,5]   , 4            ,
> 5                , 6                   ]
> 
>   attr_accessor( * METHODS - [:sum_of_income] )
> 
>   # just to whow that these need not be so simple as the 1:1 method
> assignment to ivar that attr_accessor gives
>   def sum_of_income=(incomes)
>     summed = incomes.inject { |sum,income| sum + income }
>     @calculated_sum_of_income = summed
>   end
> 
>   def initialize
>     METHODS.zip(DEFAULTS).each { |method,default| send "#{method}=" ,
> default }
>   end
> 
> end
> 
> Example.new # => #<Example:0x10019a770 @sum_of_taxes=4, @national_debt=2,
> @sum_of_investments=6, @step=1, @calculated_sum_of_income=15,
> @sum_of_happiness=5>
> 
> 
> (note that I wouldn't mind better method param support built into core Ruby,
> but doesn't quite apply to this, where it is legitimate to ask how you would
> differentiate between setters local vars)
> 
>  (Joke: Hmm, if you get rid of your self, do you commit suicide?)
>> 
> Being a determinist, "I" have often argued that there is no self and there
> never was. Of course, "I" don't let that stop "me" from being who "I" choose
> to be. (English is cruel to existential commentary)
> 
> 
> On Wed, Jun 16, 2010 at 12:26 PM, Robert Dober <robert.dober / gmail.com>wrote:
> 
>> Only by changing your paradigm and by defining setters like these
>> 
>> %w{ step taxes what }.each do | att_name |
>> define_method att_name do | *args |
>> return instance_variable_get( "@#{att_name}") if args.empty?
>> instance_variable_set( "@#{att_name}", args.first)
>> end
>> end
>> 
>> def tax *values
>> return @tax if values.empty?
>> @tax = valeus.first
>> end
>> ....
>> 
>> 
> This is the approach _why took in Dwemthy's Array.
> 
> http://mislav.uniqpath.com/poignant-guide/book/chapter-6.html#section3
> (best read with the soundtrack
> http://s3.amazonaws.com/mislav.baconfile.com/poignant-guide%2Fchapter-6-downtown.mp3
)

Just 
> 
set the instance variables yourself. Stop replacing clear, explicit 
code with metaprogramming magic that you can't understand the day after 
you write it.

If you're going to use some sort of dynamic attr method that sets with 
an argument and gets without an argument, define an attr_*-like method 
and use it for each attribute so you can see what you're doing.

_why's code is full of metaprogramming where simpler alternatives 
exist. Case in point: Camping. I wonder if it isn't in part to blame 
for the current Ruby fetish with metaprogramming. That said, I do love 
me some chunky bacon.

-- 
Rein Henrichs
http://puppetlabs.com
http://reinh.com