Yukihiro Matsumoto (Matz), Creator of Ruby
Yukihiro Matsumoto (Matz), Creator of Ruby
We have got different languages used for testing, in those languages ‘Ruby’ is one
of the language. Ruby is evolving scripting language, in which Object Oriented Techniques are
used. It has become one of the prominent scripting languages. It is flexible and considers everything
as an object and also it is highly portable.
Ruby was released for the very first time on 24th Feb, 1993 by Yukihiro
Matsumoto. Yukihiro actually wanted to create a fresh programming language that could provide a
balance between functional programming and imperative programming. As stated by Yukihiro, he
wanted to have a scripting language that is more powerful and useful as compared to Perl and
provide more object oriented features as compared to Python. Because of the above two reasons, he
developed Ruby.
After then, the first publication of Ruby was released with the name of Ruby
0.95 on December 21, 1995. Ruby 1.0 was then released on December 25, 1996 and the process of
improving Ruby went on and on. Recently, in June 2008, the latest version of Ruby was released;
Ruby 1.8.7. According to Yukihiro, the next stable version of Ruby would be the 1.9.1 which will
introduce Block local variables, character encodings and much more.
However, there are some differences as well between the syntax of Ruby and
Perl. For instance, variables in Perl are not mandatorily prefixed with a sigil as in case of Perl. On
the other hand, there are many similarities as well like class definitions or method definitions are
signaled using keywords in both Perl and Ruby. At the end, we have presented one of the simplest
examples for any programming language; the hello world example. This example is meant to show
you a difference between Ruby and Perl. For instance, if you want to output hello world using
Ruby, you have to write the following piece of code
Another major difference between Ruby and Perl/Python is that all the instances of
any variable are kept completely private to the class only, in case of Ruby which is not the case in
Perl or Python. For more details about the syntax and semantics of Ruby programming language,
please refer to our section Top 3 Learning Resources for Ruby & Recommended forums for Ruby
which will assist you in finding great online learning resources that you can use to learn more about
Ruby.
How the Ruby is going to find out everything in terms of Objects and Classes?
OBJECT:
Objects are key to understanding object-oriented technology. Look around right now
and you'll find many examples of real-world objects: your dog, your desk, your television set, your
bicycle.
Hold on your breathe for a while, to observe the real-world objects that are in your
immediate area. For each object that you see, ask yourself two questions: "What possible states can
this object be in?" and "What possible behavior can this object perform?” Make sure to write down
your observations. As you do, you'll notice that real-world objects vary in complexity; your desktop
lamp may have only two possible states (on and off) and two possible behaviors (turn on, turn off),
but your desktop radio might have additional states (on, off, current volume, current station) and
behavior (turn on, turn off, increase volume, decrease volume, seek, scan, and tune). You may also
notice that some objects, in turn, will also contain other objects
CLASSES:
In terms of OOPS (Object Oriented Programming) Class is said to be the blue print
of the Object. At basic we can say that a Class is a Construct that is used as a blue print ( or
template) to create objects of particular class. As we discussed earlier, this blue print (Class) will
also describes about the state and behavior of the objects. Here an object for a given class is called
an instance of the class. The Blue Print (Class) that contains (and also it is used to create) that
instance can be considered as the type of that object.
A class usually represents a noun, such as a person, place or (possibly quite abstract) thing - it is a
model of a concept within a computer program. Fundamentally, it encapsulates the state and
behavior of the concept it represents. It encapsulates state through data placeholders called
attributes (or member variables or instance variables); it encapsulates behavior through reusable
sections of code called methods.
And now you people will be in a confused state as we discussed Ruby is a Scripting
language and Why and what is the need of Objects and Classes?
For this query we have been said that its Scripting language which uses Object Oriented
Techniques in its programming. So in order to get some idea about what we are doing its been
discussed in earlier stages here.
Ruby has exception handling features, like Java or Python, to make it easy to handle errors.
Ruby features a true mark-and-sweep garbage collector for all Ruby objects. No need to
maintain reference counts in extension libraries. As Matz says, “This is better for your
health.”
Writing C extensions in Ruby is easier than in Perl or Python, with a very elegant API for
calling Ruby from C. This includes calls for embedding Ruby in software, for use as a
scripting language. A SWIG interface is also available.
Ruby can load extension libraries dynamically if an OS allows.
Ruby features OS independent threading. Thus, for all platforms on which Ruby runs, you
also have multithreading, regardless of if the OS supports it or not, even on MS-DOS!
Ruby is highly portable: it is developed mostly on GNU/Linux, but works on many types of
UNIX, Mac OS X, Windows 95/98/Me/NT/2000/XP, DOS, BeOS, OS/2, etc.
If you are familiar with the Object Oriented Programming, Ruby should become your second nature
of programming. If you've struggled with learning object-oriented programming or are not familiar
with it, Ruby should make more sense to you than other object-oriented languages because Ruby's
methods are consistently named, concise, and generally act the way you expect.
CHAPTER1. STRINGS
Let’s start working with Ruby now. In my opinion to start any programming
language STRINGS are the best to start with. The Strings in ruby are as much similar to the strings
that are used in JAVA, C and some other dynamic languages like Perl, PHP and PHYTON. Ruby
Strings are Dynamic, Mutable and Flexible. To get started using Strings in Ruby type a simple
sentence into your interactive Ruby Session.
When you click Enter, you should get an output like this
è “Hello Matz”
In Ruby everything you declare to a variable is an object. And to interact with Ruby to little
extent let’s talk about the classes and objects with the above example. Here, the variable string
points to an object of class String. That class defines over a hundred built-in methods: named
pieces of code that examine and manipulate the string. So, why we need to wait let’s start exploring
the strings with some different examples.
Expression substitution is a means of embedding the value of any Ruby expression into a string
using #{ and }:
x, y, z = 12, 36, 72
puts "The value of x is #{ x }."
puts "The sum of x and y is #{ x + y }."
puts "The average was #{ (x + y + z)/3 }."
And the out put for the above statements will be
The value of x is 12.
1. Str % arg
Formats a string using a format specification. arg must be an array if it contains more than one
substitution. For information on the format specification. See printf under "Kernel Module."
2. Str * integer
Returns a new string containing integer times str. In other words, str is repeated integer.
3. Str + other_str
Concatenates other_str to str.
6. Str == obj
Tests str and obj for equality. If obj is not a String, returns false; returns true if str <=> obj returns 0.
7. Str =~ obj
Matches str against a regular expression pattern obj. Returns the position where the match starts;
otherwise, false.
8. Str =~ obj
Matches str against a regular expression pattern obj. Returns the position where the match starts;
otherwise, false.
9. Str.capitalize
Capitalizes a string.
10. Str.capitalize!
Same as capitalize, but changes are made in place.
11. Str.casecmp
Makes a case-insensitive comparison of strings.
12. str.center
Centers a string.
13. Str.chomp
Removes the record separator ($/), usually \n, from the end of a string. If no record separator exists,
does nothing.
14. Str.chomp!
Same as chomp, but changes are made in place.
15. Str.chop
Removes the last character in str.
16. Str.chop!
Same as chop, but changes are made in place.
22. Str.downcase
Returns a copy of str with all uppercase letters replaced with lowercase.
23. Str.downcase!
Same as down case, but changes are made in place.
24. Str.dump
Returns a version of str with all nonprinting characters replaced by \n notation and all special
characters escaped.
28. Str.empty?
Returns true if str is empty (has a zero length).
29. str.eql?(other)
Two strings are equal if they have the same length and content.
32.str[fixnum] = fixnum [or] str[fixnum] = new_str [or] str[fixnum, fixnum] = new_str [or]
str[range] = aString [or] str[regexp] =new_str [or] str[regexp, fixnum] =new_str [or]
str[other_str] = new_str ]
Replace (assign) all or part of a string. Synonym of slice!.
34. Str.hash
Returns a hash based on the string's length and content.
35. str.hex
Treats leading characters from str as a string of hexadecimal digits (with an optional sign and an
optional 0x) and returns the corresponding number. Zero is returned on error.
39. Str.inspect
Returns a printable version of str, with special characters escaped.
41. Str.length
Returns the length of str. Compare size.
43. Str.lstrip
Returns a copy of str with leading whitespace removed.
44. Str.lstrip!
Removes leading whitespace from str, returning nil if no change was made.
46. Str.oct
Treats leading characters of str as a string of octal digits (with an optional sign) and returns the
corresponding number. Returns 0 if the conversion fails.
48. Str.reverse
Returns a new string with the characters from str in reverse order. 49str.reverse!
Reverses str in place.
Returns the index of the last occurrence of the given substring, character (fixnum), or pattern
(regexp) in str. Returns nil if not found. If the second parameter is present, it specifies the position
in the string to end the search. Characters beyond this point won't be considered.
If integer is greater than the length of str, returns a new String of length integer with str right-
justified and padded with padstr; otherwise, returns str.
52. Str.rstrip
Returns a copy of str with trailing whitespace removed.
53. Str.rstrip!
Removes trailing whitespace from str, returning nil if no change was made.
If pattern is a String, then its contents are used as the delimiter when splitting str. If pattern is a
single space, str is split on whitespace, with leading whitespace and runs of contiguous whitespace
characters ignored.
If pattern is a Regexp, str is divided where the pattern matches. Whenever the pattern matches a
zero-length string, str is split into individual characters.
If pattern is omitted, the value of $; is used. If $; is nil (which is the default), str is split on
whitespace as if ` ` were specified.
If the limit parameter is omitted, trailing null fields are suppressed. If limit is a positive number, at
most that number of fields will be returned (if limit is 1, the entire string is returned as the only
entry in an array). If negative, there is no limit to the number of fields returned, and trailing null
fields are not suppressed.
58. Str.squeeze!([other_str]*)
Squeezes str in place, returning either str, or nil if no changes were made.
59. Str.strip
Returns a copy of str with leading and trailing whitespace removed.
60. Str.strip!
Removes leading and trailing whitespace from str. Returns nil if str was not altered.
67. Str.swapcase!
Equivalent to String#swapcase, but modifies the receiver in place, returning str, or nil if no changes
were made.
68. Str.to_f
Returns the result of interpreting leading characters in str as a floating-point number. Extraneous
characters past the end of a valid number are ignored. If there is not a valid number at the start of
str, 0.0 is returned. This method never raises an exception.
69. str.to_i(base=10)
Returns the result of interpreting leading characters in str as an integer base (base 2, 8, 10, or 16).
Extraneous characters past the end of a valid number are ignored. If there is not a valid number at
the start of str, 0 is returned. This method never raises an exception.
74.str.tr_s!(from_str, to_str)
Performs String#str_ processing on str in place, returning str, or nil if no changes were made.
76. Str.upcase
Returns a copy of str with all lowercase letters replaced with their uppercase counterparts. The
operation is locale insensitive. Only characters a to z are affected.
77. Str.upcase!
Changes the contents of str to uppercase, returning nil if no changes are made.