hello world (first method)

H

hawat.thufir

For the following:

C:\Documents and Settings\nsaunders\Desktop>
C:\Documents and Settings\nsaunders\Desktop>type HelloWorld.rb
#5.times { print "Odelay!" }




def fact(n)
if n <= 1
1
else
n * fact(n - 1)
end

print n

end


C:\Documents and Settings\nsaunders\Desktop>
C:\Documents and Settings\nsaunders\Desktop>HelloWorld.rb

C:\Documents and Settings\nsaunders\Desktop>
C:\Documents and Settings\nsaunders\Desktop>HelloWorld.rb
1C:/Documents and Settings/nsaunders/Desktop/HelloWorld.rb:10:in `*':
nil can't
be coerced into Fixnum (TypeError)
from C:/Documents and Settings/nsaunders/Desktop/HelloWorld.rb:
10:in `fa
ct'
from C:/Documents and Settings/nsaunders/Desktop/HelloWorld.rb:
10:in `fa
ct'
from C:/Documents and Settings/nsaunders/Desktop/HelloWorld.rb:
17

C:\Documents and Settings\nsaunders\Desktop>
C:\Documents and Settings\nsaunders\Desktop>


Does there need to be class defined to call the method?



thanks,

Thufir
 
S

shuaib.zahda

I believe so and u need to create an object and call the method using
the object


class Test

def fact
...
end

end

obj = Test.new
obj.fact

I wish this will help
 
H

hawat.thufir

Thank you :)

In the interim, I got better results:

C:\Documents and Settings\nsaunders\Desktop>
C:\Documents and Settings\nsaunders\Desktop>
C:\Documents and Settings\nsaunders\Desktop>
C:\Documents and Settings\nsaunders\Desktop>
C:\Documents and Settings\nsaunders\Desktop>type HelloWorld.rb
#5.times { print "Odelay!" }


n=5

def fact(n)
if n <= 1
1
else
n * fact(n - 1)
end
end


print fact(20)




def hello(i, aString)

i.times {print "\n" + aString + "\n"}

end


hello(6, "success")
C:\Documents and Settings\nsaunders\Desktop>
C:\Documents and Settings\nsaunders\Desktop>HelloWorld.rb
2432902008176640000
success

success

success

success

success

success

C:\Documents and Settings\nsaunders\Desktop>
C:\Documents and Settings\nsaunders\Desktop>


Once I took the plunge, it's quick progress so far! I can change a
line, then just run it. I haven't gotten used to the idea of the irb,
for now just going back and forth between the DOS prompt and notepad
works smoothly.



-Thufir
 
H

hawat.thufir

I believe so and u need to create an object and call the method using
the object

class Test
[...]

I'd like to have two .rb files:

Test.rb
useTest.rb (or would that be UseTest.rb ?)


How would I do that, please?


thanks,

Thufir
 
S

sebastian.probst.eide

Include the test.rb file in the useTest.rb file like this:

require 'test'

S



I believe so and u need to create an object and call the method using
the object

class Test
[...]

I'd like to have two .rb files:

Test.rb
useTest.rb (or would that be UseTest.rb ?)


How would I do that, please?


thanks,

Thufir
 
B

bbxx789_05ss

Thufir said:
line, then just run it. I haven't gotten used to the idea of the irb,
for now just going back and forth between the DOS prompt and notepad
works smoothly.

There's nothing wrong with that. I never use irb (or interactive
sessions in other languages I program in). I find them unwieldy and too
much of a hassle to bother with.

If notepad doesn't provide automatic indenting, then you should try to
obtain a text editor that does. It makes typing in code much easier.
The way it works is that when you hit return and the next line should be
indented, the text editor automatically indents the cursor on the next
line--you don't have to type any spaces. Likewise, if you type in a
line of code that is indented, and the next line shouldn't be indented,
then the text editor automatically dedents the next line.
 
T

Thufir

Thanks :)

C:\code>
C:\code>
C:\code>
C:\code>
C:\code>
C:\code>
C:\code>dir
Volume in drive C has no label.
Volume Serial Number is 0491-510F

Directory of C:\code

11/03/2007 11:34 AM <DIR> .
11/03/2007 11:34 AM <DIR> ..
11/03/2007 11:35 AM 122 doFactorial.rb
11/03/2007 11:34 AM 114 factorial.rb
11/03/2007 11:32 AM 353 Hello.rb
11/03/2007 11:26 AM 319 Hello.txt
4 File(s) 908 bytes
2 Dir(s) 28,968,902,656 bytes free

C:\code>type doFactorial.rb
require 'factorial'

x=20

print "\n\n\nThe factorial of "
print x
print " is "
print fact(x)
print "\n\n\n\n"

C:\code>
C:\code>type factorial.rb
def fact(n)
if n <= 1
1
else
n * fact(n - 1)
end
end
C:\code>
C:\code>doFactorial.rb



The factorial of 20 is 2432902008176640000




C:\code>
C:\code>


Also, when I got home I read poignant ruby and found it :)



-Thufir
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top