<?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>WowKhmer Tech &#187; Ruby</title>
	<atom:link href="http://tech.wowkhmer.com/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://tech.wowkhmer.com</link>
	<description>We.Passionate.Technologies</description>
	<lastBuildDate>Sun, 28 Mar 2010 08:39:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.3</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Method Access Levels in Ruby</title>
		<link>http://tech.wowkhmer.com/2009/10/method-access-levels-in-ruby/</link>
		<comments>http://tech.wowkhmer.com/2009/10/method-access-levels-in-ruby/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 01:14:53 +0000</pubDate>
		<dc:creator>Samnang Chhun</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://tech.wowkhmer.com/?p=139</guid>
		<description><![CDATA[A common point of confusion on method access levels for some programmers who just started in Ruby. There are three access levels: public, protected, and private.

Public methods can be called by anyone—no access control is enforced. Methods are public by default (except for initialize, which is always private).
Protected methods can be invoked only by objects [...]]]></description>
			<content:encoded><![CDATA[<p>A common point of confusion on method access levels for some programmers who just started in Ruby. There are three access levels: public, protected, and private.</p>
<ul>
<li><strong>Public methods</strong> can be called by anyone—no access control is enforced. Methods are public by default (except for initialize, which is always private).</li>
<li><strong>Protected methods</strong> can be invoked only by objects of the deﬁning class and its sub-classes. Access is kept within the family.</li>
<li><strong>Private methods</strong> cannot be called with an explicit receiver—the receiver is always the current object, also known as self. This means that private methods can be called only in the context of the current object; you can’t invoke another object’s private methods.</li>
</ul>
<p>The difference between &#8220;protected&#8221; and &#8220;private&#8221; is fairly subtle and is different in Ruby than in most common OO languages. If a method is protected, it may be called by any instance of the deﬁning class or its subclasses. If a method is private, it may be called only within the context of the calling object—it is never possible to access another object’s private methods directly, even if the object is of the same class as the caller.</p>
<p>Suppose you have two instances of the Foo class, A and B. In languages like C#, A and B can call each other&#8217;s private methods. In Ruby, you need to use a protected method for that. This is the main difference between private and protected methods in Ruby.</p>
<p>Suppose further that you have a class called Foo and a subclass SubFoo. In languages like C#, SubFoo has no access to any private methods defined by Foo. In Ruby, it provides no way to hide a class&#8217;s methods from its subclasses. In this way, Ruby&#8217;s private works like C#&#8217;s protected.</p>
<p>To demonstrate these differences, let’s see in a example below:</p>
<pre class="brush: ruby;">
class Foo
  def public_default_bar
    &quot;public&quot;
  end

  protected
  def protected_bar
    &quot;protected&quot;
  end

  private
  def private_bar
    &quot;private&quot;
  end

  def private_bar=(bar)
    @bar = &quot;private&quot;
  end

  public
  def public_bar
    &quot;public&quot;
  end
end

class SubFoo &lt; Foo
  def check_protected_method_explicit_receiver
    self.protected_bar #it works
  end

  def check_protected_method_implicit_receiver
    protected_bar #it works
  end

  def check_private_method_explicit_receiver
    self.private_bar #it doesn't work
  end

  def check_private_method_implicit_receiver
    private_bar #it works
  end

  def check_set_private_method_explicit_receiver
    self.private_bar = &quot;foo&quot; #it works, specail case only for set method
  end

  def check_set_private_method_implicit_receiver
    private_bar = &quot;foo&quot; #it doesn't call the method, it's just a local variable assignment here
  end
end
</pre>
<p>I put the comments on each method in SubFoo to clarify which method call works, but you can also instantiate object from SubFoo in irb and call each method to see the result. Hopefully, you get understand and use it correctly when you do class design.</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.wowkhmer.com/2009/10/method-access-levels-in-ruby/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>.NET Coder should give Ruby a try!</title>
		<link>http://tech.wowkhmer.com/2009/01/net-coder-should-give-ruby-a-try/</link>
		<comments>http://tech.wowkhmer.com/2009/01/net-coder-should-give-ruby-a-try/#comments</comments>
		<pubDate>Tue, 20 Jan 2009 01:29:00 +0000</pubDate>
		<dc:creator>Samnang Chhun</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[technologies]]></category>

		<guid isPermaLink="false">http://tech.wowkhmer.com/?p=43</guid>
		<description><![CDATA[Last weekend, I gave Ruby a try. Ruby is a dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write. I&#8217;m a .NET coder, everyday I use C# for developing projects in my company. There are few reasons that [...]]]></description>
			<content:encoded><![CDATA[<p>Last weekend, I gave <a href="http://www.ruby-lang.org/">Ruby</a> a try. Ruby is a dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write. I&#8217;m a .NET coder, everyday I use C# for developing projects in my company. There are few reasons that bring me to try Ruby because I see a lot programmers mention Ruby is a very powerful and flexible programming language, is it true? And the main reason, I would like to try something new because almost my programming skills are just in static typed language and a bit dynamic typed language with Javascript.</p>
<p>After I played with Ruby about a few hours, I started loving it. Ruby is a fun toy. Like the differences between spoken languages, Ruby differs from most other programming languages not only by syntax, but by culture, grammar, and customs. And some programmers consider Ruby is English for computers. Ruby brings a new way of thinking about programming and software development. I would like to show you very basic examples, let’s go to see snippet code comparing between C# and Ruby.</p>
<pre class="brush: csharp;">
//Swap value between two variables
int a = 5, b = 10;
int temp = a;
a = b;
b = temp;

//Output message 3 times
for(int i = 0; i &lt;= 3; i++)
 Console.WriteLine(&quot;Hello&quot;);

//Output message if condition is true string
str = string.Empty;
if(str == string.Empty)
 Console.WriteLine(&quot;str is empty&quot;);
</pre>
<pre class="brush: ruby;">
#Swap value between two variables
a, b = 5, 10
a, b = b, a

#Output message 3 times
3.times { puts &quot;Hello&quot; }

#Output message if condition is true
str = ''
puts &quot;str is empty&quot; if str.empty?
</pre>
<p>Code comparisons above are not to show which language is bad and another is good, but I just want to show how I feel about new language. Ruby code is short and readable like in English. There are lots of more powerful features in Ruby that I haven’t mentioned here.</p>
<p>If you haven’t touched something new recently, why don’t you give Ruby a try?</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.wowkhmer.com/2009/01/net-coder-should-give-ruby-a-try/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
