<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Pragmatic Forums | Posts in topic 'define_method in instance_eval'</title>
    <link>/forums/77/topics/561.rss</link>
    <language>en-us</language>
    <ttl>60</ttl>
    <item>
      <title>define_method in instance_eval posted by khaled alhabache @ Fri, 26 Dec 2008 14:56:26 +0000</title>
      <description>&lt;p&gt;You can bypass that by defining the method to your singleton class&lt;br /&gt;smthing like :&lt;/p&gt;


	&lt;p&gt;class Object&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;def singleton_class 
  class &amp;lt;&amp;lt; self
    self 
  end 
end&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;end&lt;/p&gt;


	&lt;p&gt;class Foo&lt;br /&gt;end&lt;/p&gt;


	&lt;p&gt;Foo.singleton_class.class_eval do
  define_method(:shout) do
    &amp;#8220;shouting&amp;#8230;&amp;#8221; 
  end&lt;br /&gt;end&lt;/p&gt;


	&lt;p&gt;puts Foo.respond_to? :shout #true&lt;/p&gt;</description>
      <pubDate>Fri, 26 Dec 2008 14:56:26 +0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:561:7113</guid>
      <author>khaled alhabache</author>
      <link>http://forums.pragprog.com/forums/77/topics/561</link>
    </item>
    <item>
      <title>define_method in instance_eval posted by James Whiteman @ Thu, 19 Jun 2008 16:09:13 +0000</title>
      <description>&lt;p&gt;Thanks, Dave&amp;#8212;fantastic series by the way.&lt;/p&gt;</description>
      <pubDate>Thu, 19 Jun 2008 16:09:13 +0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:561:3248</guid>
      <author>James Whiteman</author>
      <link>http://forums.pragprog.com/forums/77/topics/561</link>
    </item>
    <item>
      <title>define_method in instance_eval posted by Dave Thomas @ Thu, 19 Jun 2008 12:53:19 +0000</title>
      <description>&lt;p&gt;define_method always creates an instance method&amp;#8212;now idea why, but it&amp;#8217;s a deliberate decision of the implementers.&lt;/p&gt;


	&lt;p&gt;Dave&lt;/p&gt;</description>
      <pubDate>Thu, 19 Jun 2008 12:53:19 +0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:561:3242</guid>
      <author>Dave Thomas</author>
      <link>http://forums.pragprog.com/forums/77/topics/561</link>
    </item>
    <item>
      <title>define_method in instance_eval posted by James Whiteman @ Wed, 18 Jun 2008 23:17:40 +0000</title>
      <description>&lt;p&gt;Hello, all.&lt;/p&gt;


	&lt;p&gt;Does anyone have a good explanation for this?&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
class Foo
end

Foo.instance_eval do
  def jump
    "jumping..." 
  end

  define_method(:shout) do
    "shouting..." 
  end
end

Foo.respond_to? :jump
  =&amp;gt; true
Foo.respond_to? :shout
  =&amp;gt; false
Foo.new.respond_to? :shout
  =&amp;gt; true
&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;

	&lt;p&gt;It seems that define_method is pushing the method &amp;#8216;shout&amp;#8217; into Foo&amp;#8217;s own m_tbl instead of in the metaclass.&lt;br /&gt;This was a little unexpected to me.&lt;/p&gt;</description>
      <pubDate>Wed, 18 Jun 2008 23:17:40 +0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:561:3232</guid>
      <author>James Whiteman</author>
      <link>http://forums.pragprog.com/forums/77/topics/561</link>
    </item>
  </channel>
</rss>
