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.
We can read values from the terminal. (Terminal and console are synonyms)
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 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.
In the following example, we wil print all arguments and also the script name.
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.
This chapter covered some basics of the Ruby language.
We start with a very simple code example.
#!/usr/bin/rubyThis is simple Ruby script. It will print "This is Ruby" message to the console.
puts "This is Ruby"
#!/usr/bin/rubyThis 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.rbExecuting the script gives the above output.
This is Ruby
We can read values from the terminal. (Terminal and console are synonyms)
#!/usr/bin/rubyThe second program will read a value from a console and print it.
print "What is your name? "
name = gets
puts "Hello #{name}"
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 = getsHere 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.rbThis is the output of the second program.
What is your name? Jan
Hello Jan
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"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.
1.9.3
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/rubyIn 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.
class Being end
m = Test.new
p m
$ ruby -c syntax_check.rbSyntax error was found. If we put a semicolon after the Being class, the error message will disappear.
syntax_check.rb:3: syntax error, unexpected keyword_end, expecting '<' or ';' or '\n'
syntax_check.rb:6: syntax error, unexpected $end, expecting keyword_end
Command line arguments
Ruby programs can receive command line arguments. They follow the name of the program, when we run it.#!/usr/bin/rubyCommand line arguments specified after the file name are available to a Ruby program in the global array named
puts ARGV
ARGV
. puts ARGVHere we print all the command line arguments to the terminal.
$ ./args.rb 1 2 3We provide three numbers as command line arguments and these are printed to the console.
1
2
3
In the following example, we wil print all arguments and also the script name.
#!/usr/bin/rubyThe $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.
puts $0
puts $*
$ ./args2.rb Ruby Python PerlThe args2.rb script receives three strings. The name of the scrip and the three arguments are printed to the terminal.
./args2.rb
Ruby
Python
Perl
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/rubyIn the above example, we work with four variables.
city = "New York"
name = "Paul"; age = 35
nationality = "American"
puts city
puts name
puts age
puts nationality
city = "London"
puts city
city = "New York"We assign a string value to the city variable. The variable is dynamically created.
name = "Paul"; age = 35We 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 cityWe print the values of the variables to the terminal.
puts name
puts age
puts nationality
city = "London"We assign a new value to the city variable.
$ ./variables.rbOutput of the example.
New York
Paul
35
American
London
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/rubyIn this example, we declare two constants and one variable.
WIDTH = 100
HEIGHT = 150
var = 40
puts var
var = 50
puts var
puts WIDTH
WIDTH = 110
puts WIDTH
WIDTH = 100Constants in ruby begin with capital letter. It is a common practice to write all characters in uppercase letters.
HEIGHT = 150
var = 40We declare and initialize a variable. Later, we assign a new value to the variable. It is legal.
puts var
var = 50
WIDTH = 110We 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.rbOutput of the script.
40
50
100
./constants.rb:13: warning: already initialized constant WIDTH
110
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/rubyIn Ruby, strings are immutable. We cannot modify an existing string. Variable interpolation happens during string creation.
age = 34
name = "William"
puts "#{name} is #{age} years old"
age = 34Here we declare two variables.
name = "William"
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.rbOutput.
William is 34 years old
This chapter covered some basics of the Ruby language.
0 comments:
Post a Comment