# Ruby elegance

**URL:** <https://www.ruby-forum.com/t/ruby-elegance/107666>\
**Category:** Rails\
**Created:** [June 25, 2007, 2:15am UTC](https://www.ruby-forum.com/t/ruby-elegance/107666 "2007-06-25T02:15:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@michael](https://www.ruby-forum.com/u/michael)\
**Post date:** [June 25, 2007, 2:15am UTC](https://www.ruby-forum.com/t/ruby-elegance/107666/1 "2007-06-25T02:15:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![michael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@michael](https://www.ruby-forum.com/u/michael)\
**Post date:** [June 25, 2007, 2:32am UTC](https://www.ruby-forum.com/t/ruby-elegance/107666/2 "2007-06-25T02:32:41Z")

</div>

P.S. Erratum - there shouldn’t be “do” in the second line of the code of  
course 😉

---

<div class="post-metadata">

**Author:** ![michael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@michael](https://www.ruby-forum.com/u/michael)\
**Post date:** [June 25, 2007, 2:33am UTC](https://www.ruby-forum.com/t/ruby-elegance/107666/3 "2007-06-25T02:33:21Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> sfi = RawText.new  
> city\_map.save!  
> mechanism?
> 
> Thanks,  
> Mike
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
[http://www.jeremymcanally.com/](http://www.jeremymcanally.com/)

My free Ruby e-book:  
[http://www.humblelittlerubybook.com/book/](http://www.humblelittlerubybook.com/book/)

My blogs:

> **[Site not found · DreamHost](http://www.mrneighborly.com/)**
>
> The owner of this domain has not yet uploaded their website.

[http://www.rubyinpractice.com/](http://www.rubyinpractice.com/)

---

<div class="post-metadata">

**Author:** ![michael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@michael](https://www.ruby-forum.com/u/michael)\
**Post date:** [June 25, 2007, 11:43am UTC](https://www.ruby-forum.com/t/ruby-elegance/107666/4 "2007-06-25T11:43:16Z")

</div>

Thanks very much 😉

---

<div class="post-metadata">

**Author:** ![michael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@michael](https://www.ruby-forum.com/u/michael)\
**Post date:** [June 25, 2007, 1:14pm UTC](https://www.ruby-forum.com/t/ruby-elegance/107666/5 "2007-06-25T13:14:29Z")

</div>

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 do

See 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](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
