Getting Started with Ruby

Print your name :)

Now it is time to write code. I will start by writing my name. Start a new project and make a new file with extension .rb. Write down the following code:

  1. puts 'hello harish'

On running this program, you will get 'hello harish' in the console. 'puts' is a method which is used to print in the console. You can print numbers, floats etc. with this method.

  1. puts 'hello harish'
  2. puts 3
  3. puts 3.4
  4. puts 'name'+' space harish'

Note that fourth line is adding 2 different texts with '+'. Try to change the text and observer results. NOTE: puts method change line every time after printing. If you don't want to change line then use print method.

  1. print 'name' # no line break
  2. print 'name' # no line break
  3. puts 'name' # line break
  4. puts 'name' # line break

Result of above code is:

  1. namenamename
  2. name

Printing Multiple lines:

In all above examples, we are printing single lines. Now we want to print multiple line say a paragraph. For this purpose we will use EOF statements.

  1. print <<EOF
  2. this is a multiple line text called string
  3. a simple text is called string
  4. this is line three written in EOF
  5. EOF
  6. print <<"EOF"; # same as above
  7. This is the second way of creating multiple line string
  8. Try it yourself and observe the results
  9. EOF

The output of above code is:

  1. this is a multiple line text called string
  2. a simple text is called string
  3. this is line three written in EOF
  4. This is the second way of creating multiple line string
  5. Try it yourself and observe the results

Comment

Comment is the code which are written for understanding code later. These are not executed by compiler. You can assume them as notes which only for humans which will be ignored by machines. You may ask why we need comments. In complex programs, it is not possible to remember whole code and to understand code written by other programmers by directly reading the code. The comments make work easy and work like an instructor. Single line comment starts with # and multiline comment starts with =begin and ends with =end.

  1. # this is a comment
  2. # This will be ignored by machine
  3. # this file is a demo file
  4. =begin
  5. This is a comment.
  6. This is a comment, too.
  7. This is a multiline comment
  8. This will be also ignored
  9. =end
  10. puts 'comments'

Begin and End statements:

In all above cases you see that code is executed line by line, but we can change the sequence by using begin and end statements. When a program runs first of all code inside the begin statement executes and after it code execute line by line. Similarly code inside the end statements execute after all other code. For example:

  1. END {
  2. puts "This line is inside the end statement"
  3. }
  4. puts "this is first line"
  5. BEGIN {
  6. puts "this line is inside begin statement"
  7. }

Now you will get result:

  1. this line is inside begin statement
  2. this is first line
  3. This line is inside the end statement

So sequence is begin code >> other code >> end code.
[000webhost_bottom_banner]



About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

Ruby is a pure object oriented, general purpose,interpreted and server side scripting and programming language. Ruby ...
Introduction to Ruby
In this step by step free tutorials we will learn about printing, comments, puts, print, begin, end and EOF methods f...
Getting Started with Ruby
In this tutorial of free step by step free tutorials series we read about variables, constant, printing in staring an...
Variables in Ruby

Login or Sign up to leave comment.