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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:14:54+00:00 2026-06-13T13:14:54+00:00

I’m creating a object of hash in order to write a little script that

  • 0

I’m creating a object of hash in order to write a little script that reads in a file a line at a time, and assigns arrays into my hash class. I get wildly different results depending if I subclass Hash or not, plus using super changes things which I don’t’ understand.

My main issue is that without subclassing hash ( < Hash) it works perfectly, but I get no methods of Hash (like to iterate over the keys and get things out of it…. Subclassing Hash lets me do those things, but it seems that only the last element of the hashed arrays is ever stored…. so any insight into how you get the methods of a subclass. The Dictionary class is a great example I found on this site, and does exactly what I want, so I’m trying to understand how to use it properly.

filename = 'inputfile.txt.'

# ??? class Dictionary < Hash
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]
#    super(key,words)
  end
end


listData = Dictionary.new

File.open(filename, 'r').each_line do |line|
  line = line.strip.split(/[^[:alpha:]|@|\.]/)
  puts "LIST-> #{line[0]}  SUB->  #{line[1]}  "
  listData[line[0]] = ("#{line[1]}")  
end

puts '====================================='
puts listData.inspect
puts '====================================='
print listData.reduce('') {|s, (k, v)|
  s << "The key is #{k} and the value is #{v}.\n"
}

If anyone understands what is going on here subclassing hash, and has some pointers, that would be excellent.

Running without explicit < Hash:

./list.rb:34:in `<main>': undefined method `reduce' for #<Dictionary:0x007fcf0a8879e0> (NoMethodError)

That is the typical error I see when I try and iterate in any way over my hash.

Here is a sample input file:

listA   billg@microsoft.com
listA   ed@apple.com
listA   frank@lotus.com
listB   evanwhite@go.com
listB   joespink@go.com
listB   fredgrey@stop.com
  • 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-06-13T13:14:55+00:00Added an answer on June 13, 2026 at 1:14 pm

    I can’t reproduce your problem using your code:

    d = Dictionary.new               #=> #<Dictionary:0x007f903a1adef8 @data={}>
    d[4] << 5                        #=> [5]
    d[5] << 6                        #=> [6]
    d                                #=> #<Dictionary:0x007f903a1adef8 @data={4=>[5], 5=>[6]}>
    d.instance_variable_get(:@data)  #=> {4=>[5], 5=>[6]}
    

    But of course you won’t get reduce if you don’t subclass or include a class/module that defines it, or define it yourself!

    The way you have implemented Dictionary is bound to have problems. You should call super instead of reimplementing wherever possible. For example, simply this works:

    class Dictionary < Hash
      def initialize
        super { |hash, key| hash[key] = [] }
      end
    end
    
    d = Dictionary.new  #=> {}
    d['answer'] << 42   #=> [42]
    d['pi'] << 3.14     #=> [3.14
    d                   #=> {"answer"=>[42], "pi"=>[3.14]}
    

    If you want to reimplement how and where the internal hash is stored (i.e., using @data), you’d have to reimplement at least each (since that is what almost all Enumerable methods call to) and getters/setters. Not worth the effort when you can just change one method instead.

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

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.