Why is this not implemented in Ruby

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

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

···

from :0
--
Posted via http://www.ruby-forum.com/.

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-forum.com/\.

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.

···

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.

--
Avdi

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.

···

--
Nobu Nakada

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.

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-forum.com/\.
>
>

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

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-forum.com/\.

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

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

>
> 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-forum.com/\.
>
>

···

On Mon, Mar 3, 2008 at 10:36 PM, Jason Roelofs <jameskilton@gmail.com> wrote:

On Mon, Mar 3, 2008 at 4:27 PM, Damjan Rems <d_rems@yahoo.com> wrote:

Nobuyoshi Nakada wrote:

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 :stuck_out_tongue:

-- Yehuda

···

--
Posted via http://www.ruby-forum.com/\.

Yes, definitely VB6.

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

Fred

···

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

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

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

--
Avdi

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.

···

On Mar 3, 2008, at 7:10 PM, William James wrote:

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.

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?

···

--
Posted via http://www.ruby-forum.com/\.

> 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 11:25 PM, F. Senault <fred@lacave.net> wrote:

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

On Mon, Mar 3, 2008 at 10:36 PM, Jason Roelofs <jameskilton@gmail.com> wrote:

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