You are here:Home » Ruby on Rails » Step By Step Ruby programming Tutorials

Step By Step Ruby programming Tutorials

In this part of the Ruby tutorial, we will introduce the Ruby programming language.

Goal

The goal of this tutorial is to get you started with the Ruby programming language. The tutorial covers the core of the Ruby language. Variables, expressions, collections, control structures and other core features. It also describes some more advanced concepts like object-oriented programming, or regular expressions. It is not a complete coverage of the language. The tutorial was created on Ubuntu Linux.

Ruby

Ruby logo Ruby is a dynamic, reflective, general-purpose object-oriented programming language. The original author is a Japanese programmer Yukihiro Matsumoto. Ruby first appeared in 1995.
Ruby supports various programming paradigms. This includes object orientation, reflection, imperative and reflective programming. Ruby language was influenced primarily by Perl, Smalltalk, Eiffel, and Lisp. Unlike languages like Java, C# or C, Ruby has no official specification. Instead the original C implementation of the Ruby language serves as a de facto reference. There are other implementations of the Ruby language like JRuby, IronRuby or MacRuby.
The official web site is ruby-lang.org.

Popularity

There are hundreds of programming languages in use today. Ruby belongs to the most popular ones. The langpop.com and tiobe sites put Ruby around the 10th place. Ruby on Rails, a very popular web application framework is the first killer application created in Ruby.

Interactive interpreter

We can run Ruby statements in a script or in an interactive interpreter. In this tutorial, we will use the interactive Ruby session to demonstrate some smaller code fragments. Larger code examples are to be put in Ruby scripts.
$ irb
irb(main):001:0> puts RUBY_VERSION
1.8.7
=> nil
This is an example of the Ruby interactive session. We print the value of a special RUBY_VERSION constant to the console. It is set to the version of the current Ruby in use.

Ruby scripts

We will have our first simple example of a Ruby script.
#!/usr/bin/ruby

# first.rb

puts "This is Ruby"
In this script, we print a message to the console.
#!/usr/bin/ruby
Every script in the UNIX starts with a shebang. The shebang is the first two characters in the script: #!. The shebang is followed by the path to the interpreter, which will execute our script. The /usr/bin/ is the most common location for the Ruby interpreter. It could also be located in /usr/local/bin/ or elsewhere.
# first.rb 
Comments in Ruby are preceded by a # character.
puts "This is Ruby"
The puts method prints a string to the console.
$ which ruby
/usr/bin/ruby
The path to the Ruby interpreter can be found using the which command.
$ chmod +x first.rb 
$ ./first.rb
This is Ruby
We make the script executable with the chmod command. And execute it.

Sources

The following sources were used to create this tutorial:
In this part of the Ruby tutorial, we have introduced the Ruby language.

0 comments:

Post a Comment