Project

General

Profile

Actions

Feature #15286

closed

Proposal: Add Kernel.#expand(*args)

Added by osyo (manga osyo) about 6 years ago. Updated about 6 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:89737]

Description

This is a suggestion for Hash shorthand.

Kernel.#expand(*args) is expand local variable and method to Hash.
*args is method or local variable names.

Example

def meth
  "meth"
end
a, b, c = 1, 2, 3

# #expand args is local variable or method names
# Expand to { a: a, b: b: meth: meth }
p expand(:a, :b, :meth)
# => {:a=>1, :b=>2, :meth=>"meth"}

# Error: `expand': undefined method `hoge' for main:Object (NoMethodError)
p expand(:hoge)

What can be expanded,

  • local variable
  • method

and, If there are duplicate names, prioritize local variable.

def meth
   "meth"
end
meth = 42

p expand(:meth)
# => {:meth=>42}

pull request: https://github.com/ruby/ruby/pull/2006


Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #15236: add support for hash shorthandRejectedActions

Updated by shevegen (Robert A. Heiler) about 6 years ago

I think one possible question in regards to the suggestion here is whether the above method may be useful
on its own, even without a shorthand syntax for Hash. (This is really just a question; I personally am
not having any strong pro/con opinion.)

I also understand that using a method is different compared to the other two proposals. For example,

{ a }
{ a: a}

and

{x, y}
{x: x, y: y}

is different to:

meth = 42
p expand(:meth) # => {:meth=>42}

So using a method is different to the other two proposals (in the two older
issue request, by Ignatius and Shugo Maeda). Perhaps one or more use cases
could be described for a new method to be useful even without the hash
shorthand notation? I can not think of a good one right now but perhaps others
have some ideas.

Updated by osyo (manga osyo) about 6 years ago

Thanks for comment, shevegen.
Kernel.#expand can be used as follows.

names = [:a, :b, :meth]

# expand(:a, :b, :meth)
expand(*names)
# => {:a=>1, :b=>2, :meth=>"meth"}

# ???
{ *names }

Updated by nobu (Nobuyoshi Nakada) about 6 years ago

Interesting feature, but I don't think the name Kernel#expand is acceptable.
Maybe an instance method of Binding?
And I think it should raise a NameError instead of a NoMethodError on invalid names.

Updated by nobu (Nobuyoshi Nakada) about 6 years ago

And, what do you expect for keywords, e.g., __FILE__, __LINE__, self, super, and etc?

Updated by osyo (manga osyo) about 6 years ago

Thanks nobu :)

Interesting feature, but I don't think the name Kernel#expand is acceptable.

Yes, I looking for a more good name.
Are there any good names?

Maybe an instance method of Binding?

Yes..., but binding.expand(:a, :b, :c) is long...
Kernel.#expand got the idea from Kernel.#local_variables.

And I think it should raise a NameError instead of a NoMethodError on invalid names.

OK, I try :)

And, what do you expect for keywords, e.g., FILE, LINE, self, super, and etc?

Yes, I think it is necessary to discuss what to "expand".
This is a simple implementation with methods and local variables

Actions #6

Updated by matz (Yukihiro Matsumoto) about 6 years ago

Updated by matz (Yukihiro Matsumoto) about 6 years ago

  • Status changed from Open to Rejected

I am against the idea for some reasons:

  • I don't think expand is the right name for the behavior
  • meta-programming is too much for this half-baked substitute for #15236

Regarding #15236, we are waiting for the time when our recognition changed to accept the JS behavior. Currently, we (at least me) recognize {a,b,c} as a literal for a set, not a shorthand for {a:a,b:b,c:c}. I am neutral. I don't want Ruby to follow every JS behavior.

Matz.

Updated by osyo (manga osyo) about 6 years ago

OK, Thank you, matz :)

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0