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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:13:02+00:00 2026-06-15T07:13:02+00:00

Ruby 1.9.3: I am trying to implement a method which takes as argument a

  • 0

Ruby 1.9.3: I am trying to implement a method which takes as argument a class, a symbol and a proc and defines (eventually overwriting) a new instance method for that class, but not a class method so that this test should pass:

require 'test/unit'

include Test::Unit::Assertions

class String
  def my_method
    'my_method'
  end
end

def define_instance_method(klass, method, &block)
  # klass.send :define_method, method, &block
  # ...
end

define_instance_method(Object, :my_method) { 'define_instance_method my_method' }
define_instance_method(String, :my_method) { 'define_instance_method my_method' }

assert Object.new.my_method == 'define_instance_method my_method'
assert_raise(NoMethodError) { Object.my_method }

assert String.new.my_method == 'define_instance_method my_method'
assert_raise(NoMethodError) { String.my_method }

I did many tryings (define_method, define_singleton_method, class_eval, instance_eval, instance_exec, module_eval…) but with no success; do you have any ideas?

  • 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-15T07:13:03+00:00Added an answer on June 15, 2026 at 7:13 am

    @ProGNOMmers, recall that objects in Ruby are classes and classes are objects.

    So everything in Ruby are objects, even classes 🙂

    And when you define a method inside Object it will also be available to Class as well as to any class that inherits from Object.

    That’s mean to all classes.

    Some proof:

    define_instance_method(String, :my_method) { 'define_instance_method my_method' }
    
    p String.new.my_method
    p String.my_method
    
    # =>  "define_instance_method my_method"
    # =>  stackoverflow.rb:11:in `<main>': undefined method `my_method' for String:Class (NoMethodError)
    

    See, your method not yet defined inside Object, thus not available inside String.

    Now let’s do it:

    define_instance_method(Object, :my_method) { 'define_instance_method my_method' }
    
    p String.my_method
    # => "define_instance_method my_method"
    
    p Array.my_method
    # => "define_instance_method my_method"
    
    p NilClass.my_method
    # => "define_instance_method my_method"
    

    So just comment define_instance_method(Object... in your tests
    and String specs will pass just well.

    Here are some crash tests

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

Sidebar

Related Questions

I'm new to Ruby (being a Java dev) and trying to implement a method
I'm trying to figure out how to implement an event in a ruby class.
I'm trying to write a regex or Ruby method which will find the longest
I am trying to implement ruby on rails app using Capistrano, I am running
I'm new to ruby/rails and trying to build a simple Projects / Tags app
I am trying to implement Superfeedr subscriptions using PubSubHubbub and Ruby on Rails. The
I'm trying to implement the following code in Ruby and getting stuck on it.
I've been trying to implement the Luhn algorithm in Ruby. I've been following these
I am trying to learn Ruby, and want to implement the Python algorithms from
I'm trying to fill in class variables for a class instance from a file,

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.