logo codesdope

puts and print


Let's print using print


print "Hello World!"
Output
Hello World!
print "Codes"+"Dope"
Output
CodesDope

Difference between puts and print


The only difference between puts and print is that puts automatically prints a new line after the end of a sentence whereas print doesn't.

print "CodesDope"
print "CodesDope"
puts "CodesDope"
puts "CodesDope"
Output
CodesDopeCodesDopeCodesDope
CodesDope

You can see that in the above example, there is no new line printed after "CodesDope" written in print but there is a new line after "CodesDope" in puts.

Printing in multiline


What if someone has to print in two or three lines and not just in a single line?
Ruby provides the simplest way to do this. Just write your stuff in between (""" """) and that's it. Let's see an example.

puts"""Once there was a boy.
He wanted to be a programmner.
He found Ruby.
Now, he is a programmer.
"""
Output
Once there was a boy.
He wanted to be a programmner.
He found Ruby.
Now, he is a programmer.

Other ways


We can also use "\n" ( newline character ) to print a new line whenever we want as used in most of the programming languages. Let's see an example.

puts "Hello\nWorld\nCodesDope\nYes, newline"
Output
Hello
World
CodesDope
Yes, newline

\n is the newline character. It prints a new line.
In the above example, when puts encountered H, it printed H, when e then e. Similarly, when it encountered \n, it printed a new line and that's it.

One more thing you should know is "\t". \t prints a tab space. Let's see how:

puts "Hello World\n\tCodesDope"
Output
Hello World
        CodesDope

I hope it is clear by the above example.

Commenting


You can also include something in your code which won't be compiled and your computer will simply ignore that while running your code. Comment is written to make your code more readable. Comments are written after '#'. Comments written after '#' are single line comments. New line will not be a part of the comment

# I am using a variable x
# This is another comment
x = 10
puts "#{x}"
Output
10
comment in ruby

Why use comments?


As mentioned earlier, it makes your code more readable. Assume that you have written a software and after releasing it, you hired a few good programmers for its maintenance. Without comments, it would be a very difficult job for them to understand your code. Most of the time it happens that the person who has written a code is not the one who is going to modify it. So, make it a habit of writing comments.

Types of variable


Here, we will discuss the different types of variables that Ruby uses. See this example first.

a = 5
b = "5"
c = 5.0
d = 2.3
e = "Hello"
puts "#{a.class}\n#{b.class}\n#{c.class}\n#{d.class}\n#{e.class}"
Output
Fixnum
String
Float
Float
String

.class gives us class of anything. For now, just keep in mind that it can be understood as type of any thing e.g. - in maths, 1 is of type integer, 2.3 is of type decimal, etc; ice-cream if of type desert, juice is of type drink, etc. Now let's go through all the classes printed in the previous example one by one.

Fixnum : It is just a simple integer. e.g.- 3, 5, 10 ,100, etc are Fixnum.

String : String means a collection of characters. In simple English, it is a letter, word, sentence or collection of sentences. You will go through a whole topic on string. So, leave it for that time if you are getting confused and just keep in mind that strings are characters, words or sentences and these are written in between ' ' or " ". Since Hello and 5 written between " " i.e. "Hello" and "5" so, they are strings.

Float : You can understand Float as decimal numbers in maths. 2.3, 4.4, 2.9, etc all are float. One most important thing you should notice here is that 5.0 is Float whereas, 5 is Fixnum.

5 is Fixnum
5.0 is Float
"5" is String
Programming is all about practice and solving problems. You must solve questions after progressing through the chapters from practice section.

You are only good as the chances you take.
-Al Pacino

Share what you know
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.