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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:57:49+00:00 2026-05-21T11:57:49+00:00

A parameter defaulted to an empty hash attrs={} returns an error: can’t convert Array

  • 0

A parameter defaulted to an empty hash attrs={} returns an error:

can't convert Array into Hash
(TypeError)

I’ve tried this on Ruby versions 1.8.6, 1.8.7 and 1.9.1. A hash will be passed to attrs.

class Category < Object   

  attr_accessor :attributes  
  attr_accessor :rel_attrs  
  attr_accessor :cls_str

  def initialize (term='',title='', attrs={}, scheme = '', rel=[], cls_str='')

    @attributes ={}
    @attributes['scheme']  = scheme
    @attributes['term']    = term
    @attributes['title']   = title
    @attributes['related'] = rel
    @cls_str = cls_str

    if not attrs.empty?
       @attributes.update attrs
    end
  end
end

What am I doing wrong?

  • 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-21T11:57:50+00:00Added an answer on May 21, 2026 at 11:57 am

    Some notes:

    • You don’t have to inherit from Object.
    • if not can more idiomatically be expressed as unless.
    • Making the attrs hash the last argument has the advantage that you can leave off the {} around the hash elements when calling Category.new. You can not do this if the hash is the middle, so it would make sense to show us your call to Category.new.

    I changed your code accordingly:

    class Category 
    
      attr_accessor :attributes
      attr_accessor :rel_attrs  
      attr_accessor :cls_str
    
      def initialize (term='',title='', scheme = '', rel=[], cls_str='', attrs={})
    
        @attributes ={}
        @attributes['scheme']  = scheme
        @attributes['term']    = term
        @attributes['title']   = title
        @attributes['related'] = rel
        @cls_str = cls_str
    
        @attributes.update(attrs) unless attrs.empty?
      end
    end
    

    Here’s how you call it:

    >> c = Category.new("term", "title", "scheme", [1,2,3], 'cls_string', :foo => 'bar', :baz => 'qux') 
    #=> #<Category:0x00000100b7bff0 @attributes={"scheme"=>"scheme", "term"=>"term", "title"=>"title", "related"=>[1, 2, 3], :foo=>"bar", :baz=>"qux"}, cls_str"cls_string"
    >> c.attributes 
    #=> {"scheme"=>"scheme", "term"=>"term", "title"=>"title", "related"=>[1, 2, 3], :foo=>"bar", :baz=>"qux"}
    

    This obviously has the problem that all other optional arguments have to be specified in order to be able to specify attrs. If you don’t want this, move attrs back to the middle of the argument list and make sure to include the {} in the call. Or even better, make the whole argument list a hash and merge it with the defaults args. Something like

     class Category(opts = {})
       # stuff skipped
       @options = { :bla => 'foo', :bar => 'baz'}.merge(opts)
       # more stuff skipped
     end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sometimes it seems natural to have a default parameter which is an empty list.
I was reading this today: http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#default-parameter-values and I can't seem to understand what's happening
This code is not valid: private void Foo(string optionalString = string.Empty) { // do
Your basic SP with a default parameter: ALTER PROCEDURE [usp_debug_fails] @DATA_DT_ID AS int =
Is there a way to have a default parameter passed to a action in
I know of two methods of setting a default parameter, but I would like
In Crystal reports, you can define default values for the report parameters. For example,
The only SET parameter that I have found specific guidance for is SET TRANSACTION
[NOTE: I'm using ASP.NET MVC2 RC2.] I have URLs like this: /customers/123/orders/456/items/index /customers/123/orders/456/items/789/edit My
problem description: This model works fine with one user at a time. As soon

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.