Hello, I realise that Ruby is a both powerful and elegant language, so I have a question on how to improve my code. I am currently working on a CMS. I decided to put some predefined and constant database rows into a table, and that code must be run once at the initialization of the application (in environment.rb). So below is that code: RawTexts = 6 if RawText.find(:all).length != RawTexts do sfi = RawText.new sfi.name = ''sfi'' sfi.save! conference_plan = RawText.new conference_plan.name = ''conference_plan'' conference_plan.save! workshops_plan = RawText.new workshops_plan.name = ''workshops_plan'' workshops_plan.save! city_map = RawText.new city_map.name = ''city_map'' city_map.save! organizers = RawText.new organizers.name = ''organizers'' organizers.save! partners = RawText.new partners.name = ''partners'' partners.save! end My question is: how to make it more elegant? There are some obvious repetitions in the code; is it possible to omit them by some smart Ruby mechanism? Thanks, Mike -- 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:
RawTexts = 6
if RawText.find(:all).length != RawTexts do
[''sfi'', ''conference_plan'',
''workshops_plan'', ''city_map'',
''organizers'', ''partners''].each do |itm|
RawText.create(:name => itm)
end
end
There''s probably an even more elegant way, but that at least removes a
lot of the duplication.
On 6/24/07, Michael
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:>
> Hello,
> I realise that Ruby is a both powerful and elegant language, so I have a
> question on how to improve my code. I am currently working on a CMS. I
> decided to put some predefined and constant database rows into a table,
> and that code must be run once at the initialization of the application
> (in environment.rb). So below is that code:
>
> RawTexts = 6
> if RawText.find(:all).length != RawTexts do
> sfi = RawText.new
> sfi.name = ''sfi''
> sfi.save!
> conference_plan = RawText.new
> conference_plan.name = ''conference_plan''
> conference_plan.save!
> workshops_plan = RawText.new
> workshops_plan.name = ''workshops_plan''
> workshops_plan.save!
> city_map = RawText.new
> city_map.name = ''city_map''
> city_map.save!
> organizers = RawText.new
> organizers.name = ''organizers''
> organizers.save!
> partners = RawText.new
> partners.name = ''partners''
> partners.save!
> end
>
> My question is: how to make it more elegant? There are some obvious
> repetitions in the code; is it possible to omit them by some smart Ruby
> mechanism?
>
> Thanks,
> Mike
>
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>
--
http://www.jeremymcanally.com/
My free Ruby e-book:
http://www.humblelittlerubybook.com/book/
My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.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
-~----------~----~----~----~------~----~------~--~---
P.S. Erratum - there shouldn''t be "do" in the second line of the code of course ;) -- 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 -~----------~----~----~----~------~----~------~--~---
Thanks very much ;) -- 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 -~----------~----~----~----~------~----~------~--~---
Hi -- On Mon, 25 Jun 2007, Michael wrote:> > Hello, > I realise that Ruby is a both powerful and elegant language, so I have a > question on how to improve my code. I am currently working on a CMS. I > decided to put some predefined and constant database rows into a table, > and that code must be run once at the initialization of the application > (in environment.rb). So below is that code: > > RawTexts = 6 > if RawText.find(:all).length != RawTexts doSee Jeremy''s answer for shortening the code. Also you can do: if RawText.count instead of find(:all).length. That will save you loading all of the records into memory. David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.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 -~----------~----~----~----~------~----~------~--~---