Showing posts with label About Ruby. Show all posts
Showing posts with label About Ruby. Show all posts

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...

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