[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: Keyword arguments with symbol as default value
From:       Eric Anderson <eric () bloodhub ! com>
Date:       2019-04-22 17:03:47
Message-ID: CAM_Bep0+RAN-Gj0F8dmmeG0CZDMzxNC0uh5L51JPOyG=ThscOg () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


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

[Attachment #5 (text/html)]

<div dir="ltr"><div dir="ltr"><div>Can&#39;t you just put the symbol directly in the \
method definition?</div><div><br></div><div style="margin-left:40px"><span \
style="font-family:monospace,monospace">$ irb<br>irb(main):001:0&gt; def foo bar: \
:baz<br>irb(main):002:1&gt; puts bar<br>irb(main):003:1&gt; end<br>=&gt; \
:foo<br>irb(main):004:0&gt; foo<br>baz<br>=&gt; \
nil</span><br></div><div><br></div><div><br></div></div></div><br><div \
class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Apr 22, 2019 at 12:52 \
PM RRRoy BBBean &lt;<a \
href="mailto:rrroybbbean@gmail.com">rrroybbbean@gmail.com</a>&gt; \
wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I need to use a Ruby \
symbol as the default value for an argument, but I <br> also want to use keyword \
arguments,   because they are so neat.<br> <br>
I found that wrapping the symbol in in parenthesis or using a constant <br>
to refer to the symbol works.<br>
<br>
Is there a better way to do this with Ruby 2.6+ (and still have a symbol <br>
as the default value)?<br>
<br>
def mymethod myarg:(:mydefaultsymbol)<br>
<br>
         p myarg<br>
<br>
end<br>
<br>
# or<br>
<br>
DEFAULT_MYVALUE = :mydefaultsymbol<br>
<br>
def mymethod myarg:DEFAULT_MYVALUE<br>
<br>
         p myarg<br>
<br>
end<br>
<br>
# where, in either case, mymethod # outputs :mydefaultsymbol<br>
<br>
<br>
<br>
Unsubscribe: &lt;mailto:<a href="mailto:ruby-talk-request@ruby-lang.org" \
target="_blank">ruby-talk-request@ruby-lang.org</a>?subject=unsubscribe&gt;<br> \
&lt;<a href="http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk" \
rel="noreferrer" target="_blank">http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk</a>&gt;<br>
 </blockquote></div>



Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic