Friday, 5 September 2008

Beginning Ruby By Peter Cooper -Part2

Yesterday i started reading Beginning Ruby Book By Peter Cooper.

I wanted to share few important quotations i found from the next 3 chapters(4-5-6) from the book.

1) Difference between split and scan methods?

text = "First-class decisions require clear-headed thinking."
puts "Scan method: #{text.scan(/\w+/).length}"
puts "Split method: #{text.split.length}"

Scan method: 7
Split method: 5

Interesting! The scan method is looking through for all blocks of alphanumeric characters,and, sure enough, there are seven in the sentence. However, if you split by spaces,there are five words. The reason is the hyphenated words. Hyphens aren’t “alphanumeric,” so scan is seeing “first” and “class” as separate words.

2) Most written material, including this very book, contains a high number of words that,although providing context and structure, are not directly useful or interesting. In the last sentence, the words “that,” “and,” “are,” and “or” are not of particular interest, even if the sentence would make less sense without them. These words are typically called “stop words,” and are often ignored by computer systems whose job is to analyze and search through text, because they aren’t words most people are likely to be searching for (as opposed to nouns, for example). Google is a perfect example of this, as it doesn’t want to have to store information that takes up space and that’s generally irrelevant to searches.

For more information about stop words, including links to complete lists, visit http://en.wikipedia.org/wiki/Stop_words.

Example:

text = %q{Los Angeles has some of the nicest weather in the country.}
stop_words = %w{the a by on for of are with just but and to the my I has some}
words = text.scan(/\w+/)
key_words = words.select { |word| !stop_words.include?(word) }
puts key_words.join(' ')

Output: Los Angeles nicest weather country

3) A class is a blueprint for objects. You only have one class called Shape, but with it you can create multiple instances of shapes (shape objects), all of which have the methods and attributes defined by the Shape class.

An object is an instance of a class. If Shape is the class, then x = Shape.new creates a new Shape instance and assigns the object to the variable x. You would then say x is a Shape object, or an object of class Shape.

4) initialize is a special method that’s called when a new object based on that class is created.

5) Ruby’s inheritance features are similarly simple. Any class can inherit the features and functionality of another class, but a class can only inherit from a single other class.Some other languages support multiple inheritance, a feature that allows classes to inherit features from multiple classes, but Ruby doesn’t support this. Multiple inheritance can cause some confusing situations—for instance, classes could inherit from one another in an endless loop—and the efficacy of multiple inheritance is debatable.

Basic Example of Inheritance

class ParentClass
def method1
puts "Hello from method1 in the parent class"
end

def method2
puts "Hello from method2 in the parent class"
end
end

class ChildClass < ParentClass
def method2
puts "Hello from method2 in the child class"
end
end

my_object = ChildClass.new
my_object.method1

6) Reflection and Discovering an Object’s Methods
Reflection is the name of the process by which a computer program can inspect, analyze,and modify itself while it’s running and being used. Ruby takes reflection to an extreme,and allows you to change the functionality of great swathes of the language itself while running your own code.It’s possible to query almost any object within Ruby for the methods that are defined within it. This is another part of reflection.

a = "This is a test"
puts a.methods.join(' ')

7) Polymorphism is the concept of writing code that can work with objects of multiple types and classes at once. For example, the + method works for adding numbers, joining strings, and adding arrays together. What + does depends entirely on what type of things you’re adding together.

Example:

class Animal

attr_accessor :name
def initialize(name)
@name = name
end
end

class Cat < Animal
def talk
"Meaow!"
end
end

class Dog < Animal
def talk
"Woof!"
end
end

animals = [Cat.new("Flossie"), Dog.new("Fido"), Cat.new("Tinkle")]

animals.each do |animal|
puts animal.talk
end

Prints:
Meaow!
Woof!
Meaow!

8) Modules provide a structure to collect Ruby classes, methods, and constants into a single,separately named and defined unit. This is useful so you can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes

Example:

module ToolBox
class Ruler
attr_accessor :length
end
end

module Country
class Ruler
attr_accessor :name
end
end

a = ToolBox::Ruler.new
a.length = 50
b = Country::Ruler.new
b.name = "Ghengis Khan from Moskau"
puts b.name

Prints: Ghengis Khan from Moskau

9) I personally liked Dungeon example very much.

About the Author

Peter Cooper is a highly experienced Ruby developer and trainer. He manages BigBold (www.bigbold.com), a Ruby training and development company, and has produced many commercial web sites using Ruby on Rails, the Ruby-based web framework. In addition, he created Code Snippets, one of the world’s largest public code repositories, and Congress, an online chat client utilizing Ajax and Ruby on Rails. He also created Feed Digest, a feed distribution service that was recently profiled by Business 2.0 magazine.

In addition to development work, Peter has written professionally about various development techniques and tools, producing over 100 articles. He was coeditor of WebDeveloper.com, and worked on iBoost.com and Webpedia.com.

He lives in Lincolnshire, England, with his girlfriend. In his spare time he enjoys hiking, camping, and exploring mountains.

No comments: