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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:45:51+00:00 2026-05-14T15:45:51+00:00

I want to add elements to my Hash lists, which can have more than

  • 0

I want to add elements to my Hash lists, which can have more than one value. Here is my code. I don’t know how I can solve it!

class dictionary

  def initialize(publisher)             
    @publisher=publisher
    @list=Hash.new()                    
  end

  def []=(key,value)
    @list << key unless @list.has_key?(key)
    @list[key] = value
  end

end


dic = Dictionary.new

dic["tall"] = ["long", "word-2", "word-3"]

p dic

Many thanks in advance.

regards,

koko

  • 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-14T15:45:51+00:00Added an answer on May 14, 2026 at 3:45 pm

    I think this is what you’re trying to do

    class Dictionary
      def initialize()
        @data = Hash.new { |hash, key| hash[key] = [] }
      end
      def [](key)
        @data[key]
      end
      def []=(key,words)
        @data[key] += [words].flatten
        @data[key].uniq!
      end
    end
    
    d = Dictionary.new
    d['tall'] = %w(long word1 word2)
    d['something'] = %w(anything foo bar)
    d['more'] = 'yes'
    
    puts d.inspect
    #=> #<Dictionary:0x42d33c @data={"tall"=>["long", "word1", "word2"], "something"=>["anything", "foo", "bar"], "more"=>["yes"]}>
    
    puts d['tall'].inspect
    #=> ["long", "word1", "word2"]
    

    Edit

    Now avoids duplicate values thanks to Array#uniq!.

    d = Dictionary.new
    d['foo'] = %w(bar baz bof)
    d['foo'] = %w(bar zim)     # bar will not be added twice!
    
    puts d.inspect
    #<Dictionary:0x42d48c @data={"foo"=>["bar", "baz", "bof", "zim"]}>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have a hash where the key is a 3digit code and value
I want to add some HTML elements that have the same class name. So,
Suppose I have an iterator, and I want to add some elements before or
I have some elements on the page. Here is example of one element: <div
I have an arraylist where I want to add elements via a for loop.
I have an XML configuration file to which I will occasionally add new elements
I want to add elements to arrays within an object that I access like
HI.. i want to add elements dynamically to listview in QT for symbian OS,
I want to add database elements into array. how do I do that. the
This is my array and I want to add some elements to make it

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.