aaron@skelly:~/test$ cat test.rb
puts 'Hello there, and what\'s your first name?'
firstname = gets.chomp
puts 'And your middle name?'
midname = gets.chomp
puts 'And your last name?'
lastname = gets.chomp
puts 'Your name is ' +firstname+midname+lastname+ '? What a lovely name!'
puts 'Pleased to meet you, ' +name+midname+lastname+ '.'
aaron@skelly:~/test$ ruby ./test.rb
Hello there, and what's your first name?
Davion
And your middle name?
Aaron
And your last name?
Kalhen
Your name is DavionAaronKalhen? What a lovely name!
./test.rb:8: undefined local variable or method `name' for main:Object (NameError)
puts 'What\'s your first name?'
fname = gets.chomp
puts 'And what is your last name?'
lname = gets.chomp
puts 'Oh, and what is your middle name?'
mname = gets.chomp
puts 'Ok, your whole name is ' + fname + ' ' + mname + ' ' + lname + '.'
totalCh = fname.length.to_i + (mname.length.to_i + lname.length.to_i)
puts 'You have ' + totalCh.to_s + ' letters in your name.'
p 'What is your first name?'
name = gets.chomp
p 'What is your middle name?'
name << (' ' + gets.chomp)
p 'What is your last name?'
name << (' ' + gets.chomp)
p "#{name} is an excellent name!"
sleep(2)
puts 'Hello there, and what\'s your first name?'
firstname = gets.chomp
puts 'Hmm… ' +firstname+ ' interesting. And your middle name?'
midname = gets.chomp
puts 'Ah, '+midname+ ' is a good name. And your last name?'
lastname = gets.chomp
puts lastname + '! How wonderful!'
puts 'Your name is ' +firstname+ ' ' +midname+ ' ' +lastname+ '? What a lovely name!'
puts 'Pleased to meet you, ' +firstname+ ' ' +midname+ ' ' +lastname+ '.'
sleep 2[/quote]
puts 'What is your first name?'
name1 = gets.chomp
count1 = name1.length
puts 'What is your middle name?'
name2 = gets.chomp
count2 = name2.length
puts 'And what is your last name?'
name3 = gets.chomp
count3 = name3.length
count4 = count1+count2+count3
puts 'There are ' +count4.to_s+ ' letters in your name, ' +name1+ ' ' +name2+ ' ' +name3+ '.'
sleep 2
puts 'What is your full name?'
name = gets.chomp
puts 'There are ' + name.length.to_s + ' characters in your name, ' + name + '.'
def meth
p 'hi'
end
alias m meth
m # hi
class NameGetter
def initialize
@name = String.new
end
def add_name(name)
@name << (' ' + name)
end
def get_name
@name
end
end
ng = NameGetter.new
puts 'What is your first name?'
ng.add_name(gets.chomp)
puts 'What is your middle name?'
ng.add_name(gets.chomp)
puts 'What is your last name?'
ng.add_name(gets.chomp)
puts "#{ng.get_name} is a great name!"
sleep(1)
The program works fine until the end where it ends the executable without displaying the last line. It puts the 2nd to last line up but then closes the program (i had to repeat it several times and look carefully to see that it does indeed display the 2nd to last line cause it closes it fast).
Maybe this is just something I need to configure with the ruby program itself, or maybe it's faulty writing on my part, I am not sure so I figured I'd ask for help on here.
I realize that what I have would display the name with no spaces between first middle and last, but that's not the problem I'm dealing with right now.
[edit to add] Well I feel stupid now, after looking at it, I notice that in the last line the firstname variable is just called name by mistake. But the program still closes immediately after it displays that last line. Any hints on how to prevent it from doing that aside from putting gets as the last line?