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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:05:07+00:00 2026-05-28T19:05:07+00:00

So I have this ruby code I grabbed from wikipedia and I modified a

  • 0

So I have this ruby code I grabbed from wikipedia and I modified a bit:

@trie = Hash.new()
def build(str)
    node = @trie
    str.each_char { |ch|
      cur = ch
      prev_node = node
      node = node[cur]
      if node == nil
        prev_node[cur] = Hash.new()
        node = prev_node[cur]
      end
    }
  end

build('dogs')

puts @trie.inspect

I first ran this on console irb, and each time I output node, it just keeps giving me an empty hash each time {}, but when I actually invoke that function build with parameter 'dogs' string, it actually does work, and outputs {"d"=>{"o"=>{"g"=>{"s"=>{}}}}}, which is totally correct.

This is probably more of a Ruby question than the actual question about how the algorithm works. I don’t really have adequate Ruby knowledge to decipher what is going on there I guess.

  • 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-28T19:05:08+00:00Added an answer on May 28, 2026 at 7:05 pm

    You’re probably getting lost inside that mess of code which takes an approach that seems a better fit for C++ than for Ruby. Here’s the same thing in a more concise format that uses a special case Hash for storage:

    class Trie < Hash
      def initialize
        # Ensure that this is not a special Hash by disallowing
        # initialization options.
        super
      end
    
      def build(string)
        string.chars.inject(self) do |h, char|
          h[char] ||= { }
        end
      end
    end
    

    It works exactly the same but doesn’t have nearly the same mess with pointers and such:

    trie = Trie.new
    trie.build('dogs')
    puts trie.inspect
    

    Ruby’s Enumerable module is full of amazingly useful methods like inject which is precisely what you want for a situation like this.

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

Sidebar

Related Questions

I have ruby code like this: module Hello class Hi def initialize() puts self.module.name
I'm relatively new to Ruby on Rails. As of now have this working code
I have a bit of ruby code: def createCal(cal) mod = @on + @off
I have the following code in Ruby. I want to convert this code into
Let's say I have a hash in Ruby like this: d = {1 =>
I'm new on both this site and ruby on rails! I have a common
This is a Ruby code: if (@user.isAdmin?) @admin_profile = AdminProfile.new @user.admin_profile = @admin_profile @admin_profile.save
I have this trial timer code to time euler solutions in Ruby. $RUNS =
I have seen many samples of Ruby code with this line (for example, http://www.sinatrarb.com/
I have this syntax in my code, but it displays differently: <a href=http://railstutorial.org/>Ruby on

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.