Jason Lillywhite wrote:
> I'm thinking there must be a great Ruby-way to do the same thing to many
> different variables. Here is a boiled down version of what I have:
> 
>         @yn = Unit.new("#{@yn} feet")
>         @yc = Unit.new("#{yc} feet")
>         @v = Unit.new("#{@v} fps")
>         @twmax = Unit.new("#{@twmax} lbs/ft^2")
>         @tc = Unit.new("#{@tc} lbs/ft^2")
>         @twc = Unit.new("#{@twc} lbs/ft^2")
> 
> My first thought is to create a hash of variables like this:
> 
>         results = {'yn' => "#{@yn} feet", 'yc' => "#{yc} feet", 'v' =>
> "#{@v} fps", 'twmax' => "#{twmax} lbs/ft^2", 'tc' => "#{tc} lbs/ft^2",
> 'twc' => "#{twc} lbs/ft^2"}
> 
> then use a block something like this?
> 
>         results.each_value {|item| Unit.new(item)}
> 
> ...but that doesn't seem to work. Any ideas to make this better?
> 
> Thank you!

The most hardcore way:

h={:yn=>"feet",:yc=>"feet",...}
h.each_pair\
{ |v,u|
iv=:"@#{v}"
instance_variable_set(iv,Unit::new("#{instance_variable_get(iv)} #{u}"))
}

Or there are some variations using eval, but I think using eval is less 
elegant.

TPR.
-- 
Posted via http://www.ruby-forum.com/.