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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:08:00+00:00 2026-06-14T08:08:00+00:00

So here is a more java-esq approach (I think)…I have this code in Ruby

  • 0

So here is a more java-esq approach (I think)…I have this code in Ruby

class Human
  def move
    p 'Human move: X'
  end
end


class Computer
  def move
    p'Computer move: O'
  end
end

class Player
  def initialize(letter)
    if letter == 'X'
      return Human.new
    else
      return Computer.new
    end
  end
end

bob = Player.new('X')
bob.move

I’m expecting to get back on cli ‘Human move: X’
I’m getting an error of the following…

undefined method `move' for #<Player:0x007f8d930895a8> (NoMethodError)

Can anybody help this Ruby noob?

————————-discussion————————

Now I’ve just figured a way of doing this in a more, I guess, ‘classic’ Ruby fashion using inheritance and overriding. Now, this just makes no sense to the sub processors in my mind.I mean, if I do it this way…I could have just required the class files in any other class file and instantiated a new object their. It does not feel truly polymorphic the way it does when you do it in Java….here’s the code….

class Player
  def move
    return ""
  end
end

class Human < Player
  def move
    return 'Human move: X'
  end
end


class Computer < Player
  def move
    return'Computer move: O'
  end
end



players = [Human.new, Computer.new]
players.each {|player|
  print player.move
}
  • 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-14T08:08:01+00:00Added an answer on June 14, 2026 at 8:08 am

    You almost made it 🙂 In ruby you can’t return an arbitrary object from a constructor. Well, you can, but that return value is ignored. Instead use a Factory Method (in OOP speak).

    class Human
      def move
        p 'Human move: X'
      end
    end
    
    
    class Computer
      def move
        p 'Computer move: O'
      end
    end
    
    class Player
      def self.get_player(letter)
        if letter == 'X'
          return Human.new
        else
          return Computer.new
        end
      end
    end
    
    bob = Player.get_player('X')
    bob.move
    # >> "Human move: X"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've decided to reopen this because I want to here more peoples views on
Here one more basic question asked in MS interview recently class A { public
I have a dynamically inflated layout. <TableLayout> <!-- Two more tablelayout here --> <ScrollView>
Here is my code: $(document).ready(function(){ //These variables are for loading more posts var create_ptr
Its more of a best practices sort of question. Here it goes... I have
Here's (i think) an interesting question. With AJAX more and more common i feel
Little bit of background, I'm more experienced with Java, and have some C/C++ experience.
It's the first time I use java Rmi*. I have a custom class which
I'm interested in becoming more fluent in Java so I have been going through
I have two (or more) Java Threads creating, updating and deleting entities from a

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.