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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:21:21+00:00 2026-05-26T23:21:21+00:00

I seem to be having problems with using a hash parameter to my constructor

  • 0

I seem to be having problems with using a hash parameter to my constructor in Ruby 1.9.2. I note that what I’m trying to do works in 1.8.7. Here is my example code:

def initialize(*params)
    @attr1 = params[:attr1] or nil
    @attr2 = params[:attr2] or nil
end

However, when I try to instantiate an object of this example class, I get an error message on the line where the first instance variable is set: in '[]': can't convert Symbol into Integer (TypeError)

Why does this not work in 1.9.2? And how do I work around it?

  • 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-26T23:21:22+00:00Added an answer on May 26, 2026 at 11:21 pm

    You don’t need the splat (*) to capture a single hash argument. It’s used to capture an unknown number of arguments. Change your function definition to

    def initialize(params = {})
        @attr1 = params[:attr1] or nil
        @attr2 = params[:attr2] or nil
    end
    

    and everything should work the way you’d expect. Edit: The params = {} makes the params argument optional, and sets it to an empty hash when nothing is provided.

    What’s actually being captured in params with the function definion you have now, would be like this:

    Whatever.new(:foo => 'foo', :bar => 'bar')
    # params contains [{:foo => 'foo', :bar => 'bar'}]
    

    so you’d need to reach into the array first to get the hash, then use the hash keys.

    When Ruby sees a set of hash key/value pairs as the last argument to a function, it’s automatically wrapped into a Hash. So even though it looks like you’re supplying multiple arguments to the function, the interpreter is actually only receiving a single argument.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I seem to be having problems with my code and wondered if you guys
I am having problems trying to use the regular expression that I used in
I have the following code, I seem to be having problems with booleans in
We are using Replication and seem to be having endless problems with it. It
I'm having some problems with hashes in Chrome (and Safari). I'm using the hash
I am using appengine and seem to be having some problems with url routing
Okay, I seem to be having a problem. I'm trying to create a twicker
I'm having a problem with my Seam code and I can't seem to figure
Seem to be having an issue with std::auto_ptr and assignment, such that the object
Hi I seem to be having an issue trying to align an H1 tag

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.