Project

General

Profile

Actions

Bug #1664

closed

Kernel#define_singleton_method not documented [patch]

Added by marcandre (Marc-Andre Lafortune) almost 15 years ago. Updated almost 13 years ago.

Status:
Closed
Assignee:
-
Target version:
ruby -v:
ruby 1.9.2dev (2009-06-20 trunk 23755) [i386-darwin9.7.0]
Backport:
[ruby-core:23944]

Description

=begin
Kernel#define_singleton_method is currently missing its documentation.
I propose the following:

call-seq:
define_singleton_method(symbol, method) => new_method
define_singleton_method(symbol) { block } => proc

Defines a singleton method in the receiver. The method
parameter can be a +Proc+ or +Method+ object.
If a block is specified, it is used as the method body. This block
is evaluated using instance_eval.

class A
  class << self
    def class_name
      to_s
    end
  end
end
A.define_singleton_method(:who_am_i) do
  "I am: #{class_name}"
end
A.who_am_i   # ==> "I am: A"

guy = "Bob"
guy.define_singleton_method(:hello) { "#{self}: Hello there!" }
guy.hello    # =>  "Bob: Hello there!"

I've included the corresponding patch. This method should also be documented in the 1.8.7 branch.
=end


Files

doc_dsm.patch (1.02 KB) doc_dsm.patch marcandre (Marc-Andre Lafortune), 06/20/2009 07:58 PM
Actions #1

Updated by yugui (Yuki Sonoda) over 14 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

=begin
Applied in changeset r24089.
=end

Actions #2

Updated by yugui (Yuki Sonoda) over 14 years ago

=begin
Thank you. The patch was applied except the last sentence.

On 6/20/09 7:58 PM, Marc-Andre Lafortune wrote:

If a block is specified, it is used as the method body. This block
is evaluated using instance_eval.

define_singleton_method just define a method into the singleton class.
It does not use instance_eval. So it works differently from instance_eval.

module M
obj = Object.new
obj.instance_eval do
T = Class.new
p T.name #=> nil
end
obj.define_singleton_method(:a) do
T = Class.new
p T.name #=> M::T
end
obj.a
end

-- Yugui

=end

Actions

Also available in: Atom PDF

Like0
Like0Like0