# Better way to do this?

**URL:** <https://www.ruby-forum.com/t/better-way-to-do-this/160204>\
**Category:** Ruby\
**Created:** [February 12, 2009, 11:26pm UTC](https://www.ruby-forum.com/t/better-way-to-do-this/160204 "2009-02-12T23:26:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [February 12, 2009, 11:26pm UTC](https://www.ruby-forum.com/t/better-way-to-do-this/160204/1 "2009-02-12T23:26:22Z")

</div>

Hi,

I seem to be repeating myself in the following little bit:

if (!@options[:namespace].nil?)  
namespace\_options = @options[:namespace]  
root.add\_namespace(namespace\_options[:namespace],  
namespace\_options[:value])  
end

Can someone show me a way to not repeat options[:namespace] ?

Simpler example of the exact same:

if (!hash[:value].nil?)  
h = hash[:value]  
puts h[:left], h[:right]  
end

Thanks for any tips for writing more idiomatic ruby!

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [February 12, 2009, 11:47pm UTC](https://www.ruby-forum.com/t/better-way-to-do-this/160204/2 "2009-02-12T23:47:39Z")

</div>

Pito S. wrote:

> Can someone show me a way to not repeat options[:namespace] ?
> 
> Simpler example of the exact same:
> 
> if (!hash[:value].nil?)  
> h = hash[:value]  
> puts h[:left], h[:right]  
> end

hash = {  
:value =\> {:left =\> 1, :right =\> 2}  
}

# original:

if (!hash[:value].nil?)  
h = hash[:value]  
puts h[:left], h[:right]  
end

# looking up :value only once

h = hash[:value]  
if h ## assuming you don’t use false as a value in the hash  
puts h[:left], h[:right]  
end

# living a little dangerously (assignment in conditional):

if (h = hash[:value])  
puts h[:left], h[:right]  
end

# more compactly, if you care:

h = hash[:value] and puts h[:left], h[:right]

# the last one depends on ‘and’ having low precedence (unlike &&)

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [February 12, 2009, 11:48pm UTC](https://www.ruby-forum.com/t/better-way-to-do-this/160204/3 "2009-02-12T23:48:50Z")

</div>

Hi,

Am Freitag, 13. Feb 2009, 07:24:59 +0900 schrieb Pito S.:

> if (!hash[:value].nil?)  
> h = hash[:value]  
> puts h[:left], h[:right]  
> end

Untested:

if (!(h = hash[:value]).nil?)  
puts h[:left], h[:right]  
end

or

h = hash[:value]  
unless h.nil? then  
puts h[:left], h[:right]  
end

or

h = hash[:value]  
h.nil? or puts h[:left], h[:right]

Hope you enjoyed the trip.

Bertram

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [February 12, 2009, 11:50pm UTC](https://www.ruby-forum.com/t/better-way-to-do-this/160204/4 "2009-02-12T23:50:01Z")

</div>

On Feb 12, 2009, at 3:24 PM, Pito S. wrote:

> Can someone show me a way to not repeat options[:namespace] ?
> 
> Simpler example of the exact same:
> 
> if (!hash[:value].nil?)  
> h = hash[:value]  
> puts h[:left], h[:right]  
> end

if (h = hash[:value])  
puts h[:left], h[:right]  
end

Blessings,  
TwP
