CURRENCIES = [] Currency.find(:all) {|x| CURRENCIES << [x.name + " (" + x.symbol + ")", x.id]} Yet my array stays empty. Seems like I''m missing a step between the find all and the code block. Any suggestions for this rookie? Thanks! -- 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 -~----------~----~----~----~------~----~------~--~---
Fixed it: CURRENCIES = [] @temp = Currency.find(:all) @temp.each {|x| CURRENCIES << [x.name + " (" + x.symbol + ")", x.id]} -- 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 9, 2006, at 3:57 PM, Taylor Strait wrote:> Fixed it: > > CURRENCIES = [] > @temp = Currency.find(:all) > @temp.each {|x| CURRENCIES << [x.name + " (" + x.symbol + ")", x.id]}You could probably just: CURRENCIES = Currency.find(:all).map { |x| ["#{x.name} (# {x.symbol})", x.id] } You almost certainly don''t want to use an instance variable (@temp) where a local variable (temp) would do, but all you were really missing in your first post was the .each on the result of your find (:all) -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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. This works great and is much more succinct: CURRENCIES = Currency.find(:all).map {|x| [x.symbol + " - " + x.name, x.id]} Care to get fancier? A symbol is between 1 - 3 characters long resulting in ugly uneven column of names next to the symbols. Since this array will be used to populate a select, I am trying to even it out with spaces: Currency.find(:all).map {|x| [x.symbol + (4 - x.symbol.length).times do {"+ "} + x.name, x.id]} However I get a syntax error after the second set of brackets. How can I put the spacing code block inside the array creation code block? -- 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 -~----------~----~----~----~------~----~------~--~---
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Taylor Strait wrote:> CURRENCIES = [] > Currency.find(:all) {|x| CURRENCIES << [x.name + " (" + x.symbol + ")", > x.id]} > > Yet my array stays empty. Seems like I''m missing a step between the > find all and the code block. Any suggestions for this rookie? Thanks! >You''re passing a block into the "find" method, you''re not iterating over the results. It would work if you inserted an "each" after the "(:all)". ie: Currency.find(:all).each {|x| CURRENCIES << [x.name + " (" + x.symbol + ")", x.id]} -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.3 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFFU6kxMyx0fW1d8G0RAkzGAJ40yo4kl71JiAChG2SNDxHtv51N6QCfYpK5 iwbdR6gZiH1Wc15tDtbXiqQ=T/pe -----END PGP SIGNATURE----- --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Actually this method won''t work because Ruby doesn''t interpret UTF-8 character length the right way. Of course, it would still be nice to know how to nest blocks! -- 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 9, 2006, at 5:14 PM, Taylor Strait wrote:> Thanks. This works great and is much more succinct: > > CURRENCIES = Currency.find(:all).map {|x| [x.symbol + " - " + x.name, > x.id]} > > > Care to get fancier? A symbol is between 1 - 3 characters long > resulting in ugly uneven column of names next to the symbols. Since > this array will be used to populate a select, I am trying to even > it out > with spaces: > > Currency.find(:all).map {|x| [x.symbol + (4 - > x.symbol.length).times do > {"+ "} + x.name, x.id]} > > > However I get a syntax error after the second set of brackets. How > can > I put the spacing code block inside the array creation code block?For the UTF-8, are you doing: $KCODE = ''UTF8'' require ''jcode'' Then this should work: CURRENCIES = Currency.find(:all).map do |x| [ "%s%*s %s".%([x.symbol, 4-x.symbol.jlength, ''-'', x.name]), x.id ] end look at String#% or Kernel.sprintf for the formatting details This may not work if your UTF-8 characters are of varying display widths in your browser font anyway, but it might be close enough. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 11/9/06, Taylor Strait <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Actually this method won''t work because Ruby doesn''t interpret UTF-8 > character length the right way. Of course, it would still be nice to > know how to nest blocks!Rails 1.2 includes multibyte chars support: x.symbol.chars.length jeremy --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---