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
Try this:
families = Magick.fonts.map{|f| f.family}
On Nov 7, 9:48 am, Rajeev K. [email protected]
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. [email protected]
On Nov 7, 2007 9:48 AM, Rajeev K. [email protected]
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. I
think this is going to be standard in Ruby 1.9.
Something like this maybe?
<%= select(“SomeModel”, “SomeAttribute”, @f {|f| [ f ] }), :prompt =>
“Select a font” %>
On Nov 7, 11:06 am, Rajeev K. [email protected]
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. [email protected]
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. I
think this is going to be standard in Ruby 1.9.
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. [email protected]
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. [email protected]