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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:03:29+00:00 2026-06-01T16:03:29+00:00

The following is a Ruby code snippet from Why’s Poignant Guide to Ruby Chapter

  • 0

The following is a Ruby code snippet from Why’s Poignant Guide to Ruby Chapter 6, where he attempts to demonstrate metaprogramming in Ruby:

# Get a metaclass for this class
def self.metaclass; class << self; self; end; end

I am not that well acquainted with Ruby, but is this what it would look like in expanded form?

def self.metaclass
    def self.self
    end
end

At least that’s how I understand it. However, it still don’t comprehend what this code does, exactly. What is its purpose?

Further on in the code, Why adds this:

arr.each do |a|
   metaclass.instance_eval do
     define_method( a ) do |val|
       @traits ||= {}
       @traits[a] = val
     end
   end
 end

If I understand it correctly, this piece of code adds a new value to @traits with the given name and value. Is that correct?

Thanks for your help, here’s the full source code that caused me trouble, for anyone who wants to see it:

# The guts of life force within Dwemthy's Array
class Creature

# Get a metaclass for this class
def self.metaclass; class << self; self; end; end

# Advanced metaprogramming code for nice, clean traits
def self.traits( *arr )
 return @traits if arr.empty?

 # 1. Set up accessors for each variable
 attr_accessor *arr

 # 2. Add a new class method to for each trait.
 arr.each do |a|
   metaclass.instance_eval do
     define_method( a ) do |val|
       @traits ||= {}
       @traits[a] = val
     end
   end
 end

 # 3. For each monster, the `initialize' method
 #    should use the default number for each trait.
 class_eval do
   define_method( :initialize ) do
     self.class.traits.each do |k,v|
       instance_variable_set("@#{k}", v)
     end
   end
 end

end

# Creature attributes are read-only
traits :life, :strength, :charisma, :weapon
end

And in usage:

class Dragon < Creature
    life( 1340 )     # tough scales
    strength( 451 )  # bristling veins
    charisma( 1020 ) # toothy smile
    weapon( 939 )    # fire breath
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-06-01T16:03:30+00:00Added an answer on June 1, 2026 at 4:03 pm
    class Foo
      def self.bar    # Create a method invoked by Foo.bar instead of Foo.new.bar
        42            # the return value of this method (value of last expression)
      end
    end
    
    
    class Foo
      def self.jim    # Another method on the class itself
        class << self # Change the 'self' to be the metaclass of the current object
          self        # Evaluate the current 'self' as the 'return value' of 
        end           # class<<self…end; and since this is the last expression in
      end             # the method, its value is the return value for the method
    end
    

    In short: what you are seeing defines a method named metaclass on the Creature class itself (not for instances). When you run this method, it finds the metaclass of Creature and returns that.

    Read around the ‘net for what the “metaclass” of an object is.

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

Sidebar

Related Questions

I am getting this error when I run the following code: #!/usr/bin/env ruby -rubygems
I have the following ruby code, from a learning to program book. I understand
I am trying to run the following ruby code from IRB but am unable
I have written the following code snippet to read data from the database :
I copied the following Ruby code from the Internet and made a few changes
The following ruby code in my view is like this: <%= f.text_field :email, :class
We are returning the following JSON from a ruby code: { label:CPU-7, data:[[50,45,38,34]], settings:{
How can I store the string value in stdout from the following Ruby code?
I've narrowed the problem down to the following simple code snippet: #!/usr/bin/env ruby print
Consider the following Ruby code analyzing a three-byte UTF-8 string: #encoding: utf-8 s =

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.