[#103135] [Ruby master Feature#17768] Proposal: Downward assignments — mame@...

Issue #17768 has been reported by mame (Yusuke Endoh).

10 messages 2021/04/01

[#103162] [Ruby master Feature#17773] Alias `Numeric#zero?` and `Float#zero?` as `Numeric#empty?` and `Float#empty?` — sawadatsuyoshi@...

Issue #17773 has been reported by sawa (Tsuyoshi Sawada).

9 messages 2021/04/02

[#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

[#103280] [Ruby master Bug#17781] Resolv::DNS RequestID table allocations are never freed, causing DNS lookups to eventually hang — supermathie@...

Issue #17781 has been reported by supermathie (Michael Brown).

9 messages 2021/04/07

[#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

[#103310] [Ruby master Feature#17786] Proposal: new "ends" keyword — jzakiya@...

Issue #17786 has been reported by jzakiya (Jabari Zakiya).

13 messages 2021/04/08

[#103317] [Ruby master Bug#17787] Four AIX build issues with xlc compiler and ruby-3.0.1 — lamont@...

Issue #17787 has been reported by lamont (Lamont Granquist).

9 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

[#103386] [Ruby master Bug#17793] `shorten-64-to-32` error for 32-bit Android due to `struct stat` definition — xtkoba+ruby@...

Issue #17793 has been reported by xtkoba (Tee KOBAYASHI).

8 messages 2021/04/11

[#103400] [Ruby master Feature#17795] `before_fork` and `after_fork` callback API — jean.boussier@...

Issue #17795 has been reported by byroot (Jean Boussier).

42 messages 2021/04/12

[#103434] [Ruby master Bug#17799] Seg fault in rb_class_clear_method_cache — stanhu@...

Issue #17799 has been reported by stanhu (Stan Hu).

14 messages 2021/04/13

[#103481] [Ruby master Feature#17808] Feature Request: JS like splat of Object properties as named method parameters — brad.krane@...

Issue #17808 has been reported by Lithium (Brad Krane).

8 messages 2021/04/16

[#103556] [Ruby master Bug#17820] `Errno::EINVAL` from `Process.kill` with available signal on Windows — alex.wayfer@...

Issue #17820 has been reported by AlexWayfer (Alexander Popov).

9 messages 2021/04/22

[#103591] [Ruby master Bug#17827] Monitor is not fiber safe — samuel@...

Issue #17827 has been reported by ioquatix (Samuel Williams).

11 messages 2021/04/25

[#103593] [Ruby master Misc#17828] Deprecate use of master and slave — yyoshida.at.work@...

Issue #17828 has been reported by yyoshida.at.work@gmail.com (Yasuhiro Yoshida).

10 messages 2021/04/26

[#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

[#103631] [Ruby master Feature#17837] Add support for Regexp timeouts — sam.saffron@...

Issue #17837 has been reported by sam.saffron (Sam Saffron).

45 messages 2021/04/27

[ruby-core:103485] [Ruby master Feature#17808] Feature Request: JS like splat of Object properties as named method parameters

From: brad.krane@...
Date: 2021-04-16 18:54:01 UTC
List: ruby-core #103485
Issue #17808 has been updated by Lithium (Brad Krane).


Thanks for the quick update. 

That almost works but to automate this the to_hash function needs to be aware of the methods named arguments. The below works in one case and fails in another with an object having more instance variables than the called method and it throws a `[ArgumentError] unknown keyword: :color` as there is now an extra parameter included that is not one of the named method params.

Also would this be dangerous to begin with modifying the Kernel to_hash?  


``` ruby
module Kernel
  def to_hash
    new_hash = self.instance_variables.reduce({}) do |hash, instance_var|
      hash[instance_var.to_s[1..-1].to_sym] = self.instance_variable_get instance_var
      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'

some_method(**a_dog)  # Works great!

class CockerSpanel < Dog
  def initialize name:,weight:,color:
    super name: name, weight: weight
    @color = color
  end
end

another_dog = CockerSpanel.new name: 'Jessie', weight: '5kg', color: 'black'

some_method **another_dog

```


NVM on the hash question it is a problem pasting into irb crashes with 


``` shell
C:/Ruby27-x64/lib/ruby/2.7.0/reline/unicode.rb:70: warning: Using the last argument as keyword parameters is deprecated
C:/Ruby27-x64/lib/ruby/2.7.0/reline/unicode.rb:70: warning: Using the last argument as keyword parameters is deprecated
C:/Ruby27-x64/lib/ruby/2.7.0/reline/unicode.rb:70: warning: Using the last argument as keyword parameters is deprecated
C:/Ruby27-x64/lib/ruby/2.7.0/reline/unicode.rb:70: warning: Using the last argument as keyword parameters is deprecated
C:/Ruby27-x64/lib/ruby/2.7.0/reline/unicode.rb:70: warning: Using the last argument as keyword parameters is deprecated
C:/Ruby27-x64/lib/ruby/2.7.0/reline/line_editor.rb:1080: warning: Using the last argument as keyword parameters is deprecated
C:/Ruby27-x64/lib/ruby/2.7.0/reline/line_editor.rb:1063: warning: Using the last argument as keyword parameters is deprecated
C:/Ruby27-x64/lib/ruby/2.7.0/reline/unicode.rb:70: warning: Using the last argument as keyword parameters is deprecated
C:/Ruby27-x64/lib/ruby/2.7.0/reline/unicode.rb:70: warning: Using the last argument as keyword parameters is deprecated
C:/Ruby27-x64/lib/ruby/2.7.0/reline/unicode.rb:70: warning: Using the last argument as keyword parameters is deprecated
C:/Ruby27-x64/lib/ruby/2.7.0/reline/unicode.rb:70: warning: Using the last argument as keyword parameters is deprecated
C:/Ruby27-x64/lib/ruby/2.7.0/reline/unicode.rb:70: warning: Using the last argument as keyword parameters is deprecated
C:/Ruby27-x64/lib/ruby/2.7.0/reline/unicode.rb:70: warning: Using the last argument as keyword parameters is deprecated
C:/Ruby27-x64/lib/ruby/2.7.0/irb/ext/save-history.rb:110: warning: Using the last argument as keyword parameters is deprecated
Traceback (most recent call last):
        13096: from C:/Ruby27-x64/bin/irb.cmd:31:in `<main>'
        13095: from C:/Ruby27-x64/bin/irb.cmd:31:in `load'
        13094: from C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/irb-1.2.3/exe/irb:11:in `<top (required)>'
        13093: from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:399:in `start'
        13092: from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:470:in `run'
        13091: from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:470:in `catch'
        13090: from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:471:in `block in run'
        13089: from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:536:in `eval_input'
         ... 13084 levels...
            4: from C:/Ruby27-x64/lib/ruby/2.7.0/reline/line_editor.rb:93:in `block in reset'
            3: from C:/Ruby27-x64/lib/ruby/2.7.0/reline/line_editor.rb:93:in `block in reset'
            2: from C:/Ruby27-x64/lib/ruby/2.7.0/reline/line_editor.rb:93:in `block in reset'
            1: from C:/Ruby27-x64/lib/ruby/2.7.0/reline/line_editor.rb:93:in `block in reset'
C:/Ruby27-x64/lib/ruby/2.7.0/reline/line_editor.rb:93:in `block in reset': stack level too deep (SystemStackError)
```

Running the file itself we get

``` shell
C:\Users\Brad Krane\Documents\src\ruby-splat>ruby ruby-splat.rb
fido weighs 7kg
Traceback (most recent call last):
        1: from james.rb:38:in `<main>'
ruby-splat.rb:13:in `some_method': unknown keyword: :color (ArgumentError)

C:\Users\Brad Krane\Documents\src\ruby-splat>
```


How could I automate the to_hash knowing the method arguments required so as to prevent the `unknown keyword: :color (ArgumentError)` Or should I be specifying my to_hash methods individually and if I did how can I specify the correct parameters?

----------------------------------------
Feature #17808: Feature Request: JS like splat of Object properties as named method parameters
https://bugs.ruby-lang.org/issues/17808#change-91583

* 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>

In This Thread