Convert hash to kwargs in 2.7+

Hi everyone,

I have a subclass of File where I do some custom stuff in the constructor.
The short version is that the declaration looks something like this:

def initialize(alpha: true, beta: false, options: {})
  # do stuff with alpha/beta
  options[:mode] ||= 'wb+'
  super(path, options)
end

In the past Ruby would convert the last positional hash to keyword
arguments. But with Ruby 3.0 this blows up.

What's the right way to declare this in a way that will work with both 2.x
and 3.x?

Regards,

Dan

Hello,

   try a keyword args splat e.g. ** (sorry not sure if that's the
correct technical term)

def initialize(alpha: true, beta: false, **options)
  # do stuff with alpha/beta
  options[:mode] ||= 'wb+'
  super(path, options)
end

    Cheers. Prost.

Al solved it, you have to not only use the splat in the definition, you
have to "foward" the splat, so "super(path, **options)". That worked.

(sorry, meant to post my original reply to both Al and the list)

Cheers,

Dan

···

On Mon, Dec 28, 2020 at 1:14 PM Gerald Bauer <gerald.bauer@gmail.com> wrote:

Hello,

   try a keyword args splat e.g. ** (sorry not sure if that's the
correct technical term)

def initialize(alpha: true, beta: false, **options)
  # do stuff with alpha/beta
  options[:mode] ||= 'wb+'
  super(path, options)
end

    Cheers. Prost.

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