SCSI Hard Drives
Search
Advanced Search

Categories


Recently Viewed

Clear List
Pages



My Links
Web Directory Index
A human edited, comprehensive web directory list.
Link Exchange
DesignFirms Link Exchange


Ruby Classes and Objects

By : yankees26an
Rating : Average Rating : 5.33 From 9 Voter(s)


Ruby is an object-oriented programming language; this chapter will show you what that reallymeans. Like all modern languages, Rubysupports object-oriented notions like classes, inheiritance, and polymorphism. But Ruby goes further than other languages you may have used. Some languages are strict and some are permissive; Ruby is one of the most permissive languages around.Strict languages enforce strong typing, usually at compile type: a variable defined asan arrayc an’t be used as another data type. If a method takes an arrayas an argument,you can’t pass in an array-like object unless that object happens to be a subclassof the array class or can be converted into an array.


Ruby enforces dynamic typing, or duck typing (“if it quacks like a duck, it is aduck”). A strongly typed language enforces its typing everywhere, even when it’s not needed. Ruby enforces its duck typing relative to a particular task. If a variablequacks like a duck, it is one—assuming you wanted to hear it quack. When you want “swims like a duck” instead, duck typing will enforce the swimming, and notthe quacking.

Here’s an example. Consider the following three classes, Duck, Goose, and DuckRecording:

 

class Duck

def quack

'Quack!'

end

def swim

'Paddle paddle paddle...'

end

 

end

 

class Goose

def honk

'Honk!'

end

def swim

'Splash splash splash...'

end

end

class DuckRecording

def quack

play

end

def play

'Quack!'

end

end

 

def make_it_quack(Duck duck)

duck.quack

end

make_it_quack(Duck.new) # => "Quack!"

make_it_quack(DuckRecording.new)

# TypeException: object not of type Duck

 

 

 

 

 

 

def make_it_swim(Duck duck)

duck.swim

end

make_it_swim(Duck.new) # => "Paddle paddle paddle..."

make_it_swim(Goose.new)

# TypeException: object not of type Goose

 

swim:

def make_it_quack(duck)

duck.quack

end

make_it_quack(Duck.new) # => "Quack!"

make_it_quack(DuckRecording.new) # => "Quack!"

def make_it_swim(duck)

duck.swim

end

make_it_swim(Duck.new) # => "Paddle paddle paddle..."

make_it_swim(Goose.new) # => "Splash splash splash..."

 

 

 

 

 

Over time, strict languages develop workarounds for their strong typing (have you ever done a cast when retrieving something from an Java collection?), and then workarounds for the workarounds (have you ever created a parameterized Java collection using generics?). Rubyjust doesn’t bother with anyof it. If an object supports the method you’re trying to use, Ruby gets out of its way and lets it work. Ruby’s permissiveness is more a matter of attitude than a technical advancement. Python lets you reopen a class after its original definition and modify it after the fact, but the language syntax doesn’t make many allowances for it. It’s sort of a dirty little secret of the language.

In Ruby, this behavior is not only allowed, it’s encouraged. Some parts of the standard libraryadd functionalityto built-in classes when imported, just to make it easier for the programmer to write code. The Facets Core libraryadds dozens of convenience methods to Ruby’s standard classes. Ruby is proud of this capability, and urges programmers to exploit it if it makes their lives easier. Strict languages end up needing code generation tools that hide the restrictions and complexities of the language. Rubyhas code generation tools built right into the language, saving you work while leaving complete control in your hands (see Chapter 10). Is this chaotic? It can be. Does it matter? Only when it actually interferes with you getting work done. In this chapter and the next two, we’ll show you how to follow common conventions, and how to impose order on the chaos when you need it. With Rubyy ou can impose the right kind of order on your objects, tailored for your situation, not a one-size-fits all that makes you jump through hoops most of the time. These recipes are probably less relevant to the problems you’re trying to solve than the other ones in this book, but they’re not less important. This chapter and the next two provide a general-purpose toolbox for doing the dirtywork of actual programming, whatever your underlying purpose or algorithm. These are the chapters you should turn to when you find yourself stymied by the Ruby language itself, or grinding through tedious makework that Ruby’s labor-saving techniques can eliminate. Every other chapter in this book uses the ideas behind these recipes







Comments / Feedback

Richard L Muller Email
October 25, 2007, 9:47 pm

I enjoyed the tutorial. The last few paragraphs stated the case for Ruby's approach well IMHO.

A minor error might bother real beginners: "def make_it_quack(Duck duck)" is C/C++ style. As I'm sure you know, Ruby doesn't allow a "type" qualifier for its parameters.
Alan S. Email
October 24, 2008, 1:32 pm

Thanks for the article and thanks Richard for the comment, very useful! Starting to see some of the ways I've been going wrong.

One thing though, as a 'real beginner' myself, how should "def make_it_quack(Duck duck)" have been? I'm sure I'm not alone among beginners in not knowing nearly enough C (or in fact any) to see precisely what the problem is. Thanks
buy soma Email
February 6, 2009, 7:23 am

nice site you have!
pest control Email
February 24, 2009, 3:36 am

Ruby vs Python ?
erotik Email
March 9, 2009, 12:46 am

Dies ist ein großer Ort. Ich möchte hier noch einmal.
sudh new Email
February 11, 2010, 2:26 am

uhcf vc vjncvs ncv ,mv dsv
RSS 2.0: Syndicate this article

Add Comment
* Name


* Email Address


Site



*Image Validation (?)


*Comments / Feedback





Print Article Print Article Send to a friend Send to a friend Save as PDF Save as PDF Social Bookmarking
Add to: Mr. Wong Add to: Webnews Add to: Icio Add to: Oneview Add to: Folkd Add to: Yigg Add to: Linkarena Add to: Digg Add to: Del.icio.us Add to: Reddit Add to: Simpy Add to: StumbleUpon Add to: Slashdot Add to: Netscape Add to: Furl Add to: Yahoo Add to: Spurl Add to: Google Add to: Blinklist Add to: Blogmarks Add to: Diigo Add to: Technorati Add to: Newsvine Add to: Blinkbits Add to: Ma.Gnolia Add to: Smarking Add to: Netvouz Information
Rate this Article :

1

2

3

4

5

6

7

8

9

10
Poor Excellent