Hello, I would like to dynamically name a variable... for example, in my program, I iterate over a SQL query that gives me ''displayname'', ''phone_office'', and ''phone_mobile'' (in the actual query there are many more columns). For each row in the results, I want to create an array with [0]=''phone_office'' and [1]=''phone_mobile'' and then I would like to name the array the ''displayname''. Is there a way I could do this? Any help is appreciated! Thanks! - Jeff Miller -- 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 -~----------~----~----~----~------~----~------~--~---
ESPNDev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Apr-24 22:40 UTC
Re: Dynamically Naming a Variable
I can''t believe i''m about to post this, but this will work
user = User.find(params[:id])
arr = eval("#{@user.user_name} = []")
User.column_names.each { |name| arr[arr.size] = user[name.to_sym] }
On Apr 24, 3:15 pm, Jeff Miller
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> Hello,
> I would like to dynamically name a variable... for example, in my
> program, I iterate over a SQL query that gives me
''displayname'',
> ''phone_office'', and ''phone_mobile'' (in
the actual query there are many
> more columns). For each row in the results, I want to create an array
> with [0]=''phone_office'' and
[1]=''phone_mobile'' and then I would like to
> name the array the ''displayname''. Is there a way I could
do this?
>
> Any help is appreciated!
>
> Thanks!
> - Jeff Miller
> --
> 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@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
ESPNDev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Apr-24 22:50 UTC
Re: Dynamically Naming a Variable
Honestly though I''d urge you to use a hash:
directory = {}
directory[display_name] = []
results = your_SQL_query
results.each {|r| directory[display_name].push r}
On Apr 24, 3:15 pm, Jeff Miller
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> Hello,
> I would like to dynamically name a variable... for example, in my
> program, I iterate over a SQL query that gives me
''displayname'',
> ''phone_office'', and ''phone_mobile'' (in
the actual query there are many
> more columns). For each row in the results, I want to create an array
> with [0]=''phone_office'' and
[1]=''phone_mobile'' and then I would like to
> name the array the ''displayname''. Is there a way I could
do this?
>
> Any help is appreciated!
>
> Thanks!
> - Jeff Miller
> --
> 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@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
thanks for your help, a co-worker of mine just showed me how to use hashes. I''m still pretty new to Ruby, so I honestly didn''t even know of how powerful a hash can be. It makes a lot more sense to do it that way! Thanks again, - Jeff Miller -- 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 -~----------~----~----~----~------~----~------~--~---