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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Try this: families = Magick.fonts.map{|f| f.family} On Nov 7, 9:48 am, Rajeev Kala <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Kala <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 7, 2007 9:48 AM, Rajeev Kala <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 > endUsing @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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 name="font"> <option value="">Select a font</option> <option value="">----------------------</option> <option value="Verdana">Verdana</option> <option value="Times New Roman">Times New Roman</option> <option value="Arial">Arial</option> <option value="Zrnic">Zrnic</option> ......... </select> Cheers Bob Showalter wrote:> On Nov 7, 2007 9:48 AM, Rajeev Kala <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > 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.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Something like this maybe? <%= select("SomeModel", "SomeAttribute", @f {|f| [ f ] }), :prompt => "Select a font" %> On Nov 7, 11:06 am, Rajeev Kala <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 name="font"> > <option value="">Select a font</option> > <option value="">----------------------</option> > > <option value="Verdana">Verdana</option> > > <option value="Times New Roman">Times New Roman</option> > <option value="Arial">Arial</option> > <option value="Zrnic">Zrnic</option> > ......... > </select> > > Cheers > > > > Bob Showalter wrote: > > On Nov 7, 2007 9:48 AM, Rajeev Kala <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > 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. > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I attempted your way but couldn''t get it to work. I came up with something like this: <select name="font"> <option value="">Select a font</option> <option value="">----------------------</option> <% @families = Magick.fonts.map{|f| f.family} %> <% for families in @families %> <option value="<%= families %>"><%= families %></option> <% end %> </select> 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 Kala <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Guys ... put this in a helper ... littering your views with procedural code. Will be difficult to maintain. On Nov 7, 4:44 pm, Rajeev Kala <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I attempted your way but couldn''t get it to work. I came up with > something like this: > > <select name="font"> > <option value="">Select a font</option> > <option value="">----------------------</option> > > <% @families = Magick.fonts.map{|f| f.family} %> > <% for families in @families %> > <option value="<%= families %>"><%= families %></option> > <% end %> > </select> > > 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 Kala <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---