Skip to content

Local variables whose name is a keyword #367

@akimd

Description

@akimd

Hi Markus,

I have a function that takes a named argument named return. The usual trick to capture it in modern Rubies (shorter and faster that looking into binding) is {return:}[:return].

Unfortunately, unparser simply prints what parser gave, so it turns a "local variable" named return into an actual return keyword.

require 'parser/current'
require 'unparser'

ast = Unparser.parse(<<~RUBY)
  def foo(return:)
    puts({return:}[:return])
  end
RUBY
p ast
puts Unparser.unparse(ast)
$ bundle exec /tmp/unparser.rb
s(:def, :foo,
  s(:args,
    s(:kwarg, :return)),
  s(:send, nil, :puts,
    s(:index,
      s(:hash,
        s(:pair,
          s(:sym, :return),
          s(:lvar, :return))),
      s(:sym, :return))))
def foo(return:)
  puts({ return: return }[:return])
end

Of course the generated file then fails to run properly.

As far as I'm concerned, this is not urgent: I used binding for this particular case.

Cheers!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions