[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

hello world (first method

Thufir Hawat

11/3/2007 3:53:00 AM

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


6 Answers

Shuaib Zahda

11/3/2007 4:00:00 AM

0

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
--
Posted via http://www.ruby-....

Thufir Hawat

11/3/2007 4:04:00 AM

0

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


Thufir Hawat

11/3/2007 4:13:00 AM

0

On Nov 2, 8:59 pm, Shuaib Zahda <shuaib.za...@gmail.com> 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


Sebastian probst Eide

11/3/2007 5:48:00 AM

0

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

require 'test'

S



On 3. nov. 2007, at 01.13, Thufir wrote:

> On Nov 2, 8:59 pm, Shuaib Zahda <shuaib.za...@gmail.com> 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
>
>


7stud --

11/3/2007 9:09:00 AM

0

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.
--
Posted via http://www.ruby-....

Thufir Hawat

11/3/2007 6:37:00 PM

0

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