Forum: Ruby Loading up a hash with variables if they are defined

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ed437e52d8d6720308720e7e678f3e6d?d=identicon&s=25 Patrick Doyle (Guest)
on 2008-12-23 18:00
(Received via mailing list)
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
53581739a445ad78250a676dabddf55f?d=identicon&s=25 James Coglan (Guest)
on 2008-12-23 18:07
(Received via mailing list)
>  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
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 Jesús Gabriel y Galán (Guest)
on 2008-12-23 18:14
(Received via mailing list)
On Tue, Dec 23, 2008 at 5:51 PM, Patrick Doyle <wpdster@gmail.com>
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.
Ed437e52d8d6720308720e7e678f3e6d?d=identicon&s=25 Patrick Doyle (Guest)
on 2008-12-23 18:23
(Received via mailing list)
On Tue, Dec 23, 2008 at 11:57 AM, James Coglan
<jcoglan@googlemail.com>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
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2008-12-23 18:35
(Received via mailing list)
Patrick Doyle 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}
This topic is locked and can not be replied to.