[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Why is this not implemented in Ruby

Damjan Rems

3/3/2008 9:28:00 PM


It is probably a good reason but why is this not implemented in ruby.

irb(main):005:0> def aProc(a=5,b=6,c=7)
irb(main):006:1> print a,b,c
irb(main):007:1> end
irb(main):008:0> aProc
567=> nil

irb(main):009:0> aProc(1,,3)
SyntaxError: compile error
(irb):9: syntax error, unexpected ','
aProc(1,,3)
^
from (irb):9
from :0

Why oh why I can not omit parameter in the middle of statement? I am
forced to know its default value if I want to omit a parameter in the
middle or at the begining of the statement.


by
TheR
--
Posted via http://www.ruby-....

15 Answers

Jason Roelofs

3/3/2008 9:37:00 PM

0

Show me a language that does allow you to do this, I've never seen it.
Even then, Ruby doesn't deal with method overloads via parameter
lists, so there's a fundamental reason why it won't work:

def foo(a)
end

def foo(a, b)
end

def foo(a, b, c)
end

foo(1) # => ArgumentError: wrong number of arguments (1 for 3)


Ruby 1.9 has keyword arguments, so you'll be able to get past that
easily then. You can fake it in 1.8 with a hash:

def aProc(options = {})
a = options[:a] || 5
b = options[:b] || 6
c = options[:c] || 7
print a, b, c
end

aProc(:a => 1, :c => 3)

With Ruby 1.9 that will look like (I think, doing this from memory):

aProc(a: 1, c: 3)

Jason

On Mon, Mar 3, 2008 at 4:27 PM, Damjan Rems <d_rems@yahoo.com> wrote:
>
> It is probably a good reason but why is this not implemented in ruby.
>
> irb(main):005:0> def aProc(a=5,b=6,c=7)
> irb(main):006:1> print a,b,c
> irb(main):007:1> end
> irb(main):008:0> aProc
> 567=> nil
>
> irb(main):009:0> aProc(1,,3)
> SyntaxError: compile error
> (irb):9: syntax error, unexpected ','
> aProc(1,,3)
> ^
> from (irb):9
> from :0
>
> Why oh why I can not omit parameter in the middle of statement? I am
> forced to know its default value if I want to omit a parameter in the
> middle or at the begining of the statement.
>
>
> by
> TheR
> --
> Posted via http://www.ruby-....
>
>

Avdi Grimm

3/3/2008 9:40:00 PM

0

On Mon, Mar 3, 2008 at 4:27 PM, Damjan Rems <d_rems@yahoo.com> wrote:
> Why oh why I can not omit parameter in the middle of statement? I am
> forced to know its default value if I want to omit a parameter in the
> middle or at the begining of the statement.

Either rearrange your parameters to put the most likely to be omitted
at the end, or if yo are still running into this issue even after
you've re-ordered the parameters, use an options hash.

--
Avdi

John Pritchard-williams

3/3/2008 9:46:00 PM

0

As an aside: I think it was VB that used to let you do that missing
parameter thing...but its been a while since I saw that....

On 3/3/08, Jason Roelofs <jameskilton@gmail.com> wrote:
> Show me a language that does allow you to do this, I've never seen it.
> Even then, Ruby doesn't deal with method overloads via parameter
> lists, so there's a fundamental reason why it won't work:
>
> def foo(a)
> end
>
> def foo(a, b)
> end
>
> def foo(a, b, c)
> end
>
> foo(1) # => ArgumentError: wrong number of arguments (1 for 3)
>
>
> Ruby 1.9 has keyword arguments, so you'll be able to get past that
> easily then. You can fake it in 1.8 with a hash:
>
> def aProc(options = {})
> a = options[:a] || 5
> b = options[:b] || 6
> c = options[:c] || 7
> print a, b, c
> end
>
> aProc(:a => 1, :c => 3)
>
> With Ruby 1.9 that will look like (I think, doing this from memory):
>
> aProc(a: 1, c: 3)
>
>
> Jason
>
>
> On Mon, Mar 3, 2008 at 4:27 PM, Damjan Rems <d_rems@yahoo.com> wrote:
> >
> > It is probably a good reason but why is this not implemented in ruby.
> >
> > irb(main):005:0> def aProc(a=5,b=6,c=7)
> > irb(main):006:1> print a,b,c
> > irb(main):007:1> end
> > irb(main):008:0> aProc
> > 567=> nil
> >
> > irb(main):009:0> aProc(1,,3)
> > SyntaxError: compile error
> > (irb):9: syntax error, unexpected ','
> > aProc(1,,3)
> > ^
> > from (irb):9
> > from :0
> >
> > Why oh why I can not omit parameter in the middle of statement? I am
> > forced to know its default value if I want to omit a parameter in the
> > middle or at the begining of the statement.
> >
> >
> > by
> > TheR
> > --
> > Posted via http://www.ruby-....
> >
> >
>
>

Sebastian Hungerecker

3/3/2008 10:07:00 PM

0

Jason Roelofs wrote:
> Even then, Ruby doesn't deal with method overloads via parameter
> lists, so there's a fundamental reason why it won't work:
>
> def foo(a)
> end
>
> def foo(a, b)
> end
>
> def foo(a, b, c)
> end
>
> foo(1) # => ArgumentError: wrong number of arguments (1 for 3)

I don't see how this relates to the OP's post at all.
def foo(a=1,b=2,c=3) end
foo(3)
works just fine. You don't need method overloading for that at all. The only
thing that doesn't work is leaving parameters out in the middle and I at
least can't see a good reason why this couldn't work the way the OP suggests.

--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Damjan Rems

3/3/2008 10:11:00 PM

0

Jason Roelofs wrote:
> Show me a language that does allow you to do this, I've never seen it.
> Even then, Ruby doesn't deal with method overloads via parameter
> lists, so there's a fundamental reason why it won't work:
Clipper,Alaska xBase++. Omitted parameter is treated as nil. They can be
tested as nil on the called site. If the value is nil some default value
is set.

>
> def foo(a)
> end
>
> def foo(a, b)
> end
>
> def foo(a, b, c)
> end
>
> foo(1) # => ArgumentError: wrong number of arguments (1 for 3)
This is absolutly correct. All these parameters are required. But if I
write
def foo(a,b=2,c=3)
end
First parameter is requested others are optional. It would be nice if I
could call foo(1,,4). b would have got default value of 2.

>
>
> Ruby 1.9 has keyword arguments, so you'll be able to get past that
> easily then. You can fake it in 1.8 with a hash:
>
> def aProc(options = {})
> a = options[:a] || 5
> b = options[:b] || 6
> c = options[:c] || 7
> print a, b, c
> end

I know this and hashes are great. Problem is of course libraries that
are not written by me.

For example. I am using FPDF library which has method Cell.
Cell(w,h=0,txt='',border=0,ln=0,align='',fill=0,link='')

I would like to align to right and I don't care about border or ln
parameter.
Cell(1,,'100.000,00',,,'R') would be my preferred solution.

And I know FPDF is ugly written library. But it supports using Eastern
European characters unlike some other libraries.

by
TheR
--
Posted via http://www.ruby-....

F. Senault

3/3/2008 10:20:00 PM

0

Le 3 mars 2008 à 22:46, John Pritchard-Williams a écrit :

> As an aside: I think it was VB that used to let you do that missing
> parameter thing...but its been a while since I saw that....

Yes, definitely VB6.

And, no, it's not been a while since I saw that. Alas.

Fred
--
I can imagine the moment Breaking out through the silence All the
things that we both might say And the heart it will not be denied
'Til we're both on the same damn side All the barriers blown away
(Peter Gabriel, Come Talk to Me)

Avdi Grimm

3/3/2008 10:49:00 PM

0

On Mon, Mar 3, 2008 at 5:11 PM, Damjan Rems <d_rems@yahoo.com> wrote:
> I would like to align to right and I don't care about border or ln
> parameter.
> Cell(1,,'100.000,00',,,'R') would be my preferred solution.
>
> And I know FPDF is ugly written library. But it supports using Eastern
> European characters unlike some other libraries.

Why not write a little wrapper method that only has the parameters you
care about and fills in the blanks with the defaults? That way you
only have to look up the defaults once.

--
Avdi

William James

3/4/2008 12:09:00 AM

0



Jason Roelofs wrote:

> > aProc(1,,3)
> > ^
> > from (irb):9
> > from :0
> >
> > Why oh why I can not omit parameter in the middle of statement? I am
> > forced to know its default value if I want to omit a parameter in the
> > middle or at the begining of the statement.

> Show me a language that does allow you to do this, I've never seen it.

FreeBASIC:

sub foo( a as integer, b as integer = 0, c as integer)
print using "### ### ###"; a,b,c
end sub

foo 3,,5

---> 3 0 5

Thomas Wieczorek

3/4/2008 12:52:00 AM

0

On Mon, Mar 3, 2008 at 11:25 PM, F. Senault <fred@lacave.net> wrote:
> Le 3 mars 2008 =E0 22:46, John Pritchard-Williams a =E9crit :
>
>
> > As an aside: I think it was VB that used to let you do that missing
> > parameter thing...but its been a while since I saw that....
>
> Yes, definitely VB6.
>

VB.Net still supports it in Late Binding, not sure about Early
Binding, I have seen that in an PowerPoint automation example just a
few days ago, when I worked on an recent project. I think it gets than
the optional value.
I like the hashed solution more, than leaving an empty space, between
two commatas. IMO it's more beautiful. Named parameters in Ruby 1.9
look also nice.



On Mon, Mar 3, 2008 at 10:36 PM, Jason Roelofs <jameskilton@gmail.com> wrot=
e:
>
> def aProc(options =3D {})
> a =3D options[:a] || 5
> b =3D options[:b] || 6
> c =3D options[:c] || 7
> print a, b, c
> end
>

You could also do like that:
def aProc(options =3D {})
options =3D {:a =3D> 5, :b =3D> 6, :c =3D> 7}.merge(options)
print options[:a], options[:b], options[:c]
end

Thomas Wieczorek

3/4/2008 12:53:00 AM

0

On Mon, Mar 3, 2008 at 10:36 PM, Jason Roelofs <jameskilton@gmail.com> wrote:
> Show me a language that does allow you to do this, I've never seen it.
> Even then, Ruby doesn't deal with method overloads via parameter
> lists, so there's a fundamental reason why it won't work:
>
> def foo(a)
> end
>
> def foo(a, b)
> end
>
> def foo(a, b, c)
> end
>
> foo(1) # => ArgumentError: wrong number of arguments (1 for 3)
>
>
> Ruby 1.9 has keyword arguments, so you'll be able to get past that
> easily then. You can fake it in 1.8 with a hash:
>
> def aProc(options = {})
> a = options[:a] || 5
> b = options[:b] || 6
> c = options[:c] || 7
> print a, b, c
> end
>
> aProc(:a => 1, :c => 3)
>
> With Ruby 1.9 that will look like (I think, doing this from memory):
>
> aProc(a: 1, c: 3)
>
> Jason
>
>
>
> On Mon, Mar 3, 2008 at 4:27 PM, Damjan Rems <d_rems@yahoo.com> wrote:
> >
> > It is probably a good reason but why is this not implemented in ruby.
> >
> > irb(main):005:0> def aProc(a=5,b=6,c=7)
> > irb(main):006:1> print a,b,c
> > irb(main):007:1> end
> > irb(main):008:0> aProc
> > 567=> nil
> >
> > irb(main):009:0> aProc(1,,3)
> > SyntaxError: compile error
> > (irb):9: syntax error, unexpected ','
> > aProc(1,,3)
> > ^
> > from (irb):9
> > from :0
> >
> > Why oh why I can not omit parameter in the middle of statement? I am
> > forced to know its default value if I want to omit a parameter in the
> > middle or at the begining of the statement.
> >
> >
> > by
> > TheR
> > --
> > Posted via http://www.ruby-....
> >
> >
>
>