Forum: Ruby How to 'space' certain things apart?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B45708fcc743000fb4eba027d7c00c8d?d=identicon&s=25 JB (Guest)
on 2006-03-29 09:59
(Received via mailing list)
Hi,

  I'm trying to learn a programming language and this one came up in my
searches as a fairly easy one. I've never taken any courses and have
absolutely zero knowledge of programming or scripting. The main reason I
want
to learn a language is just to simply make my own scripts for a MUD I
play
online, heh.

  Anyway, I found a place to at least get me started and I had to do
this
program below. The problem I'm having is that at the end, it shows the
persons name as, example: JimmySBrown instead of Jimmy S Brown. How do I
get
it to add the two spaces I need?

puts  'Please tell me your first name.'
first_name = gets.chomp
puts 'Please tell me your middle initial.'
middle_initial = gets.chomp
puts 'Please tell me your last name now.'
last_name = gets.chomp
var1 = first_name
var2 = middle_initial
var3 = last_name
name = var1.to_s + var2.to_s + var3.to_s
puts 'Pleased to meet you, ' + name + '!'
Ced5fff44ff8929fc974012ea108b284?d=identicon&s=25 vsv (Guest)
on 2006-03-29 10:14
(Received via mailing list)
1. you don't need extra variables var, var2, var2 - you already have
all required values in variables  first_name,  middle_initial,
last_name;
2. value stored in var1 varable is already a String - don't need to
convert it with var1.to_s;
3. to build name value you can use:
    name = first_name + ' ' + middle_initial + ' ' + last_name
or
    name = "#{first_name} #{middle_initial} #{last_name}"
or
    name = format( "%s %s %s", first_name, middle_initial, last_name )
or (same as previous)
    name = "%s %s %s" % [ first_name, middle_initial, last_name ]

don't be confused by so many ways to make it, just pick one and use it;
happy rubying!

Sergey
23172b6630dc631a134c9bad2fec2a39?d=identicon&s=25 ChrisH (Guest)
on 2006-03-29 10:15
(Received via mailing list)
JB wrote:
> persons name as, example: JimmySBrown instead of Jimmy S Brown. How do I get
> var3 = last_name
> name = var1.to_s + var2.to_s + var3.to_s
> puts 'Pleased to meet you, ' + name + '!'

You picked a good starter language to learn!

No need to create those extra vars:

name = "#{first_name} #{middle_initial} #{last_name}"

Note the spaces put in between the values.

Cheers
B45708fcc743000fb4eba027d7c00c8d?d=identicon&s=25 JB (Guest)
on 2006-03-29 11:34
(Received via mailing list)
ChrisH wrote:

  <snip>

  Thanks CrisH and vsv. That got me through it better.
This topic is locked and can not be replied to.