>All Tutorials 
Ajax and .NET Ajax and .NET RSS XML
Ajax and ColdFusion Ajax and ColdFusion RSS XML
Ajax and Java Ajax and Java RSS XML
Ajax and PHP Ajax and PHP RSS XML
Ajax and SOA Ajax and SOA RSS XML
Ajax Goodies Ajax Goodies RSS XML
Ajax Tool Ajax Tool RSS XML
Facebook MockAjax Facebook MockAjax RSS XML
Google Web Toolkit Google Web Toolkit RSS XML
JavaScript Framework JavaScript Framework RSS XML
Ruby on Rails Ruby on Rails RSS XML
Technique Introduction Technique Introduction RSS XML
Without XMLHttpRequest Without XMLHttpRequest RSS XML
Yahoo! UI Yahoo! UI RSS XML

 Duck Typing - type ruby class object method safety types methods

 
Registered tutorials: 501
Registered Users: 33128



Rating: 0 out of 0 votes cast


  
Category: Ruby on Rails

Duck Typing

Digg this   Post to del.icio.us

Abstract: If you have come to Ruby from the Java world, you might see a natural affinity between Ruby’s class hierarchy and Java’s (both have Object at the root). And you might also conclude that Ruby’s modules act in some way similar to Java’s interfaces.

So, what happens if you include Enumerable within a class of yours and forget to create an each method? If you have come to Ruby from the Java world, you might see a natural affinity between Ruby’s class hierarchy and Java’s (both have Object at the root). And you might also conclude that Ruby’s modules act in some way similar to Java’s interfaces. And so, you might expect that including the Enumerable module would check to see whether the each method is defined and throw an exception if it is not there. In Java, after all, interfaces force implementing classes to comply with their public list of methods; in essence, that acts as a contract with which the class must comply. You might reasonably expect Ruby to behave similarly. But let’s explore with an example.

For the sake of illustration, Listing 3 defines a class for bird watching that contains an internal hash of birds with counts of how many times they have been spotted. You might also notice this class declaration includes Enumerable but has not implemented the each function. Uh oh.

Listing 3. A Simple Bird-Watching Class

class BirdWatcher
include Enumerable

def initialize
@birds = {}
end

def saw_bird(bird)
if @birds[bird].nil?
@birds[bird] = 1
else
@birds[bird] += 1
end
end
end


Read Full Tutorial...



Reviews:

Rate and Review This Site

No reviews yet


Statistic Information About this Resource:


Total Hits: 24
Unique Hits: 18


  Daily Weekly Monthly
  Unique Total Unique Total Unique Total
Average 0 0 0 0 0.4 0.5
Current 0 0 0 0 0 0
Previous 0 0 0 0 0 0
Nov 29 0 0 0 0 1 1
Nov 28 0 0 0 0 0 0
Nov 27 0 0 0 0 1 2
Nov 26 0 0 0 0 1 1
Nov 25 0 0 0 0 1 1
Nov 24 0 0 0 0 0 0
Nov 23 0 0 0 0 0 0
Nov 22 0 0 0 0 0 0
Highest 1 3 2 3 4 5


Script Execution Time: 0.33875 | SQL Queries: 10 | Members: 501
Ajax Tutorial Top List - Powered by Aardvark Topsites PHP 5.1.2