[#103241] [Ruby master Bug#17777] 2.6.7 fails to build on macOS: implicit declaration of function 'rb_native_mutex_destroy' is invalid in C99 — eregontp@...
Issue #17777 has been reported by Eregon (Benoit Daloze).
17 messages
2021/04/05
[#103305] [Ruby master Feature#17785] Allow named parameters to be keywords — marcandre-ruby-core@...
Issue #17785 has been reported by marcandre (Marc-Andre Lafortune).
21 messages
2021/04/08
[#103342] [Ruby master Feature#17790] Have a way to clear a String without resetting its capacity — jean.boussier@...
Issue #17790 has been reported by byroot (Jean Boussier).
14 messages
2021/04/09
[#103388] [ANN] Multi-factor Authentication of bugs.ruby-lang.org — SHIBATA Hiroshi <hsbt@...>
Hello,
5 messages
2021/04/12
[#103414] Re: [ANN] Multi-factor Authentication of bugs.ruby-lang.org
— Martin J. Dürst <duerst@...>
2021/04/13
Is there a way to use this multi-factor authentication for (like me)
[#103547] List of CI sites to check — Martin J. Dürst <duerst@...>
Hello everybody,
4 messages
2021/04/22
[#103596] [Ruby master Feature#17830] Add Integer#previous and Integer#prev — rafasoaresms@...
Issue #17830 has been reported by rafasoares (Rafael Soares).
9 messages
2021/04/26
[ruby-core:103503] [Ruby master Feature#17808] Feature Request: JS like splat of Object properties as named method parameters
From:
brad.krane@...
Date:
2021-04-17 23:43:53 UTC
List:
ruby-core #103503
Issue #17808 has been updated by Lithium (Brad Krane).
> But ultimately your whole feature request rely on a very specific JS "feature" (many people would call it a cruft), that extra arguments are simply ignored.
Yes likely but it is very convenient.
> That's not the case in Ruby, every argument must be accounted for. And while I'm merely an observer of the ruby-core development, I'm pretty sure it isn't acceptable to change. So I doubt there any way such feature would be implemented.
>
> Or you'd need to introduce some new syntax for the caller to specify that the extra parameters should be discarded, but again, not very likely to happen.
I agree with you here. I suppose the ultimate question is if I can't redefine to_hash for everything, verified bad idea, and I don't want to mod all methods like in your `ignore_extra_parameters` example, (which is brilliant btw), how can I call my to_hash method with it knowing if it is a ** reduction and if so what caller is so as to be able to yield the appropriate hash for this to work?
Can a methods parameter identify the function it's in before it's called? and then be able to define itself based on the named params of the method after called into?
For this to work a parameter needs to know it's calling method and then react to it appropriately.
`some_method splat(object)` is the ideal syntax but seems impossible and where
where `some_method splat(object)` is really `some_method splat(object, method: some_method)`
so you could detect and emit the correct params as opposed to spamming them.
Towards Marc-Andre
I will have to study the Struct class as this looks promising but I haven't the time atm.
----------------------------------------
Feature #17808: Feature Request: JS like splat of Object properties as named method parameters
https://bugs.ruby-lang.org/issues/17808#change-91601
* 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/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>