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 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.

Click on banner below to Learn: PHP with Laravel for beginners - Become a Master in Laravel

About Harish Kumar

Harish, a technical core team member at with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and business topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Full-Stack Web Development with React

Best Udemy Courses

Top Posts

Recent Posts

The Complete Web Developer Course - Build 25 Websites


Subscribe now and get weekly updates directly in your inbox!

Any Course on Udemy in $6 (INR 455)

Development Category (English)300x250