1

In Ruby, I'm trying to solve a quiz as like a FizzBuzz Challenge. My question is "How I can print the integer number |n| adding a comma and space(", ")at the end of they?"

To separate the Namaand Team strings I'm using $stdout.print "Team, " && $stdout.print "Nama, ". But for the integers my syntax is $stdout.print n:

image

Code right now:

puts "Enter the maximum amount of numbers"
print ">"
upper_limit = gets.chomp.to_i

(1..upper_limit).each do |n|

  if n % 35  == 0
    $stdout.print "NamaTeam"

  elsif n % 7 == 0
    $stdout.print "Team, "

  elsif n % 5 == 0
    $stdout.print "Nama, "

  else
    $stdout.print n
  end
end

I already tried to use .join(' ') and .split(' ') methods but they don't work with integer numbers D:

Best Regards for the community!

Gerry
  • 10,337
  • 3
  • 31
  • 40
gbs0
  • 13
  • 4

2 Answers2

0

With string interpolation:

$stdout.print "#{n}, "
Tom Lord
  • 27,404
  • 4
  • 50
  • 77
0

Just out of curiosity; another solution would be to construct the whole line by joining the array:

puts (1..35).map { |n| 
  if n % 35 == 0 then "NamaTeam"
  elsif n % 7 == 0 then "Team"
  elsif n % 5 == 0 then "Nama"
  else n end 
}.join(', ')
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160