wtd, ruby questions
Author |
Message |
wtd
|
Posted: Mon Nov 08, 2004 7:12 pm Post subject: (No subject) |
|
|
Is it really "greet.rb", or did it get saved as "greet.rb.txt"? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
dsantamassino
|
Posted: Mon Nov 08, 2004 7:43 pm Post subject: (No subject) |
|
|
wtd wrote: Is it really "greet.rb", or did it get saved as "greet.rb.txt"?
No i just looked. Its really on my root C: and its named greet.rb
so i tried greet.rb and ruby greet.rb both in the command and still nothing. Its doesnt recognize for some reason |
|
|
|
|
|
wtd
|
Posted: Mon Nov 08, 2004 8:28 pm Post subject: (No subject) |
|
|
Ok... not gonna give up on this.
Try "type greet.rb" at the command-line. The Windows command prompt, not the IRB prompt. |
|
|
|
|
|
dsantamassino
|
Posted: Mon Nov 08, 2004 8:32 pm Post subject: (No subject) |
|
|
wtd wrote: Ok... not gonna give up on this.
Try "type greet.rb" at the command-line. The Windows command prompt, not the IRB prompt.
I tried at the C: command line both as followed and nothing
type greet.rb
"type greet.rb"
exactly both ways with quotes and without..... |
|
|
|
|
|
wtd
|
Posted: Mon Nov 08, 2004 8:46 pm Post subject: (No subject) |
|
|
Ok, let's try a different approach... instead of using Notepad or Textpad... for now we'll just used the built-in edit program.
At the command-line, enter:
You'll see a blue editor screen. Here you can type in the program again, unless it's already there.
code: | puts "Your name is?"
name = gets
puts "Hello, " + name + "!" |
You can use Alt-F-S to save, and Alt-F-X to exit.
Then to run it:
|
|
|
|
|
|
dsantamassino
|
Posted: Mon Nov 08, 2004 9:34 pm Post subject: (No subject) |
|
|
ok.. here is a different error message down below..
greet.rb:3: syntax error
puts "Hello, " + Derek "!" |
|
|
|
|
|
wtd
|
Posted: Mon Nov 08, 2004 9:43 pm Post subject: (No subject) |
|
|
dsantamassino wrote: ok.. here is a different error message down below..
greet.rb:3: syntax error
puts "Hello, " + Derek "!"
Cool.
You're missing a + between Derek and "!".
The name of the variable should also be "name". |
|
|
|
|
|
dsantamassino
|
Posted: Mon Nov 08, 2004 9:48 pm Post subject: (No subject) |
|
|
wtd wrote: dsantamassino wrote: ok.. here is a different error message down below..
greet.rb:3: syntax error
puts "Hello, " + Derek "!"
Cool.
You're missing a + between Derek and "!".
The name of the variable should also be "name".
Cool it worked.
Derek? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Mon Nov 08, 2004 10:41 pm Post subject: (No subject) |
|
|
Spiffy.
Now, do you understand why the program went something like:
code: | C:\> ruby greet.rb
And your name is?
Derek
Hello, Derek
!
C:\> |
When it should have looked like:
code: | C:\> ruby greet.rb
And your name is?
Derek
Hello, Derek!
C:\> |
|
|
|
|
|
|
dsantamassino
|
Posted: Mon Nov 08, 2004 10:44 pm Post subject: (No subject) |
|
|
wtd wrote: Spiffy.
Now, do you understand why the program went something like:
code: | C:\> ruby greet.rb
And your name is?
Derek
Hello, Derek
!
C:\> |
When it should have looked like:
code: | C:\> ruby greet.rb
And your name is?
Derek
Hello, Derek!
C:\> |
No i dont know. May u explain it to me?? |
|
|
|
|
|
wtd
|
Posted: Mon Nov 08, 2004 10:48 pm Post subject: (No subject) |
|
|
Sure.
When you call "gets" to get input from the user, you get all of their input, including the return key.
Then, then you use "puts" to output, you get that return again.
The key is to use another method named "chomp" to remove the return from the end of that string.
code: | puts "And your name is?"
name = gets.chomp
puts "Hello, " + name + "!" |
|
|
|
|
|
|
dsantamassino
|
Posted: Mon Nov 08, 2004 10:50 pm Post subject: (No subject) |
|
|
wtd wrote: Sure.
When you call "gets" to get input from the user, you get all of their input, including the return key.
Then, then you use "puts" to output, you get that return again.
The key is to use another method named "chomp" to remove the return from the end of that string.
code: | puts "And your name is?"
name = gets.chomp
puts "Hello, " + name + "!" |
Oh ok i think i got it |
|
|
|
|
|
wtd
|
Posted: Mon Nov 08, 2004 11:09 pm Post subject: (No subject) |
|
|
An exercise, shamelessly stolen from Chris Pine:
Write a program which asks for a person's first name, then middle, then last. Finally, it should greet the person using their full name. |
|
|
|
|
|
dsantamassino
|
Posted: Tue Nov 09, 2004 5:48 pm Post subject: (No subject) |
|
|
I tired to do it but its pointed an upward error to the middle name |
|
|
|
|
|
wtd
|
Posted: Tue Nov 09, 2004 6:20 pm Post subject: (No subject) |
|
|
Post the code you write. Please surround it with code tags, like so:
code: | [code]This is formatted as code.[/code] |
|
|
|
|
|
|
|
|