From: jean.boussier@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:103483] [Ruby master Feature#17808] Feature Request: JS like splat of Object properties as named method parameters
Date: Fri, 16 Apr 2021 13:43:55 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-91581.20210416134354.51554@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17808.20210416121859.51554@ruby-lang.org
Issue #17808 has been updated by byroot (Jean Boussier).
Doesn't the implicit `to_hash` answer your demand?
```ruby
def foo(bar:)
p bar
end
class MyObject
def initialize(bar)
@bar = bar
end
def to_hash
{ bar: @bar }
end
end
foo(**MyObject.new(42)) # => 42
```
----------------------------------------
Feature #17808: Feature Request: JS like splat of Object properties as named method parameters
https://bugs.ruby-lang.org/issues/17808#change-91581
* Author: Lithium (Brad Krane)
* Status: Open
* Priority: Normal
----------------------------------------
I'm pretty sure there is no equivalent Ruby for a very convenient JS way to enter named function parameters as below:
``` javascript
const test = ({param1,
param2,
param3 = null,
param4 = false,
param5 = null,
}={}) => {
console.log(`${param1}, ${param2}, ${param3}, ${param4}, ${param5}\n`)
}
let obj = {
param1: 23,
param2: 234,
param3: 235,
param4: 257
};
test({...obj});
```
which is super convenient and as far as I'm aware there is no standard Ruby equivalent. It can be accomplished in Ruby but the call syntax is far less nice. A couple examples below:
``` ruby
# Implementing such a feature wouldn't be too difficult.
# Ok so here is what you could do it. Patch Kernel with a method splat. (Sorry in advance for formatting)
module Kernel
def splat obj, method:
new_hash = method.parameters.reduce({}) do |hash, attrr|
hash[attrr.last] = obj.send(attrr.last)
hash
end
end
end
# Then you can pass it a list of symbols.
# Then for your method:
def some_method name:, weight:
puts "#{name} weighs #{weight}"
end
class Dog
attr_reader :name, :weight
def initialize name:,weight:
@name = name
@weight = weight
end
end
a_dog = Dog.new( name: 'fido', weight: '7kg')
hash_puppy = a_dog.splat(a_dog, method: method(:some_method) )
some_method(**hash_puppy)
```
or what I think is a bit better:
``` ruby
# Same class as above
a_dog = Dog.new( name: 'fido', weight: '7kg')
def other_splat obj, method:
new_hash = method.parameters.reduce({}) do |hash, attrr|
if obj.class <= Hash
hash[attrr.last] = obj[attrr.last]
else
hash[attrr.last] = obj.send attrr.last
end
hash
end
method.call **new_hash
end
other_splat(a_dog, method: method(:some_method))
# above line should be like:
# some_method ...a_dog
```
Source: https://gist.github.com/bradkrane/e051d205024a5313cb4a5b9eb1eae0e3
I'm sure I'm missing a possibly more clever way to accomplish this, but I'm pretty sure something like `other_splat(a_dog, method: method(:some_method))` is about as close as it can get, unless I'm missing something? It would be quite nice to have a similar syntax as JS but in Ruby: `some_method ...a_dog`
Thanks for your time and attention!
--
https://bugs.ruby-lang.org/
next prev parent reply other threads:[~2021-04-16 13:44 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-16 12:19 [ruby-core:103481] [Ruby master Feature#17808] Feature Request: JS like splat of Object properties as named method parameters brad.krane
2021-04-16 13:43 ` jean.boussier [this message]
2021-04-16 18:54 ` [ruby-core:103485] " brad.krane
2021-04-16 19:03 ` [ruby-core:103486] " jean.boussier
2021-04-16 19:24 ` [ruby-core:103487] " brad.krane
2021-04-16 21:00 ` [ruby-core:103488] " jean.boussier
2021-04-16 21:05 ` [ruby-core:103489] " marcandre-ruby-core
2021-04-17 23:43 ` [ruby-core:103503] " brad.krane
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-91581.20210416134354.51554@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).