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.
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):
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.
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.
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.
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
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.
On Mon, Mar 3, 2008 at 11:25 PM, F. Senault [email protected] wrote:
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.
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.
def aProc(options = {})
a = options[:a] || 5
b = options[:b] || 6
c = options[:c] || 7
print a, b, c
end
You could also do like that:
def aProc(options = {})
options = {:a => 5, :b => 6, :c => 7}.merge(options)
print options[:a], options[:b], options[:c]
end
I think this is not intuitive. In this case you’d also have to know the
param in question to omit anyway. Using a hash is better, but for me it
is still a bit hackish. As for the FPDF library you could mail the
author a little patch. Since he is somewhat active I think he would not
mind a slight change. (The thing I personally miss in FPDF is actually
that compared to the php version, we dont have as many addons as the php
folks have :< otherwise i think this is a great library)
The best solution would, IMHO, be keyword arguments.
Someone else wrote that they are in 1.9 but is this true? If so could
one write a tiny snipper example for 1.9 to test with?