0

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;
 }
}
4
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by tiger86
0

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
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.