Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > iterating methods on multiple parameters

Reply
Thread Tools

iterating methods on multiple parameters

 
 
Jason Lillywhite
Guest
Posts: n/a
 
      09-20-2008
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!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Thomas B.
Guest
Posts: n/a
 
      09-20-2008
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_var iable_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/.

 
Reply With Quote
 
 
 
 
Jason Lillywhite
Guest
Posts: n/a
 
      09-20-2008
Thank you.

But I think my problem is simpler than that. I didn't explain well
enough.

I'm finding that with either each_pair or each_value, I can't get what I
need.

here is a simple case:

h = {'x'=>2, 'y'=>5}

I want to change the values of the hash and assign those to new hash:

i = h.each_value {|v| v * 2}

I wanted i => {'x'=>4, 'y'=>10} but it remains i => {'x'=>2, 'y'=>5}

Is there some form of a 'collect' method for hashes - similar to array?
There must be some other easy answer.

thank you!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Jason Lillywhite
Guest
Posts: n/a
 
      09-20-2008
I just saw a similar issue (#163350 of this forum).

seems like this might work to change values of a hash:

hash = {'x'=>2, 'y'=5}
hash_new = hash.inject({}) do |h, (k,v)|
h[v * 2] = k
h
end

although this returns {10=>"y", 4=>"x"}

something seems amiss...

what do you think?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      09-21-2008
On Sat, Sep 20, 2008 at 6:59 PM, Jason Lillywhite
<> wrote:
> I just saw a similar issue (#163350 of this forum).
>
> seems like this might work to change values of a hash:
>
> hash = {'x'=>2, 'y'=5}
> hash_new = hash.inject({}) do |h, (k,v)|
> h[v * 2] = k
> h
> end
>
> although this returns {10=>"y", 4=>"x"}


With your example...

h = Hash['x', 2, 'y', 5]
h.each_key {|k| h[k] *= 2}

Todd

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      09-21-2008
Jason Lillywhite wrote:
> I just saw a similar issue (#163350 of this forum).
>
> seems like this might work to change values of a hash:
>
> hash = {'x'=>2, 'y'=5}
> hash_new = hash.inject({}) do |h, (k,v)|
> h[v * 2] = k
> h
> end
>
> although this returns {10=>"y", 4=>"x"}
>
> something seems amiss...


hash = {'x'=>2, 'y'=>5}
hash_new = hash.inject({}) do |h, (k,v)|
h[k] = v * 2
h
end
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Jason Lillywhite
Guest
Posts: n/a
 
      09-21-2008
Thanks everyone!

Is there a reason the hsh.inject method is not included in the Ruby
documentation?



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

 
Reply With Quote
 
Rob Biedenharn
Guest
Posts: n/a
 
      09-21-2008
On Sep 21, 2008, at 3:57 PM, Jason Lillywhite wrote:

> Thanks everyone!
>
> Is there a reason the hsh.inject method is not included in the Ruby
> documentation?



Sure it is. In my Programming Ruby, 2 ed., on page 492 it say that
Hash mixes in the Enumerable module and lists inject as one of the
methods. Enumerable#inject itself is documented on page 456.

Depending on where/how you're looking, it is!

$ ri Hash#inject
Nothing known about Hash#inject

$ fri Hash#inject
------------------------------------------------------ Enumerable#inject
enum.inject(initial) {| memo, obj | block } => obj
enum.inject {| memo, obj | block } => obj
------------------------------------------------------------------------
Combines the elements of enum by applying the block to an
accumulator value (memo) and each element in turn. At each step,
memo is set to the value returned by the block. The first form
lets you supply an initial value for memo. The second form uses
the first element of the collection as a the initial value (and
skips that element while iterating).

# Sum some numbers
(5..10).inject {|sum, n| sum + n } #=> 45
# Multiply some numbers
(5..10).inject(1) {|product, n| product * n } #=> 151200

# find the longest word
longest = %w{ cat sheep bear }.inject do |memo,word|
memo.length > word.length ? memo : word
end
longest #=> "sheep"

# find the length of the longest word
longest = %w{ cat sheep bear }.inject(0) do |memo,word|
memo >= word.length ? memo : word.length
end
longest #=> 5



What leads you to think that it isn't documented?

-Rob

Rob Biedenharn http://agileconsultingllc.com




 
Reply With Quote
 
Jason Lillywhite
Guest
Posts: n/a
 
      09-21-2008
You can see I don't know very much about this language. I've been trying
really hard to learn!

I was looking in http://www.ruby-doc.org/core/

and just went to Class Hash and didn't see that method. I didn't think
of Enumerable. I originally thought the collect method would work
similar to arrays and noticed in Google searching someone said Hashes
don't have collect. So then I just assumed Hashes must also be missing
inject as well.

Thanks for your time.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Iterating a std::vector vs iterating a std::map? carl C++ 5 11-25-2009 09:55 AM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
multiple array parameters to object methods? en2guy@hotmail.co.uk Perl Misc 2 07-12-2006 10:48 AM
Servlet parameters different from the command line parameters? Jonck van der Kogel Java 2 05-26-2004 11:34 PM
map/collect iterating over multiple arrays/arguments Zoran Lazarevic Ruby 5 10-08-2003 04:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57