Default values for method arguments

P

paul

Hi all,

Probably I'm trying something impossible, but when giving default
values for arguments to a method call, do I have to give default-values
for *all* arguments?

irb(main):001:0> class A
irb(main):002:1> def a(b = 0, c, d = '', e)
irb(main):003:2> puts b, c, d, e
irb(main):004:2> end
irb(main):005:1> end
SyntaxError: compile error
(irb):2: syntax error
def a(b = 0, c, d = '', e)
^
(irb):2: syntax error
(irb):5: syntax error
from (irb):5

And also when giving default-values to all arguments, omitting
arguments when calling the method doesn't seem to be a succes:

irb(main):001:0> class B
irb(main):002:1> def b(c = 0, d = 1, e = 2)
irb(main):003:2> puts c, d, e
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> B.new.b(1,2,3)
1
2
3
=> nil
irb(main):007:0> B.new.b(1,,3)
SyntaxError: compile error
(irb):7: syntax error
B.new.b(1,,3)
^
from (irb):7

Is it impossible or I'm I just coding it wrong?

Cheers,
Paul
 
J

James Edward Gray II

Probably I'm trying something impossible, but when giving default
values for arguments to a method call, do I have to give default-
values
for *all* arguments?

No, but all the arguments that don't have defaults must come before
(to the left of) those that do.
And also when giving default-values to all arguments, omitting
arguments when calling the method doesn't seem to be a succes:

irb(main):001:0> class B
irb(main):002:1> def b(c = 0, d = 1, e = 2)
irb(main):003:2> puts c, d, e
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> B.new.b(1,2,3)
1
2
3
=> nil
irb(main):007:0> B.new.b(1,,3)
SyntaxError: compile error
(irb):7: syntax error
B.new.b(1,,3)
^
from (irb):7

Again, you can only leave out arguments that come after (to the right
of) those you provide.
Is it impossible or I'm I just coding it wrong?

You just need to switch strategies. Use Ruby's simulated keyword
arguments for something like this:
1
nil
2
=> nil

Hope that helps.

James Edward Gray II
 
P

paul

You just need to switch strategies. Use Ruby's simulated keyword
arguments for something like this:

1
nil
2
=> nil

Hi James,

The Simulated Keyword Arguments you talk about are a little abstract to
me, but I think I get the idea. I suppose in the example above I would
have to add a couple of lines to define the default values...
This way, the general idea is the same as the following?

irb(main):001:0> def test(args)
irb(main):002:1> args['a'] = 'defaultA' unless args.has_key?('a')
irb(main):003:1> args['b'] = 'defaultB' unless args.has_key?('b')
irb(main):004:1> args['c'] = 'defaultC' unless args.has_key?('c')
irb(main):005:1> puts args.indexes('a', 'b', 'c')
irb(main):006:1> end
=> nil
irb(main):007:0> test({'a' => 'someA', 'c' => 'someC'})
(irb):5: warning: Hash#indexes is deprecated; use Hash#values_at
someA
defaultB
someC
=> nil

An additional question: do you know a link where I can find more on
those Simulated Keyword Arguments? I did some googling, but it turned
up with nothing on:
ruby "Simulated Keyword Arguments"

Those colons you use before the variable-names are a little like magic
to me. (though I checked, without those colons, you're example above
wouldn't have worked...) :)

Cheers,
Paul
 
J

James Edward Gray II

The Simulated Keyword Arguments you talk about are a little
abstract to
me, but I think I get the idea.

My fault, I did not describe them very well. Here are the critical
bits of information:

If a method call ends with some arguments that look like a Hash
definition (using the fat arrows =>), Ruby collects those and passes
a single Hash to the method in their place. You can then work with
the Hash normally.
irb(main):001:0> def test(args)
irb(main):002:1> args['a'] = 'defaultA' unless args.has_key?('a')
irb(main):003:1> args['b'] = 'defaultB' unless args.has_key?('b')
irb(main):004:1> args['c'] = 'defaultC' unless args.has_key?('c')
irb(main):005:1> puts args.indexes('a', 'b', 'c')
irb(main):006:1> end
=> nil
irb(main):007:0> test({'a' => 'someA', 'c' => 'someC'})
(irb):5: warning: Hash#indexes is deprecated; use Hash#values_at
someA
defaultB
someC
=> nil

Another way to set defaults:
?> "b" => "default b",
?> "c" => "default c" }.merge(args)some a
default b
some c
=> nil
An additional question: do you know a link where I can find more on
those Simulated Keyword Arguments?

See Collecting Hash Arguments at the bottom of:

http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_methods.html
Those colons you use before the variable-names are a little like magic
to me. (though I checked, without those colons, you're example above
wouldn't have worked...) :)

Those are just Symbol objects. Don't lose too much sleep over them
yet, as you can see from the new example Strings work fine too.

James Edward Gray II
 
A

ara.t.howard

Try this-- it's more concise, and you probably won't feel the need
for more syntactic magic after that.

def test(args)
args[:a] ||= 'defaultA'
args[:b] ||= 'defaultB'
args[:c] ||= 'defaultC'
puts args.indexes:)a, :b, :c)
end


but this breaks

test :a => false

as does this

test :a => nil

and this

test 'a' => true

regards.

-a
 
D

David Vallner

--------------enig76C2E74EC4D20351F9B9017D
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Ken said:
Try this-- it's more concise, and you probably won't feel the need
for more syntactic magic after that.
=20
def test(args)
args[:a] ||=3D 'defaultA'
args[:b] ||=3D 'defaultB'
args[:c] ||=3D 'defaultC'
puts args.indexes:)a, :b, :c)
end
=20

I find:

def test(args)
realargs =3D {:a =3D> 'defaultA',
:b =3D> 'defaultB',
:c =3D> 'defaultC'}.update(args)

puts args.indexes:)a, :b, :c)
end

more readable. Preferrably extracting the default values into constants
before the method definition and (deep?)cloning the hash.

Also, your code clobbers the hash put into args by putting in values.
This might not cause bugs most of the case, but might in case someone
passes in an actual hash, so I'd just stay on the safe side.

David Vallner


--------------enig76C2E74EC4D20351F9B9017D
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFeSCcy6MhrS8astoRAlSWAJ4j1bfWNoqvQyn9dNfjz0PRsvpITwCdHtak
hYW7ABELXOYbcHNR/CgJa2I=
=TCMD
-----END PGP SIGNATURE-----

--------------enig76C2E74EC4D20351F9B9017D--
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top