Saturday, September 6, 2014

Datatypes and Objects

In order to understand a programming language, you have to know what kinds of data it can manipulate and what it can do with that data. This chapter is about the values manipulated by Ruby programs. It begins with comprehensive coverage of numeric and textual values. The chapter then moves on to explain ranges, symbols, and the special values true, false, and nil.

1. Numbers

Ruby includes five built-in classes for representing numbers, and the standard library includes three more numeric classes that are sometimes useful. Figure shows the class hierarchy.



 All number objects in Ruby are instances of Numeric. All integers are instances of Integer. The Complex, BigDecimal, and Rational classes are not built-in to Ruby but are distributed with Ruby as part of the standard library. The Complex class represents complex numbers, of course. BigDecimal represents real numbers with arbitrary precision, using a decimal representation rather than a binary representation. And Rational represents rational numbers: one integer divided by another.

All numeric objects are immutable; there are no methods that allow you to change the value held by the object. If you pass a reference to a numeric object to a method, you need not worry that the method will modify the object.

1.1. Integer Literals

An integer literal is simply a sequence of digits:

0
123
12345678901234567890


If the integer values fit within the range of the Fixnum class, the value is a Fixnum. Otherwise, it is a Bignum, which supports integers of any size. Underscores may be inserted into integer literals (though not at the beginning or end), and this feature is sometimes used as a thousands separator:

1_000_000_000        # One billion (or 1,000 million in the UK)

1.2 Floating-Point Literals

A floating-point literal is an optional sign followed by one or more decimal digits, a decimal point (the . character), one or more additional digits, and an optional exponent. An exponent begins with the letter e or E, and is followed by an optional sign and one or more decimal digits. Here are some examples of floating-point literals:

0.0
-3.14
6.02e23                   # This means 6.02 × 1023
1_000_000.01        # One million and a little bit more


Ruby borrows the ** operator from Fortran for exponentiation. Exponents need not be integers:

x**4                # This is the same thing as x*x*x*x
x**-1              # The same thing as 1/x
x**(1/3.0)       # The cube root of x
x**(1/4)          # Oops! Integer division means this is x**0, which is always 1
x**(1.0/4.0)    # This is the fourth-root of x
 
When multiple exponentiations are combined into a single expression, they are evaluated from right to left. Thus, 4**3**2 is the same as 4**9, not 64**2.

2. Text

Text is represented in Ruby by objects of the String class. Strings are mutable objects, and the String class defines a powerful set of operators and methods for extracting substrings, inserting and deleting text, searching, replacing, and so on. Ruby provides a number of ways to express string literals in your programs, and some of them support a powerful string interpolation syntax by which the values of arbitrary Ruby expressions can be substituted into string literals.

2.1 String Literals

Ruby provides quite a few ways to embed strings literally into your programs.

2.1.1 Single-quoted string literals
 
The simplest string literals are enclosed in single quotes (the apostrophe character). The text within the quote marks is the value of the string:
'This is a simple Ruby string literal'

Single-quoted strings may extend over multiple lines, and the resulting string literal includes the newline characters. It is not possible to escape the newlines with a backslash:
'This is a long string literal \
that includes a backslash and a newline'


2.1.2 Double-quoted string literals

String literals delimited by double quotation marks are much more flexible than singlequoted literals. Double-quoted literals support quite a few backslash escape sequences, such as \n for newline, \t for tab, and \" for a quotation mark that does not terminate the string:
 
"\t\"This quote begins with a tab and ends with a newline\"\n"
"\\"               # A single backslash

2.1.3 Arbitrary delimiters for string literals

When working with text that contains apostrophes and quotation marks, it is awkward to use it as single- and double-quoted string literals. Ruby supports a generalized quoting syntax for string literals. The sequence %q begins a string literal that follows single-quoted string rules, and the sequence %Q (or just %) introduces a literal that follows double-quoted string rules. The first character following q or Q is the delimiter character, and the string literal continues until a matching (unescaped) delimiter is found. If the opening delimiter is (, [, {, or <, then the matching delimiter is ), ], }, or >. Otherwise, the closing delimiter is the same as the opening delimiter. Here are some examples:

%q(Don't worry about escaping ' characters!)
%Q|"How are you?", he said|
%-This string literal ends with a newline\n-              # Q omitted in this one

2.1.4 String literals and mutability

Strings are mutable in Ruby. Therefore, the Ruby interpreter cannot use the same object to represent two identical string literals. (If you are a Java programmer, you may find this surprising.) Each time Ruby encounters a string literal, it creates a new object. If you include a literal within the body of a loop, Ruby will create a new object for each iteration. You can demonstrate this for yourself as follows:

10.times { puts "test".object_id }
 
For efficiency, you should avoid using literals within loops.

2.2. Character Literals

Single characters can be included literally in a Ruby program by preceding the character with a question mark. No quotation marks of any kind are used:

?A          # Character literal for the ASCII character A
?"           # Character literal for the double-quote character
??          # Character literal for the question mark character

Although Ruby has a character literal syntax, it does not have a special class to represent single characters. Also, the interpretation of character literals has changed between Ruby 1.8 and Ruby 1.9. In Ruby 1.8, character literals evaluate to the integer encoding of the specified character. ?A, for example, is the same as 65 because the ASCII encoding for the capital letter A is the integer 65. In Ruby 1.8, the character literal syntax only works with ASCII and single-byte characters.

2.3. String Operators

The String class defines several useful operators for manipulating strings of text. The + operator concatenates two strings and returns the result as a new String object:

planet = "Earth"
"Hello" + " " + planet       # Produces "Hello Earth"


The << operator appends its second operand to its first, and should be familiar to C++ programmers. This operator is very different from +; it alters the lefthand operand rather than creating and returning a new object:
 
greeting = "Hello"
greeting << " " << "World"
puts greeting                   # Outputs "Hello World"

The * operator expects an integer as its righthand operand. It returns a String that repeats the text specified on the lefthand side the number of times specified by the righthand side:
 
ellipsis = 'a'*3          # Evaluates to 'aaa'

2.4 Accessing Characters and Substrings

Perhaps the most important operator supported by String is the square-bracket arrayindex operator [], which is used for extracting or altering portions of a string. This operator is quite flexible and can be used with a number of different operand types. It can also be used on the lefthand side of an assignment, as a way of altering string content.

In Ruby 1.8, a string is like an array of bytes or 8-bit character codes. The length of this array is given by the length or size method, and you get or set elements of the array simply by specifying the character number within square brackets:

s = 'hello';                    # Ruby 1.8
s[0]                             # 104: the ASCII character code for the first character 'h'
s[s.length-1]                # 111: the character code of the last character 'o'
s[-1]                           # 111: another way of accessing the last character
s[-2]                           # 108: the second-to-last character
s[-s.length]                  # 104: another way of accessing the first character
s[s.length]                   # nil: there is no character at that index
s[0,2]                         # "he"
s[-1,1]                       # "o": returns a string, not the character code ?o
s[0,0]
                        # "": a zero-length substring is always empty
s[0,10]
                     # "hello": returns all the characters that are available
s[s.length,1]
               # "": there is an empty string immediately beyond the end
s[s.length+1,1]          # nil: it is an error to read past that
s[0,-1]
                      # nil: negative lengths don't make any sense
s[0,1] = "H"
                   # Replace first letter with a capital letter
s[s.length,0] = " world"   # Append by assigning beyond the end of the string
s[5,0] = ","
                    # Insert a comma, without deleting anything
s[5,6] = ""
                    # Delete with no insertion; s == "Hellod"
s[2..3]
                          # "ll": characters 2 and 3
s[-3..-1]
                      # "llo": negative indexes work, too
s[0..0]
                         # "h": this Range includes one character index



Continue Reading...

Friday, July 25, 2014

Structure of Ruby Programs


Here you will learn the basic structure of Ruby programs. It starts with the lexical structure, covering tokens and the characters that comprise them. Next, it covers the syntactic structure of a Ruby program, explaining how expressions, control structures, methods, classes, and so on are written as a series of tokens.

1. Lexical Structure

The Ruby interpreter parses a program as a sequence of tokens. Tokens include comments, literals, punctuation, identifiers, and keywords. This section introduces these types of tokens and also includes important information about the characters that comprise the tokens and the whitespace that separates the tokens.

1.1. Comments

Comments in Ruby begin with a # character and continue to the end of the line. If a # character appears within a string or regular expression literal, then it is simply part of the string or regular expression and does not introduce a comment:

# This entire line is a comment
x = "#This is a string"                             # And this is a comment
y = /#This is a regular expression/          # Here's another comment

1.2. Literals

Literals are values that appear directly in Ruby source code. They include numbers, strings of text, and regular expressions. For example :

1               # An integer literal
1.0            # A floating-point literal
'one'          # A string literal
"two"         # Another string literal
/three/        # A regular expression literal

1.3. Punctuation

Ruby uses punctuation characters for a number of purposes. Most Ruby operators are written using punctuation characters, such as + for addition, * for multiplication, and || for the Boolean OR operation. Punctuation characters also serve to delimit string, regular expression, array, and hash literals, and to group and separate expressions, method arguments, and array indexes.

1.4. Identifiers

An identifier is simply a name. Ruby uses identifiers to name variables, methods, classes, and so forth. Ruby identifiers consist of letters, numbers, and underscore characters, but they may not begin with a number. Identifiers may not include whitespace or nonprinting characters, and they may not include punctuation characters except as described here.

Identifiers that begin with a capital letter A–Z are constants, and the Ruby interpreter will issue a warning (but not an error) if you alter the value of such an identifier. Class and module names must begin with initial capital letters. The following are identifiers:

i
x2
old_value
_internal                  # Identifiers may begin with underscores
PI                           # Constant


By convention, multiword identifiers that are not constants are written with underscores
like_this, whereas multiword constants are written LikeThis or LIKE_THIS.

Punctuation in identifiers
Punctuation characters may appear at the start and end of Ruby identifiers. They have the following meanings:

$    Global variables are prefixed with a dollar sign.
@   Instance variables are prefixed with a single at sign, and class variables are prefixed with two at signs.
?    As a helpful convention, methods that return Boolean values often have names that end with a question mark.
!    Method names may end with an exclamation point to indicate that they should be used cautiously.
=   Methods whose names end with an equals sign can be invoked by placing the method name, without the equals sign, on the left side of an assignment operator.

Here are some example identifiers that contain leading or trailing punctuation characters:

$files                  # A global variable
@data               # An instance variable
@@counter       # A class variable
empty?              # A Boolean-valued method or predicate
sort!                  # An in-place alternative to the regular sort method
timeout=            # A method invoked by assignment

 

1.5 Keywords

The following keywords have special meaning in Ruby and are treated specially by the Ruby parser:

__LINE__               case         ensure      not          then
__ENCODING__   class         false        or            true
__FILE__                def           for           redo        undef
BEGIN                    defined?   if              rescue     unless
END                        do           in              retry        until
alias                         else          module     return      when
and                          elsif          next         self           while
begin                        end          nil            super        yield
break

 

2. Syntactic Structure

The basic unit of syntax in Ruby is the expression. The Ruby interpreter evaluates expressions, producing values. The simplest expressions are primary expressions, which represent values directly.  Operators are used to perform computations on values, and compound expressions are built by combining simpler subexpressions with operators:

1                   # A primary expression
x                   # Another primary expression
x = 1             # An assignment expression
x = x + 1       # An expression with two operators


Expressions can be combined with Ruby’s keywords to create statements, such as the if statement for conditionally executing code and the while statement for repeatedly executing code:

if x < 10 then                   # If this expression is true
x = x + 1                         # Then execute this statement
end                                 # Marks the end of the conditional

2.1 Block Structure in Ruby

Ruby programs have a block structure. Module, class, and method definitions, and most of Ruby’s statements, include blocks of nested code. These blocks are delimited by keywords or punctuation and, by convention, are indented two spaces relative to the delimiters. There are two kinds of blocks in Ruby programs. One kind is formally called a “block.” These blocks are the chunks of code associated with or passed to iterator methods:

3.times { print "Ruby! " }


In this code, the curly braces and the code inside them are the block associated with the iterator method invocation 3.times. Formal blocks of this kind may be delimited with curly braces, or they may be delimited with the keywords do and end:

1.upto(10) do |x|
print x
end


do and end delimiters are usually used when the block is written on more than one line.

3. File Structure

There are only a few rules about how a file of Ruby code must be structured. These rules are related to the deployment of Ruby programs and are not directly relevant to the language itself.

First, if a Ruby program contains a “shebang” comment, to tell the (Unix-like) operating system how to execute it, that comment must appear on the first line.

Second, if a Ruby program contains a “coding” comment (as described in §2.4.1), that comment must appear on the first line or on the second line if the first line is a shebang.

Third, if a file contains a line that consists of the single token __END__ with no whitespace before or after, then the Ruby interpreter stops processing the file at that point. The remainder of the file may contain arbitrary data that the program can read using the IO stream object DATA. (See Chapter 10 and §9.7 for more about this global constant.)

Ruby programs are not required to fit in a single file. Many programs load additional Ruby code from external libraries, for example. Programs use require to load code from another file. require searches for specified modules of code against a search path, and prevents any given module from being loaded more than once.

The following code illustrates each of these points of Ruby file structure:
#!/usr/bin/ruby -w            shebang comment
# -*- coding: utf-8 -*-     coding comment
require 'socket'                load networking library
...                                    program code goes here
__END__                      mark end of code
...                                   program data goes here

4. Program Execution

Ruby is a scripting language. This means that Ruby programs are simply lists, or scripts, of statements to be executed. By default, these statements are executed sequentially, in the order they appear. Ruby’s control structures alter this default execution order and allow statements to be executed conditionally or repeatedly, for example.

Programmers who are used to traditional static compiled languages like C or Java may find this slightly confusing. There is no special main method in Ruby from which execution begins. The Ruby interpreter is given a script of statements to execute, and it begins executing at the first line and continues to the last line.

The Ruby interpreter is invoked from the command line and given a script to execute. Very simple one-line scripts are sometimes written directly on the command line. More commonly, however, the name of the file containing the script is specified. The Ruby interpreter reads the file and executes the script.


Continue Reading...

Working with irb : Interactive Ruby

irb stands for “Interactive Ruby.” “Interactive” means that as soon as you type something, your computer will immediately attempt to process it. Sometimes this sort of environment is called an immediate or interactive environment.

Start irb and make sure a prompt appears, like so:

irb(main):001:0>

This prompt is not as complex as it looks. All it means is that you’re in the irb program, you’re typing your first line (001), and you’re at a depth of 0. You don’t need to place any importance on the depth element at this time.
Type this after the preceding prompt and press Enter:

1 + 1

The result should come back quickly: 2. The entire process looks like this:

irb(main):001:0> 1 + 1
=> 2
irb(main):001:1>


Ruby is now ready to accept another command or expression from you.

Continue Reading...

Installing Ruby


Installing Ruby On Windows

When you install Ruby onto your computer, you’ll get the Ruby interpreter, the program that understands other programs written in the Ruby language, along with a collection of extensions and libraries to make your Ruby more fully featured. 

Ruby was initially designed for use under UNIX and UNIX-related operating systems such as Linux, but Windows users have access to an excellent “one-click installer,” which installs Ruby, a horde of extensions, a source code editor, and various documentation, in “one click.” Ruby on Windows is as reliable and useful as it is on other operating systems, and Windows makes a good environment for developing Ruby programs.
To get up and running as quickly as possible, follow these steps:

2. You’ll see a few links for different versions of Ruby you can download for Windows. Ideally, you want to download the file at the link that’s highest in the list that’s referred to as a “One-Click Installer.” .

3. Click the link from step 3 and run the downloaded file to launch the installer. The file is approximately 20 MB in size, so it may take a while to download.

4. If Windows gives you a “Security Error” box, click the “Run” button to give your approval.

5. Work your way through the installation screens.

6. Unless you have a specific reason not to, let the installation program install Ruby in its default location of  c:\ruby and its default program group.

7. Installation takes place when you see a stream of filenames flying up your screen. Wait several minutes for the installation process to complete, and enjoy the view. There are a lot of files to install!

8. Installation is complete when the installation program says “Installation Complete” and the “Next” button is clickable. Click the “Next” button, and then click “Finish” to exit the installation program.


Continue Reading...

Thursday, July 24, 2014

A tour of Ruby

A Tour of Ruby

This section is a guided tour through some of the most interesting features of Ruby. Everything discussed here will be documented in detail later in further posts, but this first look will give you the flavor of the language.

1. Ruby Is Object-Oriented

Ruby is a completely object-oriented language. Every value is an object, even simple numeric literals and the values true, false, and nil (it is Ruby’s version of null). Here we invoke a method named class on these values. Comments begin with # in Ruby, and the => arrows in the comments indicate the value returned by the commented code:

 1.class          # => Fixnum: the number 1 is a Fixnum
 0.0.class       # => Float: floating-point numbers have class Float
 true.class      # => TrueClass: true is a the singleton instance of TrueClass
 false.class     # => FalseClass
 nil.class        # => NilClass

In many languages, function and method invocations require parentheses, but there are no parentheses in any of the code above. In Ruby, parentheses are usually optional and they are commonly omitted, especially when the method being invoked takes no arguments. The fact that the parentheses are omitted in the method invocations here makes them look like references to named fields or named variables of the object. This is intentional, but the fact is, Ruby is very strict about encapsulation of its objects; there is no access to the internal state of an object from outside the object. Any such access must be mediated by an accessor method, such as the class method shown above.  

2. Blocks and Iterators

The fact that we can invoke methods on integers isn’t just an esoteric aspect of Ruby. It is actually something that Ruby programmers do with some frequency:

3.times { print "Ruby! " }    # Prints "Ruby! Ruby! Ruby! "
1.upto(9) { |x| print x }       # Prints "123456789"

times and upto are methods implemented by integer objects. They are a special kind of method known as an iterator, and they behave like loops. The code within curly braces -known as a block- is associated with the method invocation and serves as the body of the loop. The use of iterators and blocks is another notable feature of Ruby; although the language does support an ordinary while loop, it is more common to perform loops with constructs that are actually method calls.

The ability to associate a block of code with a method invocation is a fundamental and very powerful feature of Ruby. Although its most obvious use is for loop-like constructs, it is also useful for methods that only invoke the block once. For example:

File.open("data.txt") do |f|  # Open named file and pass stream to block
line = f.readline                 # Use the stream to read from the file
end                                  # Stream automatically closed at block end


Double-quoted strings can include arbitrary Ruby expressions delimited by #{ and }. 

 print "#{ a } + #{ b } = #{ a + b } "  # Note variables substituted into string
 
The value of the expression within these delimiters is converted to a string (by calling its to_s method, which is supported by all objects). The resulting string is then used to replace the expression text and its delimiters in the string literal. 


3 Expressions and Operators in Ruby

Ruby’s syntax is expression-oriented. Control structures such as if that would be called statements in other languages are actually expressions in Ruby. They have values like other simpler expressions do, and we can write code like this:

minimum = if x < y then x else y end

Here are examples of some commonplace and some more unusual Ruby operators:

1 + 2                                 # => 3: addition
1 * 2                                 # => 2: multiplication
1 + 2 == 3                        # => true: == tests equality
2 ** 1024                         # 2 to the power 1024: Ruby has arbitrary size ints
"Ruby! " * 3                      # => "Ruby! Ruby! Ruby! ": string repetition
"%d %s" % [3, "rubies"]    # => "3 Rubies": Python-style, printf formatting
max = x > y ? x : y            # The conditional operator


Many of Ruby’s operators are implemented as methods, and classes can define these methods however they want.

4 Methods

Methods are defined with the def keyword. The return value of a method is the value
of the last expression evaluated in its body:

def square(x)         # Define a method named square with one parameter x
x*x                       # Return x squared
end                      # End of the method


5 Assignment

The (non-overridable) = operator in Ruby assigns a value to a variable:

x = 1

Assignment can be combined with other operators such as + and -:

x += 1                # Increment x: note Ruby does not have ++.
y -= 1                 # Decrement y: no -- operator, either.


Ruby supports parallel assignment, allowing more than one value and more than one variable in assignment expressions:

x, y = 1, 2                   # Same as x = 1; y = 2
a, b = b, a                   # Swap the value of two variables
x,y,z = [1,2,3]             # Array elements automatically assigned to variables


Methods in Ruby are allowed to return more than one value, and parallel assignment is helpful in conjunction with such methods. For example:

# Define a method to convert Cartesian (x,y) coordinates to Polar
def polar(x,y)
theta = Math.atan2(y,x)               # Compute the angle
r = Math.hypot(x,y)                     # Compute the distance
[r, theta]                                      # The last expression is the return value
end


# Here's how we use this method with parallel assignment
distance, angle = polar(2,2)


6. Regexp and Range

A Regexp (regular expression) object describes a textual pattern and has methods for determining whether a given string matches that pattern or not. And a Range represents the values (usually integers) between two endpoints. Regular expressions and ranges have a literal syntax in Ruby:

/[Rr]uby/            # Matches "Ruby" or "ruby"
/\d{5}/               # Matches 5 consecutive digits
1..3                   # All x where 1 <= x <= 3
1...3                  # All x where 1 <= x < 3


Regexp and Range objects define the normal == operator for testing equality. In addition, they also define the === operator for testing matching and membership. Ruby’s case statement (like the switch statement of C or Java) matches its expression against each of the possible cases using ===, so this operator is often called the case equality operator. It leads to conditional tests like these:

# Determine US generation name based on birth year
# Case expression tests ranges with ===

generation = case birthyear
when 1946..1963: "Baby Boomer"
when 1964..1976: "Generation X"
when 1978..2000: "Generation Y"
else nil
end


7. Classes and Modules

A class is the definition of a single type of object. Class names in Ruby always start with a capital letter, so your programs will end up with classes with names like User, Person, Place, Topic, Message, and so forth.

class Person
attr_accessor :name, :age, :gender
end


8. Ruby Surprises

Every language has features that trip up programmers who are new to the language. Here we describe two of Ruby’s surprising features.

Ruby’s strings are mutable, which may be surprising to Java programmers in particular. The []= operator allows you to alter the characters of a string or to insert, delete, and replace substrings. The << operator allows you to append to a string, and the String class defines various other methods that alter strings in place.  Ruby’s conditionals and loops (such as if and while) evaluate conditional expressions to determine which branch to evaluate or whether to continue looping.

Conditional expressions often evaluate to true or false, but this is not required. The value of nil is treated the same as false, and any other value is the same as true. This is likely to surprise C programmers who expect 0 to work like false, and JavaScript programmers who expect the empty string "" to be the same as false.




Continue Reading...

Tuesday, July 22, 2014

About Ruby Language

Introduction to Ruby : 

        Ruby is a dynamic programming language with a complex but expressive grammar and a core class library with a rich and powerful API. Ruby draws inspiration from Lisp, Smalltalk, and Perl, but uses a grammar that is easy for C and Java programmers to learn. Ruby is a pure object-oriented language, but it is also suitable for procedural and functional programming styles. It includes powerful meta programming capabilities and can be used to create domain-specific languages or DSLs.

What Matz says :

I knew many languages before I created Ruby, but I was never fully satisfied with them. They were uglier, tougher, more complex, or more simple than I expected. I wanted to create my own language that satisfied me, as a programmer. I knew a lot about the language’s target audience: myself. To my surprise, many programmers all over the world feel very much like I do. They feel happy when they discover and program in Ruby.

Throughout the development of the Ruby language, I've focused my energies on making programming faster and easier. All features in Ruby, including object oriented features, are designed to work as ordinary programmers (e.g., me) expect them to work. Most programmers feel it is elegant, easy to use, and a pleasure to program.



Continue Reading...

Followers