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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:02:35+00:00 2026-05-30T19:02:35+00:00

If I call the function anagrams below in irb , I get a non-empty

  • 0

If I call the function anagrams below in irb, I get a non-empty hash container as expected. But if you comment out the print "No Key\n" line, the returned hash container is now empty. In fact for all elements in list the code in the elsif branch seems to execute. Either I’m going nuts or there is a nasty bug here:

def anagrams(list = ['cars', 'for', 'potatoes', 'racs', 'four','scar', 'creams', 'scream'])
        aHash = Hash.new()
        list.each { |el|
            aKey = el.downcase.chars.sort.to_a.hash
            if aHash.key?(aKey)
                # print "Has Key\n"
                aHash[aKey] << el
            elsif
                # print "No Key\n"
                aHash[aKey] = [el]
            end
        }

        return aHash
end

I have the following versions of ruby and irb installed:

ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]
irb 0.9.6(09/06/30)
  • 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-30T19:02:37+00:00Added an answer on May 30, 2026 at 7:02 pm

    Your problem is that you’re using an elsif where you mean else. This:

    elsif
        print "No Key\n"
        aHash[aKey] = [el]
    

    is misleading formatting, it is actually interpreted more like this:

    elsif(print "No Key\n")
        aHash[aKey] = [el]
    

    but print returns nil so the logic is like this:

    elsif(nil)
        aHash[aKey] = [el]
    

    and nil is false in a boolean context so aHash[aKey] = [el] never occurs. If you remove the print then you end up with this:

    elsif(aHash[aKey] = [el])
    

    and the assignment occurs; the assignment is also true in a boolean context (because an Array is) but the truthiness is irrelevant in this case.

    You want to use else here:

    if aHash.key?(aKey)
        aHash[aKey] << el
    else
        aHash[aKey] = [el]
    end
    

    Even better would be to use a Hash with an Array (via a block) as its default value:

    aHash = Hash.new { |h, k| h[k] = [ ] }
    

    and then you don’t need the if at all, you can just do this:

    list.each do |el|
        aKey = el.downcase.chars.sort.to_a.hash
        aHash[aKey] << el
    end
    

    And you can use anything as a key in a Ruby Hash so you don’t even need to .to_a.hash, you can simply use the Array itself as the key; furthermore, sort will give you an array so you don’t even need the to_a:

    list.each { |el| aHash[el.downcase.chars.sort] << el }
    

    Someone will probably complain about the return at the end of your method so I’ll do it: you don’t need the return at the end of your method, just say aHash and it will be the method’s return value:

    def anagrams(list = ['cars', 'for', 'potatoes', 'racs', 'four','scar', 'creams', 'scream'])
        aHash = Hash.new { |h, k| h[k] = [ ] }
        list.each { |el| aHash[el.downcase.chars.sort] << el }
        aHash
    end
    

    You could also use each_with_object to compress it even more:

    def anagrams(list = ['cars', 'for', 'potatoes', 'racs', 'four','scar', 'creams', 'scream'])
        list.each_with_object(Hash.new { |h, k| h[k] = [ ] }) do |el, h|
            h[el.downcase.chars.sort] << el
        end
    end
    

    but I’d probably do it like this to cut down on the noise:

    def anagrams(list = ['cars', 'for', 'potatoes', 'racs', 'four','scar', 'creams', 'scream'])
        h = Hash.new { |h, k| h[k] = [ ] }
        list.each_with_object(h) { |el, h| h[el.downcase.chars.sort] << el }
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this script below and somehow cannot call function from this anonymous one.
I want to call function using post inside another jquery post like below, <script
So I want to call function B in function A, but function B is
I want to call function when Ctrl + space pushed. I searched more but
i've got a simple ajax call: function message(){ $.ajax({ type: GET, url: /file/timestamp=+ timestamp,
It fires out when I try to call function with argument by reference function
I am trying to use the ExternalInterface.call function in my ActionScript(2.0) to get a
How can I get a class into a variable to call function and get
How do you call function lol() from outside the $(document).ready() for example: $(document).ready(function(){ function
I have encounters this abap snippet that I have difficulty to comprehend. call function

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.