16 Jul, 2009, Igabod wrote in the 1st comment:
Votes: 0
Ok, after reading that thread with dubstack and seeing Chris Bailey's offer to help him out inspired me to try to learn ruby and it's turning out to be quite fun. I downloaded the program chris suggested and went to a tutorial site suggested by someone else. I'm going through the tutorial right now and I just learned about the get.chomp feature. In the tutorial it suggests trying to make a program that asks for the first name of a person, then the middle name then the last name, and then greet them by the full name. Here's what I tried.

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+ '.'


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?
16 Jul, 2009, Davion wrote in the 2nd comment:
Votes: 0
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)
16 Jul, 2009, Runter wrote in the 3rd comment:
Votes: 0
Igabod said:
[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?


It exits because the program finishes. Add a gets on its own line to wait for input before continuing or use sleep to make it wait before continuing.

Congrats on learning a great language btw. Just ask if you need any help with anything else. :)
16 Jul, 2009, Tyche wrote in the 4th comment:
Votes: 0
Also on some implementations, you won't see the last thing printed to the console because the I/O buffers aren't flushed.
So you have to something like the following:

puts "something"
$stdout.flush
16 Jul, 2009, David Haley wrote in the 5th comment:
Votes: 0
There are implementations that don't flush the buffers on program exit? That's a little surprising.
16 Jul, 2009, Ssolvarain wrote in the 6th comment:
Votes: 0
Ruby is proving to be entertaining….

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.'
16 Jul, 2009, Ssolvarain wrote in the 7th comment:
Votes: 0
Can't edit anymore… I was going to say…

The Pragmatic Bookshelf's Ruby book has been one of the least confusing and most helpful resources I've used so far.
17 Jul, 2009, Chris Bailey wrote in the 8th comment:
Votes: 0
Oh yeah, ruby is tons of fun. Here is my implementation of the same, using interpolation for string clarity.

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)
17 Jul, 2009, Igabod wrote in the 9th comment:
Votes: 0
Ah, thanks for the help guys. I'm sure I'll have a billion more questions as I go, but this is certainly more fun to learn than I thought it would be. Don't know why I refused to learn it long ago when I was told about it for the first time.

A little question on chris bailey's example…

1. You can abbreviate methods in ruby? (p instead of puts)
2. What's the << and the #{name} functionality?

I'll probably learn these things later in my tutorial but I'm curious now so that's why I'm asking.

[edit to add]
Oh, and here's what I came up with to finish this simple program up.

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]
17 Jul, 2009, Metsuro wrote in the 10th comment:
Votes: 0
well << is like C++'s this goes into this, thing correct? and the #{name} is a variable in the string so it expands the variable then puts the string?
17 Jul, 2009, Igabod wrote in the 11th comment:
Votes: 0
ok so the program is saying

name = what you type for first name
then
name = first name + space + midname
then
name = first name + space + midname + space + last name

then it prints the conglomeration of first middle and last name so adding the #{} around the variable name allows you to print the results of all three questions using one variable?
17 Jul, 2009, Igabod wrote in the 12th comment:
Votes: 0
hehe this is so fun, I'm like the little nerdy kid at the video game store right now. here's an alteration on the name program that counts the letters in the name not just the characters.

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


To get the character count all you'd need to do is a simple 3 liner though

puts 'What is your full name?'
name = gets.chomp
puts 'There are ' + name.length.to_s + ' characters in your name, ' + name + '.'


Why did I not trust anybody when they said how easy it is to learn ruby? It's almost like basic but with some neat features. I remember making some fun games on my TI-82 calculator back in highschool using basic and this is so much like basic it's bringing me back to the "good ole days" of highschool, even though I didn't particularly think those days were that good back then.
17 Jul, 2009, Chris Bailey wrote in the 13th comment:
Votes: 0
Igabod, It's friendlier than basic and powerful enough to do just about anything! =)

String << String will append whatever is on the right side of the << to whatever is on the left side.
Interpolation #{} will expand a variable in place. I use it because it is very clean looking. Also keep
in mind that you can use tradition string formatting (like in c) as well. using sprintf and so on. Some
methods have aliases, puts is one of them. p will evaluate to puts. You can make your own aliases
if you like!
def meth
p 'hi'
end
alias m meth
m # hi
17 Jul, 2009, Chris Bailey wrote in the 14th comment:
Votes: 0
Also, don't forget that while you can use ruby procedurally, it is really an OO language!
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)
17 Jul, 2009, Igabod wrote in the 15th comment:
Votes: 0
I'm installing ruby in cygwin right now and was curious if there's anything else I need to install to use ruby in cygwin. I'm installing ruby which is under the devel header in the package manager. Are there any specific libs I need to install too?
17 Jul, 2009, Chris Bailey wrote in the 16th comment:
Votes: 0
Yes, you have much work to be done.
1) Uninstall Cygwin
2) Uninstall it again, just to be sure.
3) Go ahead and low level format your hdd to get rid of all windows traces.
4) Install Gentoo
5) Install Ruby
6) Program!
17 Jul, 2009, Tyche wrote in the 17th comment:
Votes: 0
David Haley said:
There are implementations that don't flush the buffers on program exit? That's a little surprising.


No.
It doesn't happen with puts, but will happen with other i/o calls that don't automatically add newlines.

for example run this…
5.times do
putc('.')
sleep(2)
end

$stdout.sync = true
5.times do
putc('.')
sleep(2)
end

So if you ask questions using a prompt without a newline you either need to flush stdout or make sure the sync flag is set to true like above.
17 Jul, 2009, Igabod wrote in the 18th comment:
Votes: 0
Chris Bailey said:
Yes, you have much work to be done.
1) Uninstall Cygwin
2) Uninstall it again, just to be sure.
3) Go ahead and low level format your hdd to get rid of all windows traces.
4) Install Gentoo
5) Install Ruby
6) Program!


I'm quite happy with windows for most of my needs, the only time it becomes troublesome is with Cygwin but I don't know unix commands well enough to switch to linux or any other unix clone. I also don't feel like learning it so I'll deal with the eccentricities of Cygwin. I've already gone into why I don't switch to andLinux in several other threads too so now that we've got that out of the way, anybody who is familiar with Cygwin want to answer my question?
17 Jul, 2009, Chris Bailey wrote in the 19th comment:
Votes: 0
Sorry, I wish I could help. I used cygwin for a few minutes several years ago, but that was it. =(
17 Jul, 2009, Metsuro wrote in the 20th comment:
Votes: 0
you'd want gems, rake, irb, ruby-dev… eventmachine for your network stuff… rails for web development…
0.0/79