Close
Close

Printing in Ruby


I am assuming that you have gone through the introduction section and know how to write and compile Ruby codes.

Now, let's print something on screen with Ruby :

puts "Hello World!"
Output
Hello World!

And it is done.

It is quite simple. Isn't it?

Let's print something more :

puts "I want Ruby"
puts "Ruby Ruby Ruby"
puts "Programming is fun"
Output
I want Ruby
Ruby Ruby Ruby
Programming is fun

Yes, it is simple. You just have to write the matter which you want to print on the screen in between ( " " ) after puts and it is done.

Let's do something more advanced


x = 10
puts "The value of x is #{x}"
Output
The value of x is 10

I hope you must have got this by just an example but let me explain you this once also.

x = 10 : It means that we are taking a variable x and giving it a value of 10.
puts "The value of x is #{10}"
#{x} : Notice that in place of #{x}, the value of x got printed on the screen i.e. 10.
It is because whatever we write in "#{}" is first evaluated and then its value gets printed on the screen.

See some more examples to understand this :

x = 10
y = 5
puts "The sum of #{x} and #{y} is #{x+y}"
Output
The sum of 10 and 5 is 15

As discussed earlier, x = 10 means that we are taking a variable x and giving it a value 10.
In the same way,
y = 5 means that 'y' is 5.

print "The sum of #{x} and #{y} is #{x+y}" : Here, there is nothing special with "sum of", so it got printed as it is. In place of #{x} and #{y}, the respective values of x and y got printed respectively.
And instead of #{x+y}, 15 got printed. As I mentioned earlier, whatever we write within #{} is evaluated first. So, x+y got evaluated first
x is 10
y is 5
So, x+y is equivalent to 10+5 i.e. 15.
So, 15 was printed on the screen.

puts in ruby
x = 10
y = 5
puts "The product of #{x} and #{y} is #{x*y}"
Output
The sum of 10 and 5 is 50

Let's have some fun


puts "."*10
Output
..........
puts "Fun"*3
Output
FunFunFun

The above code should mean "Fun"+"Fun"+"Fun". So, let's try this :

puts "Codes"+"Dope"
Output
CodesDope
'*' in ruby

Yes, it is quite simple. Keep in mind that these things are of great use in real programming.

Let's do some error analysis


This part is only to teach you how to know the error if you are getting one.
What if we type this :

put "CodesDope"

Here, we are trying to teach you, how to fix an error, if you got one.

a.rb:1:in `<main>': undefined method `put' for main:Object (NoMethodError)
Did you mean?  puts
                     putc

a.rb:1: -> a.rb is just the filename. It will be same as your filename.
:1: is the line in which error has occurred. In our code, the error is in line 1.
undefined method `put' for main:Object (NoMethodError) : put is nothing for our program, that's why we got this error.
Go back to your code and to that line and fix the error by changing put to puts.

In future, when writing long codes, make sure that you run your code from time to time while writing instead of completing your whole code and running at last. This will make debugging your code easier if you have made some errors.

Keywords


BEGIN END __ENCODING__
__END__ __FILE__ __LINE__
alias begin break
case class def
defined? do else
elsif end ensure
false for if
in module next
nil not or
redo rescue retry
return self super
self then true
undef unless until
when while yeild

These are the words which are reserved for Ruby and we can't use them for our variables. ( Try using any of these and see the error you are getting. )

Without practice, your knowledge is poison.
-Chanakya


Ask Yours
Post Yours
Doubt? Ask question