Ok, I have never used Ruby before and I'm trying to figure out how to convert my following Perl code into Ruby to do exactly what is does now.

Basically I used a loop structure to produces the following output (user can specify how many lines need to be printed):

A
AA
AAA
AAAB
AAABA
AAABAA
AAABAAA
AAABAAAB

#!/usr/bin/perl

print "Please enter your name: ";

$name = <STDIN>;

print "Please enter your favorite number: ";

$f = <STDIN>;
$a = 0;

if ($f < 0)
{
 while ($f <0)
 {
 print "Please enter a positive number: ";
 $f =<STDIN>;
 } 
}

{
 while ($a < $f)
 {
 print $name;
 $b = $a + 1;
 $a = $b;
 }
}

Recommended Answers

All 3 Replies

Your text example doesn't really show how the script you provide works. Here's what you are probably after though - this could be a bit more robust but in this state it's clear:

#!/usr/bin/env/ruby

#first get the name
puts "Enter your name"
name =  gets
#now get a number, to_i returns the entered string (eg "5" to an integer eg 5)
puts "Enter a number"
number = gets.to_i

#times is a method on integer, we can use it to iterate through the block
number.times do 
  #print the name
  puts name
end

use gets.chomp to remove any trailing after space from the input

I also used this link to start ruby - seems to be helping awesomely for beginners.
http://pine.fm/LearnToProgram/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.