How to 'space' certain things apart?


#1

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 + ‘!’


#2
  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


#3

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


#4

ChrisH wrote:

Thanks CrisH and vsv. That got me through it better.