New


#1

Im just learning ruby and im stuck the guide im using says

"Your first program
Congratulations! Now that you have made it this far, you are ready to
start writing programs.
Hello World
Grab a text editor and type in the following:

puts “Hello World”

Save the file as hello.rb and run it by typing
ruby hello.rb"

i’ve tried that more than once and it says the out is:

"irb(main):001:0> ruby hello.rb
NameError: undefined local variable or method `hello’ for main:Object
from (irb):1
irb(main):002:0> "

the guide never said what word processor should i use
i tried TextEdit.app but it fussed about the .rb
so i used Microsoft word and this is what it does…

Any tips?


#2

You need to type “ruby hello.rb” from the command line rather than
inside of
irb.

On 4/17/07, poison tooth removed_email_address@domain.invalid wrote:

the guide never said what word processor should i use


“Hey brother christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)


#3

if your in a linux computer, you may need this at the beginning of your
program

#!/usr/bin/env ruby

or

#!/usr/bin/ruby

also may need to make the file executatble by

chmod a+x /path/to/your/program.rb

hth


#4

oops, did not read your email well enough.

dont use MS word, use notepad or something
MS word puts a lot of formatting stuff in the file
hth.

sk


#5

On Apr 17, 2007, at 12:22 PM, poison tooth wrote:

Save the file as hello.rb and run it by typing
ruby hello.rb"

i’ve tried that more than once and it says the out is:

"irb(main):001:0> ruby hello.rb
NameError: undefined local variable or method `hello’ for main:Object
from (irb):1
irb(main):002:0> "

Try running “ruby hello.rb” from a Terminal window (on a Mac) instead
of from irb.
If you want to run it from irb, enter “load ‘hello.rb’”.

the guide never said what word processor should i use
i tried TextEdit.app but it fussed about the .rb
so i used Microsoft word and this is what it does…

Any tips?

You can use any text editor. TextEdit should be fine, but there are
many better choices. I see you’re on a Mac. Check out TextMate.


#6

On Apr 17, 2007, at 1:22 PM, poison tooth wrote:

Save the file as hello.rb and run it by typing
i tried TextEdit.app but it fussed about the .rb
so i used Microsoft word and this is what it does…

Any tips?

Since you mention TextEdit, I presume you are on a Mac. Also, since
you mention irb, I presume you are familiar with Terminal. Here is an
easy way to run a Ruby script in Terminal. After writing your hello
script in TextEdit save it as hello.rb. Now in terminal at a fresh
prompt, type "ruby " (without the quotes and remember to include the
space after “ruby”). Now drag hello.rb into to Terminal and hit
return. The text “Hello World” should appear.

Regards, Morton


#7

On Apr 17, 2007, at 2:59 PM, Morton G. wrote:

puts “Hello World”

fresh prompt, type "ruby " (without the quotes and remember to
include the space after “ruby”). Now drag hello.rb into to Terminal
and hit return. The text “Hello World” should appear.

Also, there is one more possible source of trouble. TextEdit must be
in Plain Text mode when you save a Ruby script. If it’s in Rich Text
mode, hit cmnd-shift-T to change the mode before you save.

Regards, Morton


#8

oh ok
i think i know where i made my mistake
thanks


#9

oh
now i got this…

h-68-164-42-185:~ stefan$ ruby ruby.rb
ruby: No such file or directory – ruby.rb (LoadError)
h-68-164-42-185:~ stefan$ cd documents
h-68-164-42-185:~/documents stefan$ ruby hello.rb
hello.rb:1: Invalid char \320' in expression hello.rb:1: Invalid char\317’ in expression
hello.rb:1: Invalid char \021' in expression hello.rb:1: Invalid char\340’ in expression
hello.rb:1: Invalid char \241' in expression hello.rb:1: Invalid char\261’ in expression
h-68-164-42-185:~/documents stefan$

do i need to get into irb then out and then do it?


#10

smc smc wrote:

hello.rb:1: Invalid char \340' in expression hello.rb:1: Invalid char\241’ in expression
hello.rb:1: Invalid char `\261’ in expression
h-68-164-42-185:~/documents stefan$

do i need to get into irb then out and then do it?

Firstly:
A: Because it makes it difficult to associate responses.
Q: Why is top posting bad?

Secondly: Save your script as a text file, not as a binary.


Phillip “CynicalRyan” Gawlowski
http://cynicalryan.110mb.com/
http://clothred.rubyforge.org

Rule of Open-Source Programming #34:

Every successful project will eventually spawn a sub-project


#11

On Apr 17, 6:28 pm, “smc smc” removed_email_address@domain.invalid wrote:

hello.rb:1: Invalid char \340' in expression hello.rb:1: Invalid char\241’ in expression
hello.rb:1: Invalid char `\261’ in expression
h-68-164-42-185:~/documents stefan$

do i need to get into irb then out and then do it?

Nope, it looks like the file wasn’t saved as plain text. I’m guessing
you tried saving it from Word, like you said earlier. I don’t know
how to save as plain text in Word, so I’ll show you how to do it
another way.

Open your file in TextEdit. If TextEdit can’t open the old file, it
might be having problems with the MS format, just delete the old one
and start a new file in TextEdit, and enter your ruby code. Make sure
you hit return at the end of your file ( this is just so we can view
it with the cat command later ).

Unless you have converted the file to plain text, TextEdit won’t let
you save it as a “.rb” file. So, first, go to “Format->Make Plain
Text” (if you only see “Make Rich Text”, then it’s already plain
text). If it asks you whether to convert the file to plain text,
click OK. Then save the file as “hello.rb”. TextEdit will probably
pop up with a message saying 'You have used the extension “.rb” at the
end of the name. The standard extension is “.txt”. Click the button
that says “Use .rb”. (

Now, you can check from the command line that your file is actually
plain text, by using the “cat” command.

h-68-164-42-185:~/documents stefan$ cat hello.rb
puts “Hello World”

And now you can run it.

h-68-164-42-185:~/documents stefan$ ruby hello.rb
Hello World

Later on, you might want to consider getting a “real” text editor to
help you write ruby code. They’ll do things that Word & TextEdit
don’t that make it easier to write good code. Features like syntax
highlighting and auto-indentation are two of the ways that “real” text
editors help coders catch errors early on.

I’m not going to tell you which one to use. If you ask any
programmer, anywhere on the web, they’ll tell you which they use, and
why it’s the one you should use. You might want to check out the
wikipedia article (http://en.wikipedia.org/wiki/Text_editors), as that
might give you some good leads.

But, I wouldn’t get too sidetracked by looking for an editor.
TextEdit (in plain text mode) should be good enough to get you through
the basic tutorials.


#12

poison tooth wrote:

Grab a text editor and type in the following:

puts “Hello World”

Save the file as hello.rb and run it by typing
ruby hello.rb"

If you just want to make sure that you can run that book example, here
is a quick low-level solution. In a terminal, run:

echo ‘puts “Hello World”’ > hello.rb
ruby hello.rb

Alternatively, you can run:

ruby -e ‘puts “Hello World”’

As for your program editing troubles, I suggest having a look at the
Ruby garden (skip down to the “Ruby development environments” section):

http://wiki.rubygarden.org/Ruby/page/show/RubyOnMacintosh


#13

while you are at it, get the book “learning unix for OS X Tiger”
You’re going to need a few unix skills here. Not a lot, but it will
help you understand stuff more.
If you need more, get Unix In a Nutshell (it covers OS X, Linux, Unix)


#14

thanks
i finally got it!!!


#15

if u r in the learning phase ,then just try scite editor.it is the best
one.


#16

On 18 Apr 2007, at 01:40, smc smc wrote:

thanks
i finally got it!!!

:slight_smile: Congratulations. I hope you have a lot of fun.


#17

i know quite a bit of unix so thats not a problem and i can also ask
my dad (but he dosent know ruby) :{