[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Variable Arguments

rohitlodha

1/11/2005 4:26:00 PM

Hi

Ruby has a variable argument syntax as well as fixed number of arguments
syntax. The caller does not know about how the called function's syntax.
In case of variable arguments, it is expected caller to provide with
number of arguments while for the other case it might not.

Since, I am writing a VM for Ruby, I would like to know how should one
handle this problem?

regards,
Mystifier
6 Answers

Robert Klemme

1/11/2005 4:32:00 PM

0


<rohitlodha@hotwireindia.com> schrieb im Newsbeitrag
news:000a01c4f7fa$30251dc0$1a87103d@vubydump...
> Hi
>
> Ruby has a variable argument syntax as well as fixed number of arguments
> syntax. The caller does not know about how the called function's syntax.

The caller can in fact know the number of arguments at runtime:

>> class Foo
>> def f1(a)end
>> def f2(*a)end
>> def f3(a,*b)end
>> end
=> nil
>> f=Foo.new
=> #<Foo:0x10183158>
>> f.method("f1").arity
=> 1
>> f.method("f2").arity
=> -1
>> f.method("f3").arity
=> -2

And you'll see an error if the arguments passed do not match:

>> f.f1(1,2,3)
ArgumentError: wrong number of arguments(3 for 1)
from (irb):11:in `f1'
from (irb):11

> In case of variable arguments, it is expected caller to provide with
> number of arguments while for the other case it might not.

I'm sorry, I don't understand what you mean by this.

> Since, I am writing a VM for Ruby, I would like to know how should one
> handle this problem?

As I'm currently mystified by your question I can't come up with an
answer. Could you please clarify this? And what kind of VM are you
writing?

Kind regards

robert

rohitlodha

1/11/2005 5:48:00 PM

0


Hi Robert,

How can you say caller know about the number of arguments. It might be
possible that it call the function with all what he has and caller
responds with "I am being called with too few or too many arguments".
This can be shown as the error you have indicated.


About VM, it is open source and will be uploaded in a week or so.


Regards,
mystifier





Florian Gross

1/11/2005 5:50:00 PM

0

Mystifier wrote:

> How can you say caller know about the number of arguments. It might be
> possible that it call the function with all what he has and caller
> responds with "I am being called with too few or too many arguments".
> This can be shown as the error you have indicated.

But you can check the method's arity before calling it. If it's a
positive number then it expects exactly that many arguments. If it's a
negative number it expects at least ~arity arguments. Is this not what
you asked?

Robert Klemme

1/12/2005 9:20:00 AM

0


"Florian Gross" <flgr@ccan.de> schrieb im Newsbeitrag
news:34ihugF4b43vqU1@individual.net...
> Mystifier wrote:
>
> > How can you say caller know about the number of arguments. It might be
> > possible that it call the function with all what he has and caller
> > responds with "I am being called with too few or too many arguments".
> > This can be shown as the error you have indicated.
>
> But you can check the method's arity before calling it. If it's a
> positive number then it expects exactly that many arguments. If it's a
> negative number it expects at least ~arity arguments. Is this not what
> you asked?

Florian, I'm as confused as you. After all, ArgumentError is a runtime
error - an exception. So whatever he does about method invocation etc.
the VM has to be able to deal with exceptions in the same way the
interpreter does. You just invoke the method with the arguments you have
and see what happens. That's it IMHO.

Cheers

robert

Bill Atkins

1/12/2005 7:33:00 PM

0

Guess that's why they call him the Mystifier.... :)

On Wed, 12 Jan 2005 18:21:16 +0900, Robert Klemme <bob.news@gmx.net> wrote:
> Florian, I'm as confused as you. After all, ArgumentError is a runtime

--
$stdout.sync = true
"Just another Ruby hacker.".each_byte do |b|
('a'..'z').step do|c|print c+"\b";sleep 0.007 end;print b.chr
end; print "\n"


Robert Klemme

1/12/2005 8:03:00 PM

0


"Bill Atkins" <batkins57@gmail.com> schrieb im Newsbeitrag
news:66b7e34b0501121133375ec6c9@mail.gmail.com...
> Guess that's why they call him the Mystifier.... :)

LOL

Although.... It appears to me that *he* calls *himself* so. :-)))

Cheers

robert

>
> On Wed, 12 Jan 2005 18:21:16 +0900, Robert Klemme <bob.news@gmx.net>
> wrote:
>> Florian, I'm as confused as you. After all, ArgumentError is a runtime
>
> --
> $stdout.sync = true
> "Just another Ruby hacker.".each_byte do |b|
> ('a'..'z').step do|c|print c+"\b";sleep 0.007 end;print b.chr
> end; print "\n"
>
>