<?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>Vinsol - Leading Ruby on Rails Development and Consulting Firm in India &#187; duck typing</title>
	<atom:link href="http://vinsol.com/blog/category/duck-typing/feed/" rel="self" type="application/rss+xml" />
	<link>http://vinsol.com/blog</link>
	<description></description>
	<lastBuildDate>Mon, 26 Jul 2010 08:37:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Second delhi.rb meetup &#8211; Some Advance Ruby Skills</title>
		<link>http://vinsol.com/blog/2007/07/20/second-delhirb-meetup-some-advance-ruby-skills/</link>
		<comments>http://vinsol.com/blog/2007/07/20/second-delhirb-meetup-some-advance-ruby-skills/#comments</comments>
		<pubDate>Fri, 20 Jul 2007 22:45:05 +0000</pubDate>
		<dc:creator>SUR</dc:creator>
				<category><![CDATA[RubyonRails]]></category>
		<category><![CDATA[advance_ruby]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[delegation]]></category>
		<category><![CDATA[delhi.rb]]></category>
		<category><![CDATA[duck typing]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://expressica.com/2007/07/20/second-delhirb-meetup-some-advance-ruby-skills/</guid>
		<description><![CDATA[Hey Everyone,
Vinsol is proudly taking charge to spread Rubyism in delhi and to grow the Ruby &#038; Rails communities here in New Delhi, India. We are organizing delhi.rb meetups around once every month, the meetup is all about ruby and rails as well. The meetup was on 19th July 2007 was our second meetup, first [...]


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Hey Everyone,<br />
<a href="http://vinsol.com">Vinsol</a> is proudly taking charge to spread Rubyism in delhi and to grow the Ruby &#038; Rails communities here in New Delhi, India. We are organizing <a href="http://ruby.meetup.com/108/">delhi.rb</a> meetups around once every month, the meetup is all about ruby and rails as well. The meetup was on 19th July 2007 was our second meetup, first was on 22nd June 2007.</p>
<p><b>Manik presenting SOLR</b><br />
<img src="http://farm2.static.flickr.com/1309/857758434_7c48720bf0.jpg?v=0" alt="Manik presenting SOLR"/></p>
<p><hr /><br />
<b>Me presenting Some Advance Ruby Skills</b><br />
<img src="http://farm2.static.flickr.com/1042/857784340_823c25521d.jpg?v=0" alt="Me presenting Some Advance Ruby Skills"/></p>
<p>More photos <a href="http://www.flickr.com/photos/tags/delhirb/">here</a>.</p>
<p>It was really a nice experience attending the meetup, sharing the ruby/rails thoughts and upcoming features. It helps keeping yourself up-to-date with the latest trends in this technology domain at least in Ruby and Rails(what else m talking except ruby :D). So, there were two presentations in the meetup &#8212; first <a href="http://fromdelhi.com">Manik</a> presented <b>Full text search implementation for Rails using SOLR</b>(it was really an interesting presentation, i got SOLR learning for free, thanks Manik :)), second <a href="http://expressica.com/me">I</a> presented <b>Some Advance Ruby Skills</b> which i am going to share in this post too. Though in the first meetup I presented <b>Caching on RubyOnRails</b> but i haven&#8217;t posted here&#8230;</p>
<h3>Some Advance Ruby Skills</h3>
<p></p>
<h4>1.) Everything is object</h4>
<p>A popular phrase about Ruby, &#8220;Everything is Object&#8221;. At the root of the ruby it is Object. Everything we define in ruby is object. Even the classes we define are actually object. A class defined with <b>class ClassName; end</b> is actually an object of the class <b>Class</b>.<br />
The Object keeps the record of whatever class or module we define. We can justify it as</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  class Klass
  end
  Object.constants.include?("Klass")  # => true
</textarea>
<h4>2.) module_eval</h4>
<p>Use module_eval to define instance and class methods of a class at runtime, when you are outside the class.<br />
<b>example 1</b><br />
defining an instance method</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  class C
  end
  
  C.module_eval do
    define_method :wish do
      p "hello instance method"
    end
  end
  
  c = C.new
  c.wish # => hello instance method
</textarea>
<p><b>example 2</b><br />
defining a class method</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  class C
  end
  
  C.module_eval do
    class << self
      define_method :wish do
        p "hello class method"
        end
    end
  end

  C.wish # => hello class method
</textarea>
<p><b>example 3</b><br />
<b>another form of using module_eval</b><br />
when method body is available as a String object</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  class D
    class << self
      def method_body
        ret =<<-EOS
          def wish
            p "hello, supplied as String object"
          end
        EOS
      end
    end
    
    class C
    end
   
    c = C.new
    
    c.class.module_eval(D.method_body)
   
    c.wish # => hello, supplied as String object
  end
</textarea>
<h4>3.) alias_method</h4>
<p>It is NOT method call delegation but insertion of customized functionalities on a specific method call.</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  class C
    
    def wish
      p "hello"
    end
    
  end

  c = C.new
  c.wish # hello

  class D
    
    class << self
        def keep_some_record
          p "I am keeping some records"
        end
      end
      
  end

  # aliasing the wish method

  c.class.module_eval do
    
    alias_method :wish_orig, :wish
    
    define_method :wish do  
      D.keep_some_record
      wish_orig
    end
    
  end

  c.wish # I am keeping some records; hello
</textarea>
<h4>4.) The Anonymous class</h4>
<p>I just presented same a la <a href="http://expressica.com/2007/03/04/class_self_self_end/">this post</a></p>
<h4>5.) send</h4>
<p>Calling a method when method name is stored as a string object in a variable i.e. you can not see which method to call.<br />
<b>example 1</b><br />
when method name is simply stored as a String object</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
 class C   
    def wish
      p "hello DELHI.rb"
    end
  end
  a = "wish"
  c  = C.new 
  c.send(a)
</textarea>
<p><b>example 2</b><br />
making set method at runtime</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  class C
    attr_accessor :name
  end
  
  c = C.new
  
  a = "name"
  
  c.send(a + "=", "SUR MAX")
  
  p c.send(a)
</textarea>
<p><b>example 3</b><br />
this is interesting, when attribute name itself is <b>send</b></p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  class C
    attr_accessor :send
  end
  
  c = C.new
  
  a = "send"
  
  c.__send__(a + "=", "SUR MAX")
  
  p c.__send__(a) # => Sur Max
</textarea>
<p>well, don&#8217;t say &#8220;what if attribute name is __send__&#8221; <img src='http://expressica.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<h4>6.) The Method class</h4>
<p>Methods of the class are objects of the Method class when retrieved with the method <b>method</b> and can be called with the method <b>call</b>.<br />
<b>example 1</b><br />
anything we define with def-end is an object of the class Method</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  class C    
     def wish
        p "hello"
      end
   end

  c = C.new
  
  m1 = c.method("wish")
  
  p m1.class # => Method

  m1.call # => hello
</textarea>
<p><b>example 2</b><br />
method can hold the object&#8217;s reference and associated instance variables</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  class C
    attr_accessor :name
    
    def initialize(name)
      self.name = name.to_s 
      end
    
    def wish
      p "hello " + name.to_s
    end  
  end
  
  
  c = C.new("Sur Max")
  
  m1 = c.method("wish")
  m1.call # => hello Sur Max
</textarea>
<p><b>example 3</b><br />
we are able to let this method object flow throughout the application code and let it available anywhere in the code.</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  class C
    attr_accessor :name
    
    def initialize(name)
      self.name = name.to_s 
      end
    
    def wish
      p "hello " + name.to_s
    end
    
    def self.supply_wish
      c = new("Sur Max")
      return c.method("wish")
    end
    
  end

  C.supply_wish.call # => hello Sur Max
</textarea>
<h4>7.) what is &#8220;self&#8221;</h4>
<p>I just presented a la <a href="http://expressica.com/2007/04/05/self_realization/">this post</a></p>
<h4>8.) Single Method Delegation - using Forwardable</h4>
<p>Allows you to delegate named method calls to other objects.</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  require 'forwardable'
  
  class C 
    extend Forwardable
    
    attr_accessor :h
    
    def initialize
      @h = {}
    end
    
    def_delegator(:@h, :[], :show)
    def_delegator(:@h, :[]=, :add)
    
  end
  
  end

  c = C.new

  c.add(1, "asdf")

  p c.show(1)

  p c.h
</textarea>
<p>Notice the beauty of ruby here&#8230; The methods [], []= of a hash object are usually called as</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  h = {}
  h["key"] # this will return the corresponding value
  h["key"] = "value" # this will set the "value" corresponding to the "key"
</textarea>
<p>BUT in the above delegation code we are calling them as(delegating the method call on them as)</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  h = {}
  h.[]("key") # this will return the corresponding value
  h.[]=("key", "value") # this will set the "value" corresponding to the "key"
</textarea>
<h4>9.) Full class Delegation - using Delegator</h4>
<p>Extending an object(instance of Class) with the capabilities of another.</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  require 'delegate'

  class Words < DelegateClass(Array)
    
    def initialize(list = "one two three four")
      super(list.split)
    end

  end


  w = Words.new

  p w # => ["one", "two", "three", "four"]

  p w.length # => 4

</textarea>
<h4>10.) SimpleDelegator</h4>
<p>Write memory optimized code with SimpleDelegator&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
require 'delegate'
  
  a = SimpleDelegator.new([10, 20])
  
  old_id = a.__id__
  
  b = a
  
  a.__setobj__("a new object") # this is not possible otherwise with the method "replace" which can replace only object of same class on same memory location
  
  new_id = a.__id__
  
  p a # => "a new object"
  p b  # => "a new object"

  p new_id == old_id # => true
</textarea>
]]></content:encoded>
			<wfw:commentRss>http://vinsol.com/blog/2007/07/20/second-delhirb-meetup-some-advance-ruby-skills/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby is DuckTyped</title>
		<link>http://vinsol.com/blog/2007/05/16/ruby-is-ducktyped/</link>
		<comments>http://vinsol.com/blog/2007/05/16/ruby-is-ducktyped/#comments</comments>
		<pubDate>Wed, 16 May 2007 10:02:08 +0000</pubDate>
		<dc:creator>SUR</dc:creator>
				<category><![CDATA[duck typing]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://expressica.com/2007/05/16/ruby-is-ducktyped/</guid>
		<description><![CDATA[NOTE :  This post may contains repeated and boring stuff for those who already have the programming practice of DuckTyping.
What is Duck Typing ?
Duck Typing is Dynamic Typing
In ruby we do not declare the type of the variable. It automatically becomes the object of the related class. For example, if we assign str = [...]


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p><b>NOTE : </b> This post may contains repeated and boring stuff for those who already have the programming practice of DuckTyping.</p>
<p><b>What is Duck Typing ?</b><br />
Duck Typing is Dynamic Typing<br />
In ruby we do not declare the type of the variable. It automatically becomes the object of the related class. For example, if we assign <b>str = &#8220;hello string&#8221;</b> then the variable str will automatically becomes the object of class String, that we can confirm by issuing str.class. But according to Duck Typing philosophy even <b>Classes are not Types</b>.<br />
That is the basic rule of <b>Duck Typing</b> is that <b>THE TYPE OF OBJECT IS DETERMINED BY WHAT IT CAN DO, NOT BY ITS CLASS</b>. Lets exemplify the statement&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  obj = nil

</textarea>
<p>now here the object <b>obj</b> is a NilClass object but (in ruby) it will be wrong to say that <b>obj</b> is a NilClass type object. The statement is confining the tasks that can be performed by it. Coz we can have the object <b>obj</b> as a NilClass object and still performing singleton functionalities declared over it. Like &#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
 obj = (def wish; puts "good morning, lets code like a Duck :)"; end)
 obj  # =&gt; nil
 obj.class # =&gt; NilClass
 obj.wish # =&gt; good morning, lets code like a Duck <img src='http://expressica.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> 
</textarea>
<p>So, even a nil object can wish you good morning! Here a method <b>wish</b> has been added to a nil object. But it will not be available for all nil objects, or will that be ? Well, thats a homewok for you!!<br />
Check this out, in this case the <b>wish</b> method will be available for all the nil objects&#8230; and the reason is that all the nil objects actually points to a same preserved <b>nil</b> object in the ObjectSpace but not creates a new object everytime like in most cases. Well, i will discuss more deeply regarding the <b>ObjectSpace</b> very soon.</p>
<p>Moreover, extending the modules over an object both at run time and compile time is itself a functionality supporting the statement that <b> Classes are not types</b> and this is what Duck Typing is.</p>
<p><b>Why known as Duck Typing ?</b></p>
<p><a href="http://en.wikipedia.org/wiki/Alex_Martelli">Alex Martelli</a>, a member of the Python Software Foundation , have given the name DuckTyping to this approach in reference with Duck Test &#8230; which says that <b>When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck. </b><br />
So, if an object is behaving like string the it is string type object but not if only it have the class as String.</p>
]]></content:encoded>
			<wfw:commentRss>http://vinsol.com/blog/2007/05/16/ruby-is-ducktyped/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
