Home > Net >  I am very new to programming. I wrote this Ruby script and realized that there is no space between t
I am very new to programming. I wrote this Ruby script and realized that there is no space between t

Time:04-18

character_name = "John"
character_age = "35"
puts ( "There was a dude named"   character_name )
puts ( "he was"    character_age   "years old." )
puts ( "He really liked the name"    character_name )
puts ( "but didnt like being"    character_age    "." )

The output of the program given up

CodePudding user response:

I assume that your question is how to put the spaces in. The answer is to... put the spaces in! :)

character_name = "John"
character_age = "35"
puts ( "There was a dude named "   character_name )
puts ( "he was "    character_age   " years old." )
puts ( "He really liked the name "    character_name )
puts ( "but didnt like being "    character_age    "." )

CodePudding user response:

You can use string interpolation and format string as you need

character_name = "John"
character_age = 35

puts "There was a dude named #{character_name}"
puts "he was #{character_age} years old."
puts "He really liked the name #{character_name}"
puts "but didnt like being #{character_age}."

It allows you to make some sort of templates

It is clearer, better readable than string concatenation. It also doesn't produce unnecessary intermediate string objects

  •  Tags:  
  • ruby
  • Related