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

  • Home
  • SEARCH
  • 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 8892003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:53:14+00:00 2026-06-14T22:53:14+00:00

The variable value in the initialize method of LocationList is populated in line 014.

  • 0

The variable value in the initialize method of LocationList is populated in line 014. These changes are relected by the print statement in line 015, but the return in line 016 thinks the hash is still empty (scroll right to see return value after =>).

def random_point
  x = rand * 2.0 - 1.0
  y = rand * 2.0 - 1.0
  until x**2 + y**2 < 1.0
    x = rand * 2.0 - 1.0
    y = rand * 2.0 - 1.0
  end
  return [x, y]
end

class LocationList < Hash
  def initialize(node_list)
    value = {}
    node_list.each {|node| value[node] = random_point }
    print value
    return value
  end
end

z = ["moo", "goo", "gai", "pan"]

LocationList.new(z)
#=> {"moo"=>[0.17733298257484997, 0.39221824315332987], "goo"=>[-0.907202436634851, 0.3589265999520428], "gai"=>[0.3910479677151635, 0.5624531973759821], "pan"=>[-0.37544369339427974, -0.7603500269538608]}=> {}

Doing substantially the same thing in a global function yields the intended return value:

def foo(node_list)
  value = {}
  node_list.each {|node| value[node] = random_point }
  return value
end

foo(z)
#=> {"moo"=>[-0.33410735869573926, -0.4087709899603238], "goo"=>[0.6093966465651919, 0.6349767372996336], "gai"=>[0.718925625951371, -0.6726652512124924], "pan"=>[0.08604969147566277, -0.518636160280254]}
  • 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-14T22:53:15+00:00Added an answer on June 14, 2026 at 10:53 pm

    You’re creating a new Hash that you call value in your initialize method, rather than initializing self. Illustrating this inline:

    class LocationList < Hash
      def initialize(node_list)
        # self is already a LocationList, which is a Hash
    
        value={}
        # value is now a new Hash
    
        node_list.each {|node| value[node]=random_point}
        # value now has keys set
    
        return value
        # value is now discarded
        # LocationList.new returns the constructed object; it does not return
        # the result of LocationList#initialize
      end
    end
    

    Try this instead:

    class LocationList < Hash
      def initialize(node_list)
        node_list.each {|node| self[node]=random_point}
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

You initialize an int variable defined within a method to have a value of
Is it possible to assign a value to an instance variable during an initialize
If I try to assign a value to a variable in a class, but
Can I create a Bash script with persistent variable-values? For example, I initialize a
I have to initialize a script variable in a for loop with different values
I want to use a variable value in exec where i don't need to
How can I pass a variable value from a template using JavaScript without using
I want to change the variable value which is a member of a structure
i have a loop where a variable value will change in each loop and
Possible Duplicate: How to get javascript variable value in php I am looking for

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.