I need to use a Ruby symbol as the default value for an argument, but I also want to use keyword arguments, because they are so neat.
I found that wrapping the symbol in in parenthesis or using a constant to refer to the symbol works.
Is there a better way to do this with Ruby 2.6+ (and still have a symbol as the default value)?
def mymethod myarg:(:mydefaultsymbol)
p myarg
end
# or
DEFAULT_MYVALUE = :mydefaultsymbol
def mymethod myarg:DEFAULT_MYVALUE
p myarg
end
# where, in either case, mymethod # outputs :mydefaultsymbol
Can't you just put the symbol directly in the method definition?
$ irb
irb(main):001:0> def foo bar: :baz
irb(main):002:1> puts bar
irb(main):003:1> end
=> :foo
irb(main):004:0> foo
baz
=> nil
ยทยทยท
On Mon, Apr 22, 2019 at 12:52 PM RRRoy BBBean <rrroybbbean@gmail.com> wrote:
I need to use a Ruby symbol as the default value for an argument, but I
also want to use keyword arguments, because they are so neat.
I found that wrapping the symbol in in parenthesis or using a constant
to refer to the symbol works.
Is there a better way to do this with Ruby 2.6+ (and still have a symbol
as the default value)?
def mymethod myarg:(:mydefaultsymbol)
p myarg
end
# or
DEFAULT_MYVALUE = :mydefaultsymbol
def mymethod myarg:DEFAULT_MYVALUE
p myarg
end
# where, in either case, mymethod # outputs :mydefaultsymbol
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>