Forum: Ruby define_singleton_method(a_symbol,&block)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bill K. (Guest)
on 2006-04-11 14:36
(Received via mailing list)
Hi,

I was wondering what was the idiomatic way to do this:

  class BigDipper
    def little_dipper(a_symbol)
      define_singleton_method(a_symbol) {
        "#{a_symbol} is double-helix"
      }
    end
  end


  s_ = BigDipper.new
  s_.little_dipper(:DNA)
  s_.DNA
    => "DNA is a double-helix"

  q_ = BigDipper.new
  q_.respond_to? :DNA
    => false


?

Thanks,

Bill
unknown (Guest)
on 2006-04-11 17:47
(Received via mailing list)
On Tue, 11 Apr 2006, Bill K. wrote:

> end
>
> s_ = BigDipper.new
> s_.little_dipper(:DNA)
> s_.DNA
>   => "DNA is a double-helix"
>
> q_ = BigDipper.new
> q_.respond_to? :DNA
>   => false


harp:~ > cat a.rb
class BigDipper
   def self.little_dipper(a_symbol)
     define_method(a_symbol) { "#{a_symbol} is double-helix" }
   end
   def little_dipper(*a, &b) self.class.little_dipper(*a, &b) end
end

s_ = BigDipper.new
s_.little_dipper(:DNA)
p s_.DNA

q_ = BigDipper.new
p q_.respond_to?(:DNA)

BigDipper.little_dipper 'DNA2'
p s_.respond_to?('DNA2')
p q_.respond_to?('DNA2')


harp:~ > ruby a.rb
"DNA is double-helix"
true
true
true

hth.

-a
Bill K. (Guest)
on 2006-04-11 22:05
(Received via mailing list)
From: <removed_email_address@domain.invalid>
>>   end
>
> s_.little_dipper(:DNA)
> p s_.DNA

Thanks, Ara.  But is there any way to write it such that the
method is only added to the specific instance of the class?

I would like the following to return false:

> q_ = BigDipper.new
> p q_.respond_to?(:DNA)   # => false


Thanks,

Bill
unknown (Guest)
on 2006-04-11 22:17
(Received via mailing list)
Hi --

On Wed, 12 Apr 2006, Bill K. wrote:

>>>     }
>>>   => false
>> s_ = BigDipper.new
>> s_.little_dipper(:DNA)
>> p s_.DNA
>
> Thanks, Ara.  But is there any way to write it such that the
> method is only added to the specific instance of the class?
>
> I would like the following to return false:
>
>> q_ = BigDipper.new
>> p q_.respond_to?(:DNA)   # => false

Try this:

  class BigDipper
    def little_dipper(a_symbol)
      (class << self; self; end).class_eval do
        define_method(a_symbol) { "#{a_symbol} is double-helix" }
      end
    end
  end


David

--
David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" coming in PDF April 15, and in paper May 5!
http://www.manning.com/black
unknown (Guest)
on 2006-04-11 22:43
(Received via mailing list)
On Wed, 12 Apr 2006, Bill K. wrote:

>> p s_.DNA
>
> Thanks, Ara.  But is there any way to write it such that the
> method is only added to the specific instance of the class?
>
> I would like the following to return false:
>
>> q_ = BigDipper.new
>> p q_.respond_to?(:DNA)   # => false


   harp:~ > cat a.rb
   class BigDipper
     def little_dipper(a_symbol)
       singleton_class{ define_method(a_symbol) { "#{a_symbol} is
double-helix" } }
     end
     def singleton_class &block
       sc = class << self; self; end
       sc.module_eval &block if block
       sc
     end
   end

   s_ = BigDipper.new
   s_.little_dipper(:DNA)
   p s_.respond_to?(:DNA)
   p BigDipper.new.respond_to?(:DNA)


   harp:~ > ruby a.rb
   true
   false

hth.

-a
This topic is locked and can not be replied to.