<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>def Tenako.blog () end &#187; constantes</title>
	<atom:link href="http://blog.tenako.com/tag/constantes/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tenako.com</link>
	<description></description>
	<lastBuildDate>Sun, 10 Jul 2011 10:15:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Ruby: metodos de clase privados</title>
		<link>http://blog.tenako.com/2008/09/18/ruby-metodos-de-clase-privados/</link>
		<comments>http://blog.tenako.com/2008/09/18/ruby-metodos-de-clase-privados/#comments</comments>
		<pubDate>Thu, 18 Sep 2008 15:57:15 +0000</pubDate>
		<dc:creator>farruco</dc:creator>
				<category><![CDATA[programacion]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[constantes]]></category>
		<category><![CDATA[dinamicas]]></category>

		<guid isPermaLink="false">http://blog.tenako.com/?p=3</guid>
		<description><![CDATA[Ayer aparecio una duda delante de mi y me asalto, ¿Metodos de clase privados? La primera idea que me paso por la cabeza y que probe fue: class Test &#160; private def Test.class_method p &#34;inside private class method&#34; end &#160; end Si pruebo este codigo en irb, madtrick:~/programacion/ruby madtrick$ irb irb&#40;main&#41;:001:0&#62; require &#34;test28&#34; &#62; true [...]]]></description>
			<content:encoded><![CDATA[<p>Ayer aparecio una duda delante de mi y me asalto, ¿Metodos de clase privados?</p>
<p>La primera idea que me paso por la cabeza y que probe fue:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Test</span>
&nbsp;
private
<span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#CC00FF; font-weight:bold;">Test</span>.<span style="color:#9900CC;">class_method</span>
<span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#996600;">&quot;inside private class method&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Si pruebo este codigo en irb,</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">madtrick:~<span style="color: #000000; font-weight: bold;">/</span>programacion<span style="color: #000000; font-weight: bold;">/</span>ruby madtrick$ irb
irb<span style="color: #7a0874; font-weight: bold;">&#40;</span>main<span style="color: #7a0874; font-weight: bold;">&#41;</span>:001:<span style="color: #000000;">0</span><span style="color: #000000; font-weight: bold;">&gt;</span> require <span style="color: #ff0000;">&quot;test28&quot;</span>
<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #c20cb9; font-weight: bold;">true</span>
irb<span style="color: #7a0874; font-weight: bold;">&#40;</span>main<span style="color: #7a0874; font-weight: bold;">&#41;</span>:002:<span style="color: #000000;">0</span><span style="color: #000000; font-weight: bold;">&gt;</span> Test.class_method
<span style="color: #ff0000;">&quot;inside private class method&quot;</span>
<span style="color: #000000; font-weight: bold;">&gt;</span> nil</pre></div></div>

<p>Podemos ver que no, que esto no funciona porque me deja llamar al metodo perfectamente y todos sabemos que eso no puede pasar ya que en ruby los metodos privados solo permiten ser invocados con un &#8220;self&#8221; explicito.Realmente lo que acabo de decir no es del todo cierto, ya que los utilizando el metodo <em><a title="send" href="http://www.ruby-doc.org/core/classes/Object.html#M000334" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.ruby-doc.org');" target="_blank">send</a></em> para enviar el mensaje da igual la visibilidad del metodo que lo reciva.</p>
<p>Lo que no entiendo es porque falla, supongo que sera porque la restriccion de visibilidad que implica <em>private</em> solo afectara a los metodos de instancia de la clase.Si alguien quiere aportar mas luz sobre esto&#8230;</p>
<p>Volviendo al tema, probemos con el metodo private_class_method.Este metodo cambia la visibilidad de cualquier metodo a privada.Veamos un ejemplo:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Test</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#CC00FF; font-weight:bold;">Test</span>.<span style="color:#9900CC;">class_method</span>
<span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#996600;">&quot;inside private class method&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
private_class_method <span style="color:#ff3333; font-weight:bold;">:class_method</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Lo probamos,</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">madtrick:~<span style="color: #000000; font-weight: bold;">/</span>programacion<span style="color: #000000; font-weight: bold;">/</span>ruby madtrick$ irb
irb<span style="color: #7a0874; font-weight: bold;">&#40;</span>main<span style="color: #7a0874; font-weight: bold;">&#41;</span>:001:<span style="color: #000000;">0</span><span style="color: #000000; font-weight: bold;">&gt;</span> require <span style="color: #ff0000;">&quot;test28&quot;</span>
<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #c20cb9; font-weight: bold;">true</span>
irb<span style="color: #7a0874; font-weight: bold;">&#40;</span>main<span style="color: #7a0874; font-weight: bold;">&#41;</span>:002:<span style="color: #000000;">0</span><span style="color: #000000; font-weight: bold;">&gt;</span> Test.class_method
NoMethodError: private method <span style="color: #000000; font-weight: bold;">`</span>class_method<span style="color: #ff0000;">' called for Test:Class
from (irb):2</span></pre></div></div>

<p>Esto ya es otra cosa,es lo esperado.Este metodo esta muy bien ya que permite cambiar la visibilidad a que por ejemplo herede nuestra clase.</p>
<p>Pero aun nos queda una manera mas de definidir metodos de clase que sean privados.</p>
<p>Como en ruby todo es un objeto, las señoras clases tambien son objetos (de la clase Class).Por tanto tambien pueden tener metodos de instancia, que a efectos practicos se comportan como los metodos de clase definidos de forma normal y corriente en una clase.</p>
<p>¿Y como añado yo metodos de instancia a una clase?Pues hay varios metodos pero el mas sencillo (o por lo menos eso me parece  a mi -se aceptan sugerencias) es el siguiente:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Test</span>
    <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#0000FF; font-weight:bold;">self</span>
      private
        <span style="color:#9966CC; font-weight:bold;">def</span> class_method
         <span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#996600;">&quot;inside private class method&quot;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Lo que acabamos de hacer es abrir la metaclase o clase-virtual de la clase para añadir un el metodo class_method con visibilidad privada.Para el que no sepa que son las clases virtuales o metaclases, le recomiendo leer los enlaces que menciono al final del post.</p>
<p>Si ejecutamos esto en irb,</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">madtrick:~<span style="color: #000000; font-weight: bold;">/</span>programacion<span style="color: #000000; font-weight: bold;">/</span>ruby madtrick$ irb
irb<span style="color: #7a0874; font-weight: bold;">&#40;</span>main<span style="color: #7a0874; font-weight: bold;">&#41;</span>:001:<span style="color: #000000;">0</span><span style="color: #000000; font-weight: bold;">&gt;</span> require <span style="color: #ff0000;">&quot;test28&quot;</span>
<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #c20cb9; font-weight: bold;">true</span>
irb<span style="color: #7a0874; font-weight: bold;">&#40;</span>main<span style="color: #7a0874; font-weight: bold;">&#41;</span>:002:<span style="color: #000000;">0</span><span style="color: #000000; font-weight: bold;">&gt;</span> Test.class_method
NoMethodError: private method <span style="color: #000000; font-weight: bold;">`</span>class_method<span style="color: #ff0000;">' called for Test:Class
from (irb):2</span></pre></div></div>

<p>Una vez mas, funciona correctamente.</p>
<p>Si quieres saber algo mas sobre las metaclasses o clases-virtuales te recomiendo que le heches un vistazo a:</p>
<p><a href="http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/whytheluckystiff.net');" target="_blank"><em>Seein metaclasses clearly</em></a></p>
<p><a href="http://www.ruby-forum.com/topic/102986" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.ruby-forum.com');" target="_blank"><em>Why virtual classes? </em></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tenako.com/2008/09/18/ruby-metodos-de-clase-privados/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

