# OT: How to make this code prettier?

**URL:** <https://www.ruby-forum.com/t/ot-how-to-make-this-code-prettier/134754>\
**Category:** Rails\
**Created:** [April 14, 2008, 8:26pm UTC](https://www.ruby-forum.com/t/ot-how-to-make-this-code-prettier/134754 "2008-04-14T20:26:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [April 14, 2008, 8:26pm UTC](https://www.ruby-forum.com/t/ot-how-to-make-this-code-prettier/134754/1 "2008-04-14T20:26:49Z")

</div>

This isn’t specific to Rails, but I would like to learn from the  
experience of others.

How would you make this code prettier…

Docdesc = Struct.new(:document\_category\_id,  
:document\_number\_prefix,  
:primary\_part\_number,  
:document\_family\_series,  
:description,  
:revision,  
:release\_status,  
:originator,  
:docdate)  
…  
row = some\_array\_of\_strings\_ints\_and\_floats()  
dd = Docdesc.new  
dd.document\_category\_id = c.id  
dd.document\_number\_prefix = row[0].to\_s unless row[0].nil?  
dd.primary\_part\_number = row[1].to\_s unless row[1].nil?  
dd.document\_family\_series = row[2].to\_s unless row[2].nil?  
dd.description = row[3].to\_s unless row[3].nil?  
dd.revision = row[4].to\_s unless row[4].nil?  
dd.release\_status = row[5].to\_s unless row[5].nil?  
dd.originator = row[6].to\_s unless row[6].nil?  
dd.docdate = row[7].to\_s unless row[7].nil?

–wpd

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [April 14, 2008, 9:19pm UTC](https://www.ruby-forum.com/t/ot-how-to-make-this-code-prettier/134754/2 "2008-04-14T21:19:03Z")

</div>

Something = Struct.new(:one, :two, :three)

index = 0  
for x in %w{ one= two= three= }  
dd.send x, row[index]  
index += 1  
end

* * *

using hash, not struct should be even simpler…

Patrick D. wrote:

> This isn’t specific to Rails, but I would like to learn from the  
> experience of others.
> 
> How would you make this code prettier…
> 
> Docdesc = Struct.new(:document\_category\_id,  
> :document\_number\_prefix,  
> :primary\_part\_number,  
> :document\_family\_series,  
> :description,  
> :revision,  
> :release\_status,  
> :originator,  
> :docdate)  
> …  
> row = some\_array\_of\_strings\_ints\_and\_floats()  
> dd = Docdesc.new  
> dd.document\_category\_id = c.id  
> dd.document\_number\_prefix = row[0].to\_s unless row[0].nil?  
> dd.primary\_part\_number = row[1].to\_s unless row[1].nil?  
> dd.document\_family\_series = row[2].to\_s unless row[2].nil?  
> dd.description = row[3].to\_s unless row[3].nil?  
> dd.revision = row[4].to\_s unless row[4].nil?  
> dd.release\_status = row[5].to\_s unless row[5].nil?  
> dd.originator = row[6].to\_s unless row[6].nil?  
> dd.docdate = row[7].to\_s unless row[7].nil?
> 
> –wpd

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [April 14, 2008, 9:45pm UTC](https://www.ruby-forum.com/t/ot-how-to-make-this-code-prettier/134754/3 "2008-04-14T21:45:15Z")

</div>

> Something = Struct.new(:one, :two, :three)
> 
> index = 0  
> for x in %w{ one= two= three= }  
> dd.send x, row[index]  
> index += 1  
> end  
> Thanks for the suggestion. I thought (a very little bit) about  
> quoting the member names of the structure, but, where they are rather  
> long, I thought the %w{some\_very\_long\_name, some\_other\_very\_log\_name,  
> etc…} looked a little unwieldy, and not any prettier than what I  
> started with, (especially when one has to remember to tack on the  
> equal sign at the end of each name).

Gently guiding this back onto the topic of Rails… later in my code,  
I want to initialize an ActiveRecord object from my Struct element. I  
tried this:

dbdoc = Document.new(doc.hash)

Where Document looks like:

class Document \< ActiveRecord::Base  
belongs\_to :document\_category  
end

and doc is instance of my structure I defined previously. However,  
when I try this, I get an exception:

…/activerecord-2.0.2/lib/active\_record/base.rb:2110:in `dup’: can’t  
dup Fixnum (TypeError)

So I am left with:

```
      dbdoc = Document.new
      dbdoc.document_category_id = doc.document_category_id
      dbdoc.document_number_prefix = doc.document_number_prefix
      dbdoc.primary_part_number = doc.primary_part_number
      dbdoc.document_family_series = doc.document_family_series
      dbdoc.description = doc.description
      dbdoc.revision = doc.revision
      dbdoc.release_status = doc.release_status
      dbdoc.originator = doc.originator
      dbdoc.docdate = doc.docdate

```

which, again, looks uglier than I think it should.

Any suggestions?

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [April 15, 2008, 12:10am UTC](https://www.ruby-forum.com/t/ot-how-to-make-this-code-prettier/134754/4 "2008-04-15T00:10:09Z")

</div>

On Apr 14, 8:44 pm, “Patrick D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> dbdoc.originator = doc.originator
> dbdoc.docdate = doc.docdate
> 
> ```
> 
> which, again, looks uglier than I think it should.\>  
> Well if you know that the column/field names will always match up you  
> can use the fact that struct mixes in enumerable, so for example you  
> can do  
> doc.each\_pair do |name, value|  
> dbdoc.send(name.to\_s + ‘=’, value)  
> end  
> You could also use that to build up a hash that you could give to  
> Document.new

Fred

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [April 15, 2008, 12:38am UTC](https://www.ruby-forum.com/t/ot-how-to-make-this-code-prettier/134754/5 "2008-04-15T00:38:51Z")

</div>

> > which, again, looks uglier than I think it should.\>  
> > Well if you know that the column/field names will always match up you  
> > I can (and have) make that to be the case

> can use the fact that struct mixes in enumerable, so for example you  
> can do  
> doc.each\_pair do |name, value|  
> dbdoc.send(name.to\_s + ‘=’, value)  
> end  
> That’s clever. But is it more readable? If it is idiomatic Ruby,  
> then I would say “yes it is more readable”. Being rather new to Ruby  
> and not having read any production Ruby code, I don’t know if that’s  
> idiomatic or not.

> You could also use that to build up a hash that you could give to  
> Document.new  
> That’s what I tried originally:

dbdoc = Document.new(doc.hash)

but that threw an exception:  
…/activerecord-2.0.2/lib/active\_record/base.rb:2110:in `dup’: can’t  
dup Fixnum (TypeError)

–wpd

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [April 15, 2008, 12:57am UTC](https://www.ruby-forum.com/t/ot-how-to-make-this-code-prettier/134754/6 "2008-04-15T00:57:34Z")

</div>

On Apr 14, 11:38 pm, “Patrick D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That’s clever. But is it more readable? If it is idiomatic Ruby,  
> then I would say “yes it is more readable”. Being rather new to Ruby  
> and not having read any production Ruby code, I don’t know if that’s  
> idiomatic or not.  
> It’s either readable or not, make your own mind up about it 🙂 It  
> doesn’t make a lot of sense to have someone else tell you if something  
> is readable: if you find it readable then that should be good enough  
> for you. slightly cuter is doc.each\_pair {|name, value|  
> dbdoc[name]=value} but it’s not quite the same things (since it just  
> writes the attribute rather than calling any custom accessor you may  
> have created)

> dup Fixnum (TypeError)

That’s not what hash does. hash returns an integer (such that a.hash !  
= b.hash =\> a != b) which is used when storing things like that in a  
hash. If you want a hash you need to iterate over the struct and build  
it typ.

Fred
