Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7570835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:31:06+00:00 2026-05-30T15:31:06+00:00

So I am figuring out how to set up some options for a class.

  • 0

So I am figuring out how to set up some options for a class. ‘options’ is a hash. I want to

1) filter out options I don’t want or need

2) set some instance variables to use elsewhere

3) and set up another hash with the processed options as @current_options.

def initialize_options(options)
  @whitelisted_options, @current_options  = [:timestamps_offset, :destructive, :minimal_author], {}
  n_options = options.select { |k,v| @whitelisted_options.include?(k) }
  @current_options[:timestamps_offset] = @timestamp_offset = n_options.fetch(:timestamps_offset, 0)*(60*60*24)
  @current_options[:destructive] = @destructive = n_options.fetch(:destructive, false)
  @current_options[:minimal_author] = @minimal_author = n_options.fetch(:minimal_author, false)
end

I’m guessing this is a bit much, no matter what I pass in I get:

{:timestamps_offset=>0, :destructive=>false, :minimal_author=>false}

When I do this line by line from the command line, it works as I want it to but not in my class. So what is going on and how do I clean this up?

EDIT: this actually works disembodied from the class I’m using it in, but inside it doesn’t so the reality is something is going on I’m not aware of right now.

attr_reader :current_options is how this is set on the class, perhaps that needs some revision.

EDIT2: line 2 of the method is supposed to select from @whitelisted_options

EDIT3: Actually turned out to be something I wasn’t thinking of…”options” comes in parsed from a yaml file as strings….and I was fetching symbols, changing that around makes a difference where before the method was looking for symbols and finding none, e.g. “destructive” vs :destructive, so always defaulting to the defaults. In short, I just needed to symbolize the hash keys when options are imported.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:31:07+00:00Added an answer on May 30, 2026 at 3:31 pm

    Your @current_options is initialized as an empty hash. When you filter the options passed as params, none of the keys will be present in @current_options so n_options will end up empty.

    Then when you set up @current_options in the following lines, it will always grab the default values (0, false, false), and that’s why your output’s always the same.

    You solve this problem by conditionally initializing @current_options so that it’s only set to {} once:

    @current_options ||= {}

    Post-OP edit:

    Your issue’s with options.select — in Ruby 1.8, it doesn’t return a Hash, but rather an Array. Your calls to fetch are then always failing (as symbols can’t be array indexes), so always returning defaults.

    Instead, try:

    n_options = options.inject({}) {|h, p| h[p[0]] = p[1] if @whitelisted_options.include? p[0]; h } 
    

    where p is an array containing each key/value pair.

    In Ruby 1.9.2, Hash.select behaves the way you expected it to.

    Edit 2: Here’s how I’d approach it:

    class Foo
      @@whitelisted_options= {:timestamps_offset => 0, :destructive => false, :minimal_author =>false}
    
      @@whitelisted_options.keys.each do |option|
        define_method(option) { return @current_options[option] rescue nil}
      end
    
      def initialize_options(options)
        @current_options = {}
        @@whitelisted_options.each {|k, v| @current_options[k] = options[k] || v}
        @current_options
      end
    end
    

    In use:

    f = Foo.new
    f.destructive #=> nil
    f.initialize_options(:minimal_author => true, :ignore => :lol)
    f.destructive #=> false
    f.minimal_author #=> true
    f.timestamps_offset #=> 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some php help in figuring out how use this array I created, but
Need some guidance figuring out what went wrong. I've been using mysql, phpmyadmin for
I'm having trouble figuring out how to get the testing framework set up and
I'm having trouble figuring out how to delete a set of records when a
I am stuck figuring out a working SQL Query for the fallowing: I need
I have a 5 byte data element and I need some help in figuring
I'm rather new to Ruby, and so far, figuring out how to use binding
I am having some difficulty figuring out how to template the following TreeView item
Having some trouble figuring out why my ImageView buttons stop working after I change
I'm having trouble figuring out the right way to set this association up. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.