<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.1.1" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Ruby protected constructors</title>
	<link>http://grahamis.com/blog/2007/08/24/ruby-protected-constructors/</link>
	<description>It's too real to be true</description>
	<pubDate>Fri, 21 Nov 2008 17:26:36 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.1.1</generator>

	<item>
		<title>By: Josh</title>
		<link>http://grahamis.com/blog/2007/08/24/ruby-protected-constructors/#comment-396</link>
		<author>Josh</author>
		<pubDate>Fri, 24 Aug 2007 04:29:00 +0000</pubDate>
		<guid>http://grahamis.com/blog/2007/08/24/ruby-protected-constructors/#comment-396</guid>
					<description>Well, I tried to get Module.protected to work the way you'd want, but it breaks the usual usage of protected as a keyword where subsequent methods are protected.

So, this works:

&lt;pre name="code" class="ruby"&gt;
protected :new, :hello
&lt;/pre&gt;

Because protected is passed all the symbols you want to protect. Unfortunately, this doesn't work:


&lt;pre name="code" class="ruby"&gt;
protected
  def hello
     "hello"
  end
&lt;/pre&gt;

Because it appears that kernel magic is suppressed (can anyone enlighten?)


Therefore, recommend you stay with the solution (in the blog post).


Anyway, for posterity, here is an almost-but-not-quite-solution:

&lt;pre name="code" class="ruby"&gt;
class Module
	alias mcp_protected protected

	def protected(*args)
		if args.member? :new
			protect_ctor
			args = args - [:new]
		end
		args.each do &#124;arg&#124;; mcp_protected arg; end
	end
end

class Class
	def protect_ctor
		class_eval(%Q[
			class &lt;&lt; self
				mcp_protected :new
				
				def inherited(klass)
					klass.class_eval do
						def self.new(*args); super; end
					end
				end
			end
		])
	end
end
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Well, I tried to get Module.protected to work the way you&#8217;d want, but it breaks the usual usage of protected as a keyword where subsequent methods are protected.</p>
<p>So, this works:</p>
<pre name="code" class="ruby">
protected :new, :hello
</pre>
<p>Because protected is passed all the symbols you want to protect. Unfortunately, this doesn&#8217;t work:</p>
<pre name="code" class="ruby">
protected
  def hello
     "hello"
  end
</pre>
<p>Because it appears that kernel magic is suppressed (can anyone enlighten?)</p>
<p>Therefore, recommend you stay with the solution (in the blog post).</p>
<p>Anyway, for posterity, here is an almost-but-not-quite-solution:</p>
<pre name="code" class="ruby">
class Module
	alias mcp_protected protected

	def protected(*args)
		if args.member? :new
			protect_ctor
			args = args - [:new]
		end
		args.each do |arg|; mcp_protected arg; end
	end
end

class Class
	def protect_ctor
		class_eval(%Q[
			class < < self
				mcp_protected :new

				def inherited(klass)
					klass.class_eval do
						def self.new(*args); super; end
					end
				end
			end
		])
	end
end
</pre>
</pre>
]]></content:encoded>
				</item>
	<item>
		<title>By: James Crisp &#38; Lawrence Song</title>
		<link>http://grahamis.com/blog/2007/08/24/ruby-protected-constructors/#comment-397</link>
		<author>James Crisp &#38; Lawrence Song</author>
		<pubDate>Tue, 28 Aug 2007 06:31:17 +0000</pubDate>
		<guid>http://grahamis.com/blog/2007/08/24/ruby-protected-constructors/#comment-397</guid>
					<description>Hey Josh!

Nice work! 

Minor bug report.. If you want to put 'protect_constructor' in 'Class' you need to drop the self. Ie,

def protect_constructor
  class_eval...

Alternatively, you can put 'protect_constructor' in 'Object' with the explicit self reference.

Cheers,
Lawrence &#38; James</description>
		<content:encoded><![CDATA[<p>Hey Josh!</p>
<p>Nice work! </p>
<p>Minor bug report.. If you want to put &#8216;protect_constructor&#8217; in &#8216;Class&#8217; you need to drop the self. Ie,</p>
<p>def protect_constructor<br />
  class_eval&#8230;</p>
<p>Alternatively, you can put &#8216;protect_constructor&#8217; in &#8216;Object&#8217; with the explicit self reference.</p>
<p>Cheers,<br />
Lawrence &amp; James</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Josh</title>
		<link>http://grahamis.com/blog/2007/08/24/ruby-protected-constructors/#comment-398</link>
		<author>Josh</author>
		<pubDate>Tue, 28 Aug 2007 06:48:46 +0000</pubDate>
		<guid>http://grahamis.com/blog/2007/08/24/ruby-protected-constructors/#comment-398</guid>
					<description>Done - thanks :-)</description>
		<content:encoded><![CDATA[<p>Done - thanks <img src='http://grahamis.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></p>
]]></content:encoded>
				</item>
</channel>
</rss>
