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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:49:10+00:00 2026-05-30T20:49:10+00:00

I’m just startin to learn ruby and I’m writing a simple program, but I’ve

  • 0

I’m just startin to learn ruby and I’m writing a simple program, but I’ve got an error undefined method 'send_for_beer' for Person:Class (NoMethodError)
Here is a code:

class Person
    @iq = 0
    @speed = 0
    @power = 0
    @beauty = 0
    def initialize (iq, speed, power, beauty)
        @iq = iq
        @speed = speed
        @power = power
    end

    def send_for_beer
        result @iq * 2 + @speed * 10 + @power * 5 + @beauty
        return result
    end
end

number_of_people = 3
person_array = Array.new(number_of_people, Person)
n = 0
beer_person = 0
beer_cof = 0
number_of_people.times do 

    ............
    person_array.push(Person.new(iq, speed, power, beauty))

    if person_array[n].send_for_beer > beer_cof     <-----here is an error
        beer_cof = person_array[n].send_for_beer
        beer_person = n
    end
    n = n+1
end
  • 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-30T20:49:11+00:00Added an answer on May 30, 2026 at 8:49 pm

    Here’s your problem:

    person_array = Array.new(number_of_people, Person)
    

    In short, don’t make array like this. Use the [] literal syntax. What this returns is:

    [Person, Person, Person]
    

    That is 3 references to the Person class, not instances. Then later you do:

    person_array.push(Person.new(iq, speed, power, beauty))
    

    And you end up with:

    [Person, Person, Person, person_instance]
    

    So when you iterate through and call send_for_beer on that first item, it does have that method because send_for_beer is an instance method that you are calling erroneously on a class object.

    The fix here is to simply assign person_array to an empty array literal, and then push things to it.

    person_array = []
    

    And a minor style note: << is usually preferred to Array#push, making the filling of the array look more like this.

    person_array << Person.new(iq, speed, power, beauty)
    

    Ruby also support implicit return of the last expression in a method. So you do not need to return result. Instead, simply calulate the return value as the only line in the method.

    def send_for_beer
      @iq * 2 + @speed * 10 + @power * 5 + @beauty
    end
    

    Instance variables don’t quite work like that either. When you have @name in the class body directly, you are not initializing instance variables for each instance. You are actually setting instance variable on the class object (which is weird, I know). What you actually need to do is set them from any instance method, typically initialize, which you are doing here. So you can totally remove the instance variable setting at the class level here.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

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.