# Hello world (first method)

**URL:** <https://www.ruby-forum.com/t/hello-world-first-method/119106>\
**Category:** Ruby\
**Created:** [November 3, 2007, 4:53am UTC](https://www.ruby-forum.com/t/hello-world-first-method/119106 "2007-11-03T04:53:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 3, 2007, 4:53am UTC](https://www.ruby-forum.com/t/hello-world-first-method/119106/1 "2007-11-03T04:53:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 3, 2007, 4:59am UTC](https://www.ruby-forum.com/t/hello-world-first-method/119106/2 "2007-11-03T04:59:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 3, 2007, 5:04am UTC](https://www.ruby-forum.com/t/hello-world-first-method/119106/3 "2007-11-03T05:04:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 3, 2007, 5:08am UTC](https://www.ruby-forum.com/t/hello-world-first-method/119106/4 "2007-11-03T05:08:34Z")

</div>

Quoth Shuaib Z.:

> end
> 
> obj = Test.new  
> obj.fact
> 
> I wish this will help

Wrong.

I don’t know what the OP is doing wrong (from the source, he isn’t  
calling  
fact() ever. However, defining a method without a class really defines  
it as  
part of Kernel (which methods can be called without Classname.method).

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 3, 2007, 5:14am UTC](https://www.ruby-forum.com/t/hello-world-first-method/119106/5 "2007-11-03T05:14:59Z")

</div>

On Nov 2, 8:59 pm, Shuaib Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 3, 2007, 7:02am UTC](https://www.ruby-forum.com/t/hello-world-first-method/119106/6 "2007-11-03T07:02:28Z")

</div>

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

require ‘test’

S

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 3, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/hello-world-first-method/119106/7 "2007-11-03T19:40:32Z")

</div>

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 .  
11/03/2007 11:34 AM …  
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

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 3, 2007, 10:09am UTC](https://www.ruby-forum.com/t/hello-world-first-method/119106/8 "2007-11-03T10:09:16Z")

</div>

Thufir wrote:

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