Igabod
Wizard


Group: Members
Posts: 969
Joined: Jul 23, 2008
|
#1 Posted Jul 16, 2009, 7:15 am
|
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.
Code (text): 1
2
3
4
5
6
7
8
9
10 | 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?
|
......................... Join the rest of the Dark Warriors in the struggle to be the greatest.
Bored?? Click THIS and you too can build your own mini city.
Every man has his follies, and often they are the most interesting thing he has got - Josh Billings
Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. - Albert Einstein
I have a love interest in every one of my films... a gun. - Arnold Schwarzenegger
Recession is when a neighbor loses his job. Depression is when you lose yours. - Ronald Reagan
I like long walks, especially when they are taken by people who annoy me. - Fred Allen
Last edited Jul 16, 2009, 7:24 am by Igabod
|
|
Davion
Idle Hand

Group: Administrators
Posts: 1,188
Joined: May 14, 2006
|
#2 Posted Jul 16, 2009, 8:54 am
|
Code (text): 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 | 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)
|
|
......................... 
|
|
Runter
Wizard


Group: Members
Posts: 1,074
Joined: Jun 1, 2006
|
#3 Posted Jul 16, 2009, 12:09 pm
|
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. :)
|
|
......................... -Heath
For once you have tasted flight Ruby you will walk the earth with your eyes turned skywards,
for there you have been and there you will long to return. --
Leonardo Da Vinci Yukihiro Matsumoto
Last edited Jul 16, 2009, 12:09 pm by Runter
|
|
|
|
|
|
Ssolvarain
Sorcerer


Group: Members
Posts: 322
Joined: Nov 14, 2008
|
#6 Posted Jul 16, 2009, 2:05 pm
|
Ruby is proving to be entertaining....
Code (text): 1
2
3
4
5
6
7
8
9
10
11 | 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.' |
|
|
......................... Builder, End of Time eotmud.com:4000
The Rodney Dangerfield of Mudding.
|
|
Ssolvarain
Sorcerer


Group: Members
Posts: 322
Joined: Nov 14, 2008
|
#7 Posted Jul 16, 2009, 4:15 pm
|
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.
|
|
......................... Builder, End of Time eotmud.com:4000
The Rodney Dangerfield of Mudding.
|
|
Chris Bailey
Sorcerer


Group: Members
Posts: 499
Joined: Sep 13, 2008
|
#8 Posted Jul 16, 2009, 8:38 pm
|
Oh yeah, ruby is tons of fun. Here is my implementation of the same, using interpolation for string clarity.
Code (text): 1
2
3
4
5
6
7
8
9
10
11
12 |
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)
|
|
......................... If what Proust says is true, that happiness is the absence of fever, then I will never know happiness. For I am possessed by a fever for knowledge, experience, and creation.
Quote:[ichat] Twisol@Talon: Do me, do me.
|
|
Igabod
Wizard


Group: Members
Posts: 969
Joined: Jul 23, 2008
|
#9 Posted Jul 17, 2009, 12:29 am
|
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.
Code (text): 1
2
3
4
5
6
7
8
9
10
11
12 | 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] |
|
......................... Join the rest of the Dark Warriors in the struggle to be the greatest.
Bored?? Click THIS and you too can build your own mini city.
Every man has his follies, and often they are the most interesting thing he has got - Josh Billings
Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. - Albert Einstein
I have a love interest in every one of my films... a gun. - Arnold Schwarzenegger
Recession is when a neighbor loses his job. Depression is when you lose yours. - Ronald Reagan
I like long walks, especially when they are taken by people who annoy me. - Fred Allen
Last edited Jul 17, 2009, 12:31 am by Igabod
|
|
Metsuro
Magician

Group: Members
Posts: 90
Joined: Jan 3, 2007
|
#10 Posted Jul 17, 2009, 12:33 am
|
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?
|
......................... 
|
|
Igabod
Wizard


Group: Members
Posts: 969
Joined: Jul 23, 2008
|
#11 Posted Jul 17, 2009, 12:52 am
|
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?
|
......................... Join the rest of the Dark Warriors in the struggle to be the greatest.
Bored?? Click THIS and you too can build your own mini city.
Every man has his follies, and often they are the most interesting thing he has got - Josh Billings
Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. - Albert Einstein
I have a love interest in every one of my films... a gun. - Arnold Schwarzenegger
Recession is when a neighbor loses his job. Depression is when you lose yours. - Ronald Reagan
I like long walks, especially when they are taken by people who annoy me. - Fred Allen
|
|
Igabod
Wizard


Group: Members
Posts: 969
Joined: Jul 23, 2008
|
#12 Posted Jul 17, 2009, 1:29 am
|
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.
Code (text): 1
2
3
4
5
6
7
8
9
10
11
12
13
14 | 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
Code (text): 1
2
3
4
5 | 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.
|
......................... Join the rest of the Dark Warriors in the struggle to be the greatest.
Bored?? Click THIS and you too can build your own mini city.
Every man has his follies, and often they are the most interesting thing he has got - Josh Billings
Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. - Albert Einstein
I have a love interest in every one of my films... a gun. - Arnold Schwarzenegger
Recession is when a neighbor loses his job. Depression is when you lose yours. - Ronald Reagan
I like long walks, especially when they are taken by people who annoy me. - Fred Allen
Last edited Jul 17, 2009, 1:30 am by Igabod
|
|
Chris Bailey
Sorcerer


Group: Members
Posts: 499
Joined: Sep 13, 2008
|
#13 Posted Jul 17, 2009, 1:36 am
|
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!
Code (text): 1
2
3
4
5
6
7
8
9 |
def meth
p 'hi'
end
alias m meth
m # hi
|
|
......................... If what Proust says is true, that happiness is the absence of fever, then I will never know happiness. For I am possessed by a fever for knowledge, experience, and creation.
Quote:[ichat] Twisol@Talon: Do me, do me.
|
|
Chris Bailey
Sorcerer


Group: Members
Posts: 499
Joined: Sep 13, 2008
|
#14 Posted Jul 17, 2009, 1:44 am
|
Also, don't forget that while you can use ruby procedurally, it is really an OO language!
Code (text): 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 |
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)
|
|
......................... If what Proust says is true, that happiness is the absence of fever, then I will never know happiness. For I am possessed by a fever for knowledge, experience, and creation.
Quote:[ichat] Twisol@Talon: Do me, do me.
|
|
Igabod
Wizard


Group: Members
Posts: 969
Joined: Jul 23, 2008
|
#15 Posted Jul 17, 2009, 1:58 am
|
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?
|
......................... Join the rest of the Dark Warriors in the struggle to be the greatest.
Bored?? Click THIS and you too can build your own mini city.
Every man has his follies, and often they are the most interesting thing he has got - Josh Billings
Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. - Albert Einstein
I have a love interest in every one of my films... a gun. - Arnold Schwarzenegger
Recession is when a neighbor loses his job. Depression is when you lose yours. - Ronald Reagan
I like long walks, especially when they are taken by people who annoy me. - Fred Allen
|
|