Why is this not implemented in Ruby

D

Damjan Rems

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
 
J

Jason Roelofs

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
 
A

Avdi Grimm

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.
 
J

John Pritchard-Williams

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....

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
 
S

Sebastian Hungerecker

Jason said:
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.
 
D

Damjan Rems

Jason said:
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
 
F

F. Senault

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
 
A

Avdi Grimm

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.
 
W

William James

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
 
T

Thomas Wieczorek

Le 3 mars 2008 =E0 22:46, John Pritchard-Williams a =E9crit :



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 =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
 
T

Thomas Wieczorek

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
 
N

Nobuyoshi Nakada

Hi,

At Tue, 4 Mar 2008 06:27:50 +0900,
Damjan Rems wrote in [ruby-talk:293399]:
It is probably a good reason but why is this not implemented in ruby.

Because it is ugly, I guess.
 
F

fedzor

People typically use hashes to get around this.



~ Ari
English is like a pseudo-random number generator - there are a
bajillion rules to it, but nobody cares.
 
Y

Yehuda Katz

Nobuyoshi said:
Hi,

At Tue, 4 Mar 2008 06:27:50 +0900,
Damjan Rems wrote in [ruby-talk:293399]:
It is probably a good reason but why is this not implemented in ruby.

Because it is ugly, I guess.

This coming from the language with {|x,| } as acceptable syntax :p

-- Yehuda
 
M

Marc Heiler

aProc(1,,3)

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?
 
T

ThoML

Apart from the solutions already proposes you could also do:
irb(main):005:0> def aProc(a=5,b=6,c=7)

def aProc(a=5,b=nil,c=nil)
b ||= 6
c ||= 7
....
end

and then instead of:
irb(main):009:0> aProc(1,,3)

aProc(1, nil, 3)

If you want to use that many optional arguments that have no "natural"
order, I think though you should use a hash instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top