# Loading up a hash with variables if they are defined

**URL:** <https://www.ruby-forum.com/t/loading-up-a-hash-with-variables-if-they-are-defined/156265>\
**Category:** Ruby\
**Created:** [December 23, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/loading-up-a-hash-with-variables-if-they-are-defined/156265 "2008-12-23T18:00:41Z")\
**Posts on this page:** 5\
**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:** [December 23, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/loading-up-a-hash-with-variables-if-they-are-defined/156265/1 "2008-12-23T18:00:41Z")

</div>

Is there a cleaner, more Rubyesque way to do this?

# Return a hash of the navigation parameters

def nav\_params  
params = {}

```
params[:prefix] = @prefix if @prefix
params[:category_id] = @category_id if @category_id
params[:page] = @page if @page

params

```

end

I want to load a parameters hash with the contents of some instance  
variables, but only if they are defined.

–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:** [December 23, 2008, 6:07pm UTC](https://www.ruby-forum.com/t/loading-up-a-hash-with-variables-if-they-are-defined/156265/2 "2008-12-23T18:07:07Z")

</div>

> end
> 
> I want to load a parameters hash with the contents of some instance  
> variables, but only if they are defined.

params = [:prefix, :category\_id, :page].inject({}) do |hash, name|  
var = instance\_variable\_get("@#{name}")  
hash[name] = var if var  
hash  
end

---

<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:** [December 23, 2008, 6:23pm UTC](https://www.ruby-forum.com/t/loading-up-a-hash-with-variables-if-they-are-defined/156265/3 "2008-12-23T18:23:33Z")

</div>

On Tue, Dec 23, 2008 at 11:57 AM, James C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > end  
> > end

Thanks… that’s certainly along the lines of what I was searching for.  
I’ll go read about #instance\_variable\_get now.

–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:** [December 23, 2008, 6:14pm UTC](https://www.ruby-forum.com/t/loading-up-a-hash-with-variables-if-they-are-defined/156265/4 "2008-12-23T18:14:10Z")

</div>

On Tue, Dec 23, 2008 at 5:51 PM, Patrick D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> params  
> end
> 
> I want to load a parameters hash with the contents of some instance  
> variables, but only if they are defined.

irb(main):005:0\> {:prefix =\> @prefix, :category\_id =\> @category, :page  
=\> @page}.reject {|k,v| v.nil?}  
=\> {:prefix=\>“asdasdf”, :category\_id=\>“cat”}

Jesus.

---

<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:** [December 23, 2008, 6:35pm UTC](https://www.ruby-forum.com/t/loading-up-a-hash-with-variables-if-they-are-defined/156265/5 "2008-12-23T18:35:16Z")

</div>

Patrick D. wrote:

> ```
> params
> 
> ```
> 
> end
> 
> I want to load a parameters hash with the contents of some instance  
> variables, but only if they are defined.

Watch out: “only if they are defined” is not the same as “only if they  
are not nil or false”. If you want the former, then this might be what  
you are looking for:

class C  
NAV\_VARS = %w{ @prefix @category\_id @page }  
NAV\_VAR\_KEYS = NAV\_VARS.inject({}) {|h,v| h[v] =  
v.delete("@").to\_sym; h}

def nav\_params  
(instance\_variables & NAV\_VARS).inject({}) do |params,var|  
params[NAV\_VAR\_KEYS[var]] = instance\_variable\_get(var)  
params  
end  
end  
end

c = C.new  
c.instance\_eval do  
@prefix = “foo”  
@page = false  
end

p c.nav\_params # ==\> {:prefix=\>“foo”, :page=\>false}
