Loading up a hash with variables if they are defined

Discussion in 'Ruby' started by Patrick Doyle, Dec 23, 2008.

  1. [Note: parts of this message were removed to make it a legal post.]

    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
     
    Patrick Doyle, Dec 23, 2008
    #1
    1. Advertising

  2. Patrick Doyle

    James Coglan Guest

    [Note: parts of this message were removed to make it a legal post.]

    >
    > # 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.




    params = [:prefix, :category_id, :page].inject({}) do |hash, name|
    var = instance_variable_get("@#{name}")
    hash[name] = var if var
    hash
    end
     
    James Coglan, Dec 23, 2008
    #2
    1. Advertising

  3. On Tue, Dec 23, 2008 at 5:51 PM, Patrick Doyle <> wrote:
    > 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.
    >



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

    Jesus.
     
    Jesús Gabriel y Galán, Dec 23, 2008
    #3
  4. [Note: parts of this message were removed to make it a legal post.]

    On Tue, Dec 23, 2008 at 11:57 AM, James Coglan <>wrote:

    > >
    > > # 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.

    >
    >
    >
    > params = [:prefix, :category_id, :page].inject({}) do |hash, name|
    > var = instance_variable_get("@#{name}")
    > hash[name] = var if var
    > hash
    > end
    >

    Thanks... that's certainly along the lines of what I was searching for.
    I'll go read about #instance_variable_get now.

    --wpd
     
    Patrick Doyle, Dec 23, 2008
    #4
  5. Patrick Doyle wrote:
    > 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.


    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}

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Dec 23, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Asfand Yar Qazi
    Replies:
    4
    Views:
    410
    Asfand Yar Qazi
    Nov 12, 2004
  2. Martin M.
    Replies:
    4
    Views:
    350
    Simon Brunning
    Dec 15, 2005
  3. Oodini
    Replies:
    1
    Views:
    1,795
    Keith Thompson
    Sep 27, 2005
  4. rp
    Replies:
    1
    Views:
    556
    red floyd
    Nov 10, 2011
  5. Ralf Baerwaldt
    Replies:
    1
    Views:
    140
    Paul Lalli
    Jul 20, 2004
Loading...

Share This Page