[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: problem using IRB

Ghelani, Vidhi

2/11/2005 7:56:00 PM

Hey,

I could download it and also use IRB. However, I was wondering if you
could tell me how to run this code on IRB:
def fact(n)
if n == 0
1
else
n * fact(n-1)
end
end

print fact(ARGV[0].to_i), "\n"

I tried doing it but its running each line separately. Is there any way
to run the whole thing at once.

Thanks,
Vidhi


-----Original Message-----
From: Curt Hibbs [mailto:curt@hibbs.com]
Sent: Friday, February 11, 2005 11:15 AM
To: ruby-talk ML
Subject: Re: new to this language

Ghelani, Vidhi wrote:
>
> Hey curt,
>
> No I did'nt install this using the one-click installer. Truly
> speaking I don't know how I got this. Neways, I tried 2 things
> 1) Tried using irb from the secure shell that I am using. I just
> typed irb, and it says -bash: -irm: command not found
> 2) Since I have a windows machine, I tried using the command
> prompt window and from there I typed in irb. Hmm...but this Does
> not work either !
>
> How do I go about getting irb, cos it sounds really kewl.

Go here:

http://rubyinstaller.ruby...

and follow the download link. Download the latest version of the
installer
(182-14). Once you have installed ruby from you should find a number of
use
tools in you start menu under "Ruby", and you should be able to run
"irb"
from the command line to get into an interactive ruby session.

Curt





4 Answers

Douglas Livingstone

2/11/2005 8:28:00 PM

0

On Sat, 12 Feb 2005 04:56:04 +0900, Ghelani, Vidhi
<vidhi.ghelani@intel.com> wrote:
> I tried doing it but its running each line separately. Is there any way
> to run the whole thing at once.

each line is evaluated as you type it in, hence it is "interactive".

Everything is stored in memory that you've typed in so far, the line
number is shown on the left.

Perhaps there is some code to rerun from the start? I don't know what
it is though...

Douglas


Ben Giddings

2/11/2005 8:56:00 PM

0

Hi Vidhi,

You seem to have a lot of questions! You would probably really benefit
from buying the book "Programming Ruby" by Dave Thomas. He answers a
lot of the questions you're asking, and a lot more besides.

It takes you all the way from the very basic things, the ones you're
having some troubles with, all the way to the really advanced stuff.

http://pragmaticprogrammer.com/ti...

You can buy a paper version, a PDF, or both! I'd recommend both. The
paper copy is great for sitting and reading, but you can take the PDF
with you, and can search through it easily.

Ben


Gavin Kistner

2/11/2005 9:08:00 PM

0

ARGV = %w| 15 |
load 'factorial.rb'

Mark Hubbart

2/12/2005 12:17:00 AM

0

Hi,

On Sat, 12 Feb 2005 04:56:04 +0900, Ghelani, Vidhi
<vidhi.ghelani@intel.com> wrote:
> Hey,
>
> I could download it and also use IRB. However, I was wondering if you
> could tell me how to run this code on IRB:
> def fact(n)
> if n == 0
> 1
> else
> n * fact(n-1)
> end
> end
>
> print fact(ARGV[0].to_i), "\n"
>
> I tried doing it but its running each line separately. Is there any way
> to run the whole thing at once.

I'm not sure why you would need to run it all at once; there's no real
difference in IRB between running the statements separately and
running them all together. Is there any particular reason?

Still, you can do it if you want, by using explicit statement
separators (";") and newline escapes ("\"):

def fact(n)
if n == 0
1
else
n * fact(n-1)
end
end;print fact(ARGV[0].to_i), "\n"

This should run it as one chunk of code in irb. The ;s make sure the
interpreter separates the statements. The \s make sure it doesn't stop
evaluating the code at the newlines

As a side note, ARGV is usually empty in irb, I think.

HTH,
Mark