Project

General

Profile

Actions

Feature #16364

closed

Top-level ruby2_keywords

Added by mame (Yusuke Endoh) almost 5 years ago. Updated almost 5 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:95918]

Description

How about defining top-level ruby2_keywords?

#!/usr/bin/env ruby

def foo(**kw)
  kw
end

ruby2_keywords def bar(*a)
  foo(*a)
end

bar(k:1) #=> {:k=>1} with no warnings in 2.7

Currently, Module#ruby2_keywords is defined only in Module class. So, to mark a top-level method as ruby2_keywords, we need to workaround by surrounding the definition with class Object; ruby2_keywords ...; end. It is a bit annoying.

I know that practical applications tend to define no top-level methods. However, I think of two reasons that we want top-level ruby2_keywords:

  1. To learn ruby2_keywords, we tend to write a minimal script without class definition. Actually, I saw some people (even a committer) confused due to lack of top-level ruby2_keywords when they are learning ruby2_keywords.
  2. We write small applications that consist of only one script file, e.g., Slack notification script, a simple AWS Lambda, a CGI script for webhooks, etc. I believe top-level methods are actually used in such a script.

So, I think the top-level ruby2_keywords is good to have in 2.7.


Files

top-level-ruby2_keywords.patch (1.57 KB) top-level-ruby2_keywords.patch mame (Yusuke Endoh), 11/24/2019 02:17 AM

Updated by Eregon (Benoit Daloze) almost 5 years ago

Makes sense to me, and agreed it's going to be a lot less confusing with it.

I'm not particularly for ruby2_keywords in general, but if we have Module#ruby2_keywords then I agree main.ruby2_keywords makes sense.

Updated by jeremyevans0 (Jeremy Evans) almost 5 years ago

I'm in favor of adding top level ruby2_keywords.

Actions #4

Updated by mame (Yusuke Endoh) almost 5 years ago

  • Status changed from Open to Closed

Applied in changeset git|3a87826d0c3dd4c42e327e0cd4fb0806d898497f.


vm_method.c: add top-level ruby2_keywords

This is a top-level version of Module#ruby2_keywords.
It can be used for functions (top-level methods) that delegates
arguments. [Feature #16364]

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0