Tuesday, 2 September 2008

Ruby By Example - Final Part

Just now i finished reading Ruby By Example Book By Kevin C.Baird.

Nice book written by Kevin C.Baird.

I wanted to share few important quotations i found from this book from chapter 9th onwards.

Finally i complete the book and i enjoyed doing a small demo image application as discussed in the book.

I think i will take time comparing rails with j2ee.

1) Ruby converts characters to numerical values by treating a String as an Array and reading values out with indices.The chr method converts from ASCII values back to a String:

irb(main):001:0> s = 'abcde'
=> "abcde"
irb(main):002:0> s[0]
=> 97
irb(main):003:0> s[0].chr
=> "a"
irb(main):004:0> 'a'[0]
=> 97
irb(main):005:0> s[1]
=> 98

2) RubyGems has already become the de facto method of creating stand-alone Ruby software (especially libraries and programmer utilities) for use by others in the Ruby community. With this system, you can easily use other programmers’ software to make your job easier, and you can also share your own work,likely making some other programmer’s job easier. Each bundle of software that is packaged together as a single unit via RubyGems is called a gem, and users are able to manipulate such gems with the appropriately named gem command

3) By executing gem list --local, you can see which gems are already installed on your system.

4) Each individual gem is installable via the command gem install --remote some_gem_name. As an example, let’s install the rails gem with gem install --remote rails.

Install required dependency rake? [Yn]
Install required dependency activesupport? [Yn]
Install required dependency activerecord? [Yn]
Install required dependency actionpack? [Yn]
Install required dependency actionmailer? [Yn]
Install required dependency actionwebservice? [Yn]
Successfully installed rails-1.2.2
Successfully installed rake-0.7.1
Successfully installed activesupport-1.4.1
Successfully installed activerecord-1.15.2
Successfully installed actionpack-1.13.2

5) There are also some pre-packaged versions of Rails available. For Windows,there is Instant Rails (http://instantrails.rubyforge.org), and for Mac OS X, there’s Locomotive (http://locomotive.sourceforge.net). Either of these pieces of software can get you up and running with Rails.

6) Rails operates under a design philosophy (or pattern) for software called Model-View-Controller (MVC), developed by Norwegian computer scientist Trygve Reenskaug1 while he was working at Xerox PARC in the late 1970s.

7) Active Record is the software that gives Rails its intuitive database interaction.It defines Models (which are classes) such that each instance represents a record in a database table. Active Record provides what’s called object-relational mapping (ORM). ORM enables a specific instance of a class to represent a record in a database table.

8) HOW DOES RUBY COMPARE TO OTHER LANGUAGES ?

a) C

Even though it isn’t the oldest language around, C is the granddaddy of languages in many programmers’ minds. For the purposes of this discussion,we’ll be focusing more on the differences between Ruby and C than the similarities. C is procedural, meaning that its programs are intended to be thought of as sets of instructions that proceed stepwise through time: Do this, then do this, then do that. C is neither object oriented nor functional, although the closely related languages C++ and Objective C are object oriented. C undeniably has functions, bits of reusable code that accept various inputs and return various outputs, but they are generally not purely functional functions. C functions often depend on information other than the strict inputs to the function, and they have side effects that mean the second call to a given function will not necessarily produce the same result as the first call. C functions often return values indicating their success, relying on side effects to accomplish their main purpose.

C’s advantages over Ruby include execution speed, greater familiarity to more people, more widespread deployment, and the additional benefits that come from its code being compiled, rather than interpreted. (Note that compiled versus interpreted is its own holy war.) When a C program compiles, you know it has passed at least one specific benchmark of reliability. Ruby’s advantages over C include a faster development cycle, flexibility, conceptual elegance, and configurability. If you are interested in combining the strengths of both Ruby and C, you can start by exploring the RubyInline project, available as the rubyinline gem or at http://www.zenspider.com/ ZSS/Products/RubyInline.

C also has what is called strong, static typing. This means that variables in C are defined to be a certain type of data (the integer 42, for example), and they will always remain that type of data. That’s the static part. If you wanted to express the integer 42 as a floating-point number, such as 42.0, you would need to pass it through a casting conversion function. Ruby is also strongly typed (requiring programmers to convert integers to floating-point numbers before using them that way), although it is dynamic, meaning that variables can change type. C also lacks anything similar to Ruby’s irb.

b) Haskell

The fact that Haskell is included in this listing of languages indicates how important the functional paradigm is to this book. Haskell is a purely functional language designed by committee and released in 1998. It has several fascinating features, most notably lazy evaluation, whereby the value of a given
piece of data does not need to be known (or even meaningful) until it needs to be used. This allows a programmer to define things in terms of infinite series, such as the set of all integers.

Haskell is the language used for Pugs, an implementation of the new Perl 6 language, which some people think is drawing more attention to Haskell than to Perl itself. Haskell has an interactive environment similar to Ruby’s irb, but it doesn’t allow function definitions except in external files that are imported, whereas irb allows full definitions for classes, methods, and functions. Like C, Haskell has both strong and static typing. Haskell is an excellent language that’s very suitable for teaching purely functional techniques,as well as general-purpose programming. You can read more about it at http://haskell.org.

c) Java

For the purposes of this discussion, Sun Microsystems’ Java is a moderately complex, object-oriented language similar to C. Java has both strong and static typing. In one way, Java is more object oriented than Ruby is: A programmer coding in Java must use an object-oriented paradigm for his own programs. On the other hand, Java is less object oriented than Ruby is in the way that it implements its own built-in features.

For example, to get the absolute value of the integer 100 in Java, you would do this:
Math.abs(100)

This means that the programmer wants to use a method called abs, which is associated with Math, to perform that method’s action on the integer 100.

The equivalent operation in Ruby would be performed as follows:
100.abs

Using Ruby’s methodology, the programmer simply asks the number 100 to report its own absolute value. This approach is common in Ruby, and it assumes that every piece of data knows the best way to deal with operations on itself. An advantage of this is that the same symbol can be used for different
(but conceptually related) operations. The + sign signifies simple addition for numbers, for example, while signifying concatenation for strings,Java is also compiled, rather than interpreted, generally using a special type of compilation called bytecode, which is the same method that projects like Parrot, Python, and Ruby 2.0 use.2 There is also an interesting project called JRuby (http://jruby.codehaus.org), which is an implementation of Ruby written in Java. Java is described in greater detail at http://java.sun.com.
The Java specification was written by Guy Steele, although he didn’t create the language itself (fellow Lisper James Gosling did). When he wrote the Java specification, Steele already had the distinction of being the co-creator of Scheme, arguably the most conceptually pure version of Lisp.

d) Python
Python is a language very similar to Ruby. Its creator, the “Benevolent Dictator For Life” Guido van Rossum, named it after the British comedy troupe Monty Python when he invented it in the early 1990s. It has strong, dynamic typing very similar to Ruby’s and a similarly clean syntax, which is aided by its use of semantically significant whitespace. In Python, neither functions, blocks of code, nor statements need to have an explicit end-of-line mark (often a semicolon). Ruby’s use of ending markers is also quite minimal, although not to the same degree as Python’s is.

One area where Python and Ruby differ significantly is in flexibility. Python explicitly embraces the idea of There should be one—and preferably only one—obvious way to do it, reporting this along with other ideas at Python’s interactive prompt when given the command import this. Python and Ruby have an interesting relationship. Python has added several new features recently that borrow heavily from Ruby and Lisp, and at
the time of this writing book sales for Ruby-related books also generally surpass those of Python-related books. Obviously, I hope those trends continue in relation to this book. Python and Ruby seem like contentious siblings who will hopefully continue to challenge and inspire each other to excel. The
Pythonistas live at http://python.org.

e) Perl

Perl is known by its mantra TMTOWTDI, and it is an extremely flexible and utilitarian language—one that has certainly had an impact on both Ruby and programming, in general. TMTOWTDI stands for There’s More Than One Way To Do It, which is a design philosophy that Perl certainly exemplifies. Its role in stressing the importance of regular expressions is enough to earn it a place in history. Perl was invented in 1987 by Larry Wall, and it was primarily intended to perform a role similar to that of Unix-centric languages like
shell, awk, and sed. Perl focuses on ease of use for tasks like Unix system administration, and it is heavily used for web applications, as well. Perl’s initial design was procedural, but in recent years it has moved in an increasingly functional direction. It can also be used for object-oriented programming,
a task for which it was never intended and for which it is not ideally suited—but the fact that this is even possible in Perl is a testament to its flexibility.

A new version of Perl is in the works (see the discussion of Pugs under Haskell), and it reminds me a great deal of Ruby. Coming from me, that is a compliment. Perl has weak dynamic typing, and like Ruby, it is interpreted. It has been called the swiss army chainsaw and the Jeep of programming languages, and it can be found at http://perl.com.

f) Lisp

As one of Ruby’s most prominent ancestors, Lisp deserves some space in this section. Lisp has been called “the most intelligent way to misuse a computer.”3 It is properly understood as a family of languages or a language specification, rather than a single language. It is also diverse enough to resist many classification attempts, but for our purposes, the Lisps can be thought of mainly as functional languages with weak, dynamic typing. Renowned Lisper Paul Graham describes “What Made Lisp Different” at http://paulgraham.com/ diff.html, and it’s interesting to note that Ruby shares all of these features
except for Lisp’s peculiar syntax. Lisp’s syntax (or lack thereof) is probably its most noteworthy feature, at first glance. Lisp code consists of bits of data bound by opening and closing
parentheses. These bits are called lists, and they give Lisp its name (which comes from LISt Processing).4 Having a syntax that is representable as a data structure within the language itself is Lisp’s most defining characteristic. Arguably, another language that implemented this same feature would not be a distinct language per se, but rather another dialect of Lisp.5 A good argument can be made that Ruby tries to take concepts from Lisp and present them within a more user-friendly framework that also takes advantage of good ideas from object orientation, as well as good text manipulation. Matz has said, “Some may say Ruby is a bad rip-off of Lisp or Smalltalk, and I admit that. But it is nicer to ordinary people.”6 Ruby owes much to Lisp, and along with many other languages, it owes much of its powerful text manipulation to this next language, Perl.

About The Author

Kevin C. Baird received his Ph.D. from the State University of New York at Buffalo. He originally wrote his dissertation in Python but rewrote the project after discovering Ruby, and he hasn't looked back since. He has presented at RubyConf and written articles for Linux Journal, Music & Computers magazine, and the New Interfaces for Musical Expression conference proceedings

No comments: