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 4609494
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:59:40+00:00 2026-05-22T00:59:40+00:00

I have an initialize method that accepts a hash to set some instance variables.

  • 0

I have an initialize method that accepts a hash to set some instance variables. This is basically what I’m trying to do:

class Ad
  DEFAULT_PAGE = 'index'.freeze

  DEFAULT_CHANNEL = 'general'.freeze

  DEFAULT_AREA = 'general'.freeze

  attr_accessor :page, :area, :channel

  def initialize args={}
    @page = args[:page] || DEFAULT_PAGE
    @area = args[:area] || DEFAULT_AREA
    @channel = args[:channel] || DEFAULT_CHANNEL
  end

  # ...
end

I saw a tip to allow dynamic setting of instance variables, but I’m not sure how to also include the default values…

  def initialize args={}
    args.each do |attr,val|
      instance_variable_set("@#{attr}", val) unless val.nil?
    end
  end

Can I refer to a constant dynamically? Or any better ways of doing this sort of thing are welcome!

… I also realize that attr_accessor variables can be set individually. But I just want to do it like this. 🙂

  • 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-22T00:59:41+00:00Added an answer on May 22, 2026 at 12:59 am

    This one also only creates the instance variables if they are in your defaults hash, so that you don’t accidentally create/overwrite other instance variables.

    I’m assuming you meant to say unless val.nil?:

    def initialize(args={})
      defaults = {
        :page    => DEFAULT_PAGE,
        :area    => DEFAULT_AREA,
        :channel => DEFAULT_CHANNEL
      }.merge(args).each do |attr, val|
        instance_variable_set("@#{attr}", val) if defaults.has_key?(attr) && (not val.nil?)
      end if args
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that subclasses UITableViewCell. I need to initialize some of the
I have a method in my business logic layer that accepts a stream, which
I have a class that looks similar to this, and findbugz is complaining about
So in writing a C++ template class, I have defined a method that returns
I have a list string tag. I am trying to initialize a dictionary with
I have a block of code that basically intializes several classes, but they are
I just started playing in scala. I got a method that accepts string array
I was wondering if the following is possible. Create a class that accepts an
I have a UserAccountController that takes routes like this /{username}/{action} . I'd like to
So I have this class public static class MyClass { static MyClass() { ...

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.