# How To Avoid Ugly Declerations

**URL:** <https://www.ruby-forum.com/t/how-to-avoid-ugly-declerations/122774>\
**Category:** Ruby\
**Created:** [December 12, 2007, 1:15am UTC](https://www.ruby-forum.com/t/how-to-avoid-ugly-declerations/122774 "2007-12-12T01:15:47Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [December 12, 2007, 1:15am UTC](https://www.ruby-forum.com/t/how-to-avoid-ugly-declerations/122774/1 "2007-12-12T01:15:47Z")

</div>

Hello! More and more I find myself having to do something like this:

def some\_method(foo)  
results = []  
foo.each {|f| results \<\< f.reverse}  
return results  
end

As you can see, that is some pretty ugly code, but it’s also a common  
scenario. How can I make it a little less ugly?

Thanks,  
Michael B.

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [December 12, 2007, 1:36am UTC](https://www.ruby-forum.com/t/how-to-avoid-ugly-declerations/122774/2 "2007-12-12T01:36:50Z")

</div>

Michael B. wrote:

> def some\_method(foo)  
> results = []  
> foo.each {|f| results \<\< f.reverse}  
> return results  
> end
> 
> As you can see, that is some pretty ugly code, but it’s also a common  
> scenario. How can I make it a little less ugly?

def some\_method foo  
foo.map {|f| f.reverse}  
end

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [December 12, 2007, 2:49am UTC](https://www.ruby-forum.com/t/how-to-avoid-ugly-declerations/122774/3 "2007-12-12T02:49:30Z")

</div>

Suraj K. wrote:

> Michael B. wrote:
> 
> > def some\_method(foo)  
> > results = []  
> > foo.each {|f| results \<\< f.reverse}  
> > return results  
> > end
> > 
> > As you can see, that is some pretty ugly code, but it’s also a common  
> > scenario. How can I make it a little less ugly?
> 
> def some\_method foo  
> foo.map {|f| f.reverse}  
> end

Thanks for that, I’ll be using that more often now 🙂 However, I’m  
having trouble refactoring this code into something like yours…

def get\_text(\*elements)  
results = []  
elements.each {|element| results \<\< self.search(element).first}  
return results  
end

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [December 12, 2007, 11:32am UTC](https://www.ruby-forum.com/t/how-to-avoid-ugly-declerations/122774/4 "2007-12-12T11:32:00Z")

</div>

Michael B. wrote:

> Suraj K. wrote:  
> def get\_text(\*elements)  
> results = []  
> elements.each {|element| results \<\< self.search(element).first}  
> return results  
> end

def get\_text(\*elements)  
elements.map {|elem| self.search(elem).first }  
end

This should do what you want.

Greetings,  
Sascha

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [December 12, 2007, 6:52pm UTC](https://www.ruby-forum.com/t/how-to-avoid-ugly-declerations/122774/5 "2007-12-12T18:52:16Z")

</div>

def get\_text(\*elements)  
elements.map{|element| self.search(element).first}  
end

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [December 12, 2007, 7:09pm UTC](https://www.ruby-forum.com/t/how-to-avoid-ugly-declerations/122774/6 "2007-12-12T19:09:06Z")

</div>

On Dec 11, 6:15 pm, Michael B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## Thanks, Michael B.
> 
> Posted viahttp://www.ruby-forum.com/.

Map:

def some\_method(foo)  
foo.map {|f| f.reverse}  
end

or inject, but it’s less clear in this case:

def some\_method(foo)  
foo.inject([]) {|arr, f| arr \<\< f.reverse}  
end

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [December 12, 2007, 6:53pm UTC](https://www.ruby-forum.com/t/how-to-avoid-ugly-declerations/122774/7 "2007-12-12T18:53:28Z")

</div>

You don’t need to define a method for that.

foo.each { |word| word.reverse! }

should do the trick.

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [December 12, 2007, 7:25pm UTC](https://www.ruby-forum.com/t/how-to-avoid-ugly-declerations/122774/8 "2007-12-12T19:25:59Z")

</div>

On Dec 11, 6:49 pm, Michael B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> return results  
> end

def get\_text( \*elements )  
elements.map{ |el| search( element ).first }  
end

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [December 12, 2007, 7:26pm UTC](https://www.ruby-forum.com/t/how-to-avoid-ugly-declerations/122774/9 "2007-12-12T19:26:37Z")

</div>

On Dec 11, 2007 7:15 PM, Michael B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks,  
> Michael B.

If I understand your goal, that’s what map is for:

foo.map {|f| f.reverse}

“map” can also be spelled “collect”.

-A

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [December 12, 2007, 9:16pm UTC](https://www.ruby-forum.com/t/how-to-avoid-ugly-declerations/122774/10 "2007-12-12T21:16:27Z")

</div>

Thanks so much for all the replies 🙂 So, what I’ve gathered is that map  
basically loops through all the values of an array, does whatever the  
block says to do, and then places it back into the array. Correct?

- Michael B.

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [December 12, 2007, 7:26pm UTC](https://www.ruby-forum.com/t/how-to-avoid-ugly-declerations/122774/11 "2007-12-12T19:26:37Z")

</div>

Michael B. wrote:

> Thanks,  
> Michael B.

In this specific case…

def some\_method(foo)  
foo.map { |f| f.reverse }  
end

Use methods which return new objects and take advantage of implicit  
return values.

-Justin

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [December 12, 2007, 10:13pm UTC](https://www.ruby-forum.com/t/how-to-avoid-ugly-declerations/122774/12 "2007-12-12T22:13:22Z")

</div>

On Dec 12, 2007, at 3:16 PM, Michael B. wrote:

> Thanks so much for all the replies 🙂 So, what I’ve gathered is that  
> map  
> basically loops through all the values of an array, does whatever the  
> block says to do, and then places it back into the array. Correct?
> 
> - Michael B.

map creates a new array

If you really want to modify the array in place, you can use map!  
instead.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [December 14, 2007, 3:25am UTC](https://www.ruby-forum.com/t/how-to-avoid-ugly-declerations/122774/13 "2007-12-14T03:25:06Z")

</div>

On Thu, Dec 13, 2007 at 05:16:24AM +0900, Michael B. wrote:

> Thanks so much for all the replies 🙂 So, what I’ve gathered is that map  
> basically loops through all the values of an array, does whatever the  
> block says to do, and then places it back into the array. Correct?

If I understand your sentence correctly – no, that’s incorrect.

My understanding is that you expect #map or #collect to do this:

foo = %w(rgb irs rib)  
foo.map {|f| f.gsub(‘r’, ‘e’)}  
puts foo

> egb  
> ies  
> eib

What it actually does is this:

foo = %w(rgb irs rib)  
bar = foo.map {|f| f.gsub(‘r’, ‘e’)}  
puts foo

> rgb  
> irs  
> rib  
> puts bar  
> egb  
> ies  
> eib

Thus, the proposed method declaration:

def some\_method(foo)  
foo.map { |f| f.reverse }  
end

. . . would return the reversed list, exactly the same as a version with  
an explicit return statement. It does not alter the provided variable  
at  
all, but instead outputs a brand-new list object.
