My personal blog around GNU/Linux

Ruby Tips [Iteration]

I am sure you will be Jaw dropping after looking at this code.

How many lines will it take in Ruby to print your name ‘n’ number of times?

The answer is 1 line. Oh.. is that so….!!! can you show me how it is? Yes here it is.

10.times { puts “Your_name\n” }

save this file as filename.rb and run from terminal like ” ruby filename.rb “. Oooo.. its great…. am wondered..

In the similar way if you want to print numbers from 1 to 100 or any range try this:

i=0

100.times { puts (i=i+1).to_s + “\n” }

just two lines.. very simple

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: