Re: Keyword arguments with symbol as default value

Eric Anderson <[email protected]>
Newsgroups gmane.comp.lang.ruby.general
Message-ID <CAM_Bep0+RAN-Gj0F8dmmeG0CZDMzxNC0uh5L51JPOyG=ThscOg@mail.gmail.com>
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 <[email protected]> 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:[email protected]?subject=unsubscribe>
> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
>


Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.