# Help on loop

**URL:** <https://www.ruby-forum.com/t/help-on-loop/119507>\
**Category:** Rails\
**Created:** [November 7, 2007, 3:48pm UTC](https://www.ruby-forum.com/t/help-on-loop/119507 "2007-11-07T15:48:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [November 7, 2007, 3:48pm UTC](https://www.ruby-forum.com/t/help-on-loop/119507/1 "2007-11-07T15:48:16Z")

</div>

Hey,

I have got a script that returns a long list of font family names, which  
I all want to store in a variable (array). I tried doing this with @f =  
font.family also in the for loop below but it only prints the last item  
when I want to display the results.

require ‘rubygems’  
require ‘RMagick’  
include Magick

for font in Magick.fonts  
puts font.family  
end

Any suggestions?

Cheers

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [November 7, 2007, 4:14pm UTC](https://www.ruby-forum.com/t/help-on-loop/119507/2 "2007-11-07T16:14:27Z")

</div>

Try this:

families = Magick.fonts.map{|f| f.family}

On Nov 7, 9:48 am, Rajeev K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [November 7, 2007, 4:17pm UTC](https://www.ruby-forum.com/t/help-on-loop/119507/3 "2007-11-07T16:17:52Z")

</div>

Thanks mate I managed to find a way:

a = []

Magick.fonts do |font|  
a \<\< font.family  
end

puts a[29]

But your one liner works too 🙂

Cheers

ebeard wrote:

> Try this:
> 
> families = Magick.fonts.map{|f| f.family}
> 
> On Nov 7, 9:48 am, Rajeev K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [November 7, 2007, 4:36pm UTC](https://www.ruby-forum.com/t/help-on-loop/119507/4 "2007-11-07T16:36:41Z")

</div>

On Nov 7, 2007 9:48 AM, Rajeev K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> require ‘RMagick’  
> include Magick
> 
> for font in Magick.fonts  
> puts font.family  
> end

Using

@f = font.family

will overwrite @f each time through the loop. That’s why you see only  
the last value.

If you wanted to build up an array, you could write:

@f = []  
for font in Magick.fonts  
@f \<\< font.family  
end

But you can do this all in one line:

@f = Magick.fonts.collect &:family

(I’m assuming you’re doing this in a Rails app. If you’re doing it in  
a standalone script, you would need to add “require ‘active\_support’”  
for the &:family thingy to work)

This is equivalent to what ebeard showed, but uses some new sugar  
Rails gives you. See  
[http://pragdave.pragprog.com/pragdave/2005/11/symbolto\_proc.html](http://pragdave.pragprog.com/pragdave/2005/11/symbolto_proc.html). I  
think this is going to be standard in Ruby 1.9.

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [November 7, 2007, 5:18pm UTC](https://www.ruby-forum.com/t/help-on-loop/119507/5 "2007-11-07T17:18:55Z")

</div>

Something like this maybe?

\<%= select(“SomeModel”, “SomeAttribute”, @f {|f| [f] }), :prompt =\>  
“Select a font” %\>

On Nov 7, 11:06 am, Rajeev K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [November 7, 2007, 5:06pm UTC](https://www.ruby-forum.com/t/help-on-loop/119507/6 "2007-11-07T17:06:49Z")

</div>

Thanks for that Bob got it working. I got another question the names I  
have stored in my array I want to display them as a drop down menu.  
Basically I want something like the code below, but the array contains  
hundreds of names so entering them all manually would be wasting my  
time.

 Select a font ----------------------

```
    <option value="Verdana">Verdana</option>

    <option value="Times New Roman">Times New Roman</option>

```

 Arial Zrnic .........

Cheers

Bob S. wrote:

> On Nov 7, 2007 9:48 AM, Rajeev K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > require ‘RMagick’  
> > include Magick
> > 
> > for font in Magick.fonts  
> > puts font.family  
> > end
> 
> Using
> 
> @f = font.family
> 
> will overwrite @f each time through the loop. That’s why you see only  
> the last value.
> 
> If you wanted to build up an array, you could write:
> 
> @f = []  
> for font in Magick.fonts  
> @f \<\< font.family  
> end
> 
> But you can do this all in one line:
> 
> @f = Magick.fonts.collect &:family
> 
> (I’m assuming you’re doing this in a Rails app. If you’re doing it in  
> a standalone script, you would need to add “require ‘active\_support’”  
> for the &:family thingy to work)
> 
> This is equivalent to what ebeard showed, but uses some new sugar  
> Rails gives you. See  
> [http://pragdave.pragprog.com/pragdave/2005/11/symbolto\_proc.html](http://pragdave.pragprog.com/pragdave/2005/11/symbolto_proc.html). I  
> think this is going to be standard in Ruby 1.9.

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [November 8, 2007, 2:50pm UTC](https://www.ruby-forum.com/t/help-on-loop/119507/7 "2007-11-08T14:50:00Z")

</div>

Guys … put this in a helper … littering your views with procedural  
code. Will be difficult to maintain.

On Nov 7, 4:44 pm, Rajeev K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [November 7, 2007, 5:44pm UTC](https://www.ruby-forum.com/t/help-on-loop/119507/8 "2007-11-07T17:44:56Z")

</div>

I attempted your way but couldn’t get it to work. I came up with  
something like this:

 Select a font ----------------------

\<% @families = Magick.fonts.map{|f| f.family} %\>  
\<% for families in @families %\>

 \<%= families %\> \<% end %\>

Seems to work fine so cant really complain.

Cheers

ebrad wrote:

> Something like this maybe?
> 
> \<%= select(“SomeModel”, “SomeAttribute”, @f {|f| [f] }), :prompt =\>  
> “Select a font” %\>
> 
> On Nov 7, 11:06 am, Rajeev K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
