You are here:Home » Ruby on Rails » Basics of Ruby programming Language

Basics of Ruby programming Language

In this part of the Ruby tutorial, we will cover basic programming concepts of the Ruby language. We introduce the very basic programs. We will work with variables, constants and basic data types. We will read and write to the console; we will mention variable interpolation.
We start with a very simple code example.
#!/usr/bin/ruby

puts "This is Ruby"
This is simple Ruby script. It will print "This is Ruby" message to the console.
#!/usr/bin/ruby
This is a path to the Ruby interpreter, which will execute the script.
puts "This is Ruby"
The puts is a Ruby keyword, which prints its argument to the terminal. In our case the argument is a string message, delimeted by double qoutes.
$ ./first.rb 
This is Ruby
Executing the script gives the above output.

We can read values from the terminal. (Terminal and console are synonyms)
#!/usr/bin/ruby

print "What is your name? "
name = gets

puts "Hello #{name}"
The second program will read a value from a console and print it.
print "What is your name? "
The print keyword prints a message to the console. The difference between the print and puts keywords is, that the print keyword does not start a new line. The puts keyword automatically starts a new line.
name = gets
Here we read an input from the user and store it in the name variable. The gets is a method, which in our case reads a line from the terminal. It is one of the methods, that we have at our disposal by default.
puts "Hello #{name}"
In this code line, we perform variable interpolation. Variable interpolationis replacing variables with their values inside string literals. Another names for variable interpolation are: variable substitution and variable expansion.
$ ./name.rb 
What is your name? Jan
Hello Jan
This is the output of the second program.

Ruby code can be run from the command line. This is inspired by Perl one-liners, where small fragments of code are run to do some petit tasks.
$ ruby -e "puts RUBY_VERSION"
1.9.3
The -e option tells Ruby to execute Ruby code specified on the line and not to search for a Ruby file name. Our example prints the version of the Ruby interpreter to the terminal.

Ruby interpreter has a -c option, which checks the syntax of the code. If this option is used, the code is not executed. If there is no syntax error, Ruby will print "Syntax OK" to the standard output.
#!/usr/bin/ruby

class Being end

m = Test.new
p m
In the above example, there is a syntax error. If we put class and end keywords on one line, we must also use the semicolon ; character.
$ ruby -c syntax_check.rb 
syntax_check.rb:3: syntax error, unexpected keyword_end, expecting '<' or ';' or '\n'
syntax_check.rb:6: syntax error, unexpected $end, expecting keyword_end
Syntax error was found. If we put a semicolon after the Being class, the error message will disappear.

Command line arguments

Ruby programs can receive command line arguments. They follow the name of the program, when we run it.
#!/usr/bin/ruby

puts ARGV
Command line arguments specified after the file name are available to a Ruby program in the global array named ARGV.
puts ARGV
Here we print all the command line arguments to the terminal.
$ ./args.rb 1 2 3
1
2
3
We provide three numbers as command line arguments and these are printed to the console.

In the following example, we wil print all arguments and also the script name.
#!/usr/bin/ruby

puts $0
puts $*
The $0 global variable contains the name of the script being executed. Global variables in Ruby begin with the $ character. The $* is another global variable. It is a synonym for the ARGV variable. It contains command line arguments given for the current script.
$ ./args2.rb Ruby Python Perl
./args2.rb
Ruby
Python
Perl
The args2.rb script receives three strings. The name of the scrip and the three arguments are printed to the terminal.

Variables and constants

A variable is a place to store data. A variable has a name and a data type. Data types are different types of values. For example integers, strings or floating point numbers. Ruby is a dynamic language. This means, that we do not have to (and cannot) declare a variable to be of a certain data type. Instead, the Ruby interpreter determines the data type at the moment of the assignment. Plus a variable can contain different values and also different types of values over time. This differs from languages, which are strongly types, Java, C or Pascal. Unlike variables, constants (should) retain their values. Once initialized, they cannot be modified. In Ruby however, it is possible to change the value of a constant. In such a case a warning is issued.
#!/usr/bin/ruby

city = "New York"
name = "Paul"; age = 35
nationality = "American"

puts city
puts name
puts age
puts nationality

city = "London"

puts city
In the above example, we work with four variables.
city = "New York"
We assign a string value to the city variable. The variable is dynamically created.
name = "Paul"; age = 35
We create two more variables. We can put two statements into one line. But for readability reasons, each statement should be rather on a separate line.
puts city
puts name
puts age
puts nationality
We print the values of the variables to the terminal.
city = "London"
We assign a new value to the city variable.
$ ./variables.rb 
New York
Paul
35
American
London
Output of the example.

Constants As we already said above, constants store one value over the time. Unlike in other languages, this rule is however not enforced in Ruby.
#!/usr/bin/ruby

WIDTH = 100
HEIGHT = 150

var = 40
puts var

var = 50
puts var

puts WIDTH
WIDTH = 110
puts WIDTH
In this example, we declare two constants and one variable.
WIDTH = 100
HEIGHT = 150
Constants in ruby begin with capital letter. It is a common practice to write all characters in uppercase letters.
var = 40
puts var

var = 50
We declare and initialize a variable. Later, we assign a new value to the variable. It is legal.
WIDTH = 110
We assign a new value to a constant. Constants should not be modified, once they are created. Otherwise it has no meaning to create a constant. The Ruby interpreter will issue a warning.
$ ./constants.rb 
40
50
100
./constants.rb:13: warning: already initialized constant WIDTH
110
Output of the script.

Variable interpolation

Variable interpolation is replacing variables with their values inside string literals. Another names for variable interpolation are: variable substitution and variable expansion.
#!/usr/bin/ruby

age = 34
name = "William"

puts "#{name} is #{age} years old"
In Ruby, strings are immutable. We cannot modify an existing string. Variable interpolation happens during string creation.
age = 34
name = "William"
Here we declare two variables.
puts "#{name} is #{age} years old" 
The string message has double quotes as its boundaries. When we put a variable name between the #{ and } characters, the variable is interpolated, that is replaced with its value.
$ ./interpolation.rb 
William is 34 years old
Output.
This chapter covered some basics of the Ruby language.

0 comments:

Post a Comment