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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:14:40+00:00 2026-06-15T21:14:40+00:00

In Ruby 1.9.3 I need to create a few class instances which each have

  • 0

In Ruby 1.9.3 I need to create a few class instances which each have similar instance- and class-methods but which vary only by a few fixed parameters. The distinction of their class type is also important so I cannot simply use separate instances of the same class.

A simplified example looks like this.

module Animal
private
  def self.make_animal(name, legs, noise)
    klass = Class.new
    klass.const_set(:NUM_LEGS, legs)
    klass.class.send(:define_method, :scream) { noise.upcase + '!' }
    Animal.const_set(name, klass)
  end
  make_animal :Tiger, 4, 'roar'
  make_animal :Human, 2, 'derp'
end

This seems to work fine except that the variables used in the block which dynamically defines the “scream” method are bound at runtime of the “scream” method instead of runtime of the “make_animal” method.

Animal::Human::NUM_LEGS # => 2 -- ok
Animal::Tiger::NUM_LEGS # => 4 -- ok
Animal::Human.scream # => "DERP!" -- ok
Animal::Tiger.scream # => "DERP!" -- fail!

How can I modify the above code so that the Tiger screams "ROAR!"?

[Note] I really do need to maintain the goofy OO structure in the example for reasons that are too involved to describe here. I’m interested only in learning how to programmatically define class methods on dynamically defined classes with parameterized method implementations.

  • 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-15T21:14:42+00:00Added an answer on June 15, 2026 at 9:14 pm

    klass.class is the same in both cases (Class): all classes are instances of Class. As a result you’re defining scream and then redefining it.

    What are often thought of as class methods in ruby are actually singleton methods (there’s lots of stuff to read about eigenclasses etc if you are interested).

    The

    def some_object.foo
    end
    

    Construct creates singleton methods. Very often this will be inside a class definition, using self but you can do it on anything, for example if you do

    x = 'dog'
    def x.bark
      "Woof"
    end
    

    Then x.bark will return woof, but bark won’t be defined on any other string.

    Here your method needs to reference your noise variable, so you’ll need to use define_singleton_method to define your method.

    If you’re still in ruby 1.8 you can’t use define_singleton_method – you need to use the fact that singleton methods are methods on the eigenclass.

    klass = Class.new
    eigenclass = class << klass; self; end
    eigenclass.send(:define_method, :scream){noise}
    

    Is equivalent to using define_singleton_method

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

Sidebar

Related Questions

On my view, I have 2 URLs I need to create. Each has a
I need to create a class with some properties and methods for my application
For my Ruby on Rails application, I need the possibility to create several LectureNote
I have developed a simple library in Ruby and need to use this in
I read about many CSS related languages and tools which need ruby. What is
I need to write a little ruby function that does word wrapping. I have
I have an Ruby on Rails 3 Heroku application, which needs to perform text
I need to create a class that represent SVN inside a module called SCM.
At the moment I have a Ruby on Rails application which maintains my Users,
I need to share a database handle among a few Ruby scripts. I am

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.