[#106355] [Ruby master Bug#18373] RBS build failure: '/include/x86_64-linux/ruby/config.h', needed by 'constants.o'. — "vo.x (Vit Ondruch)" <noreply@...>
Issue #18373 has been reported by vo.x (Vit Ondruch).
28 messages
2021/12/01
[ruby-core:106677] [Ruby master Feature#15192] Introduce a new "shortcut assigning" syntax to convenient setup instance variables
From:
"austin (Austin Ziegler)" <noreply@...>
Date:
2021-12-14 18:42:04 UTC
List:
ruby-core #106677
Issue #15192 has been updated by austin (Austin Ziegler).
Dan0042 (Daniel DeLorme) wrote in #note-26:
> austin (Austin Ziegler) wrote in #note-22:
> > As far as I understand rightward assignment (I’m not currently using Ruby 3), that’s not something that is legal.
>
> Of course it's not currently legal to use rightward assignment in the method signature, but I think if you compare with positional arguments it's easy to see the similarity:
> `def foo(name = 'Ciri' => node_name)`
> Would be pretty much equivalent to the currently valid:
> `name = 'Ciri' => node_name`
> And I think it's not much of a stretch to go from `name='Ciri' => node_name` to `name:'Ciri' => node_name`, at least for the method signature.
>
> Now if only `name = 'Ciri' => @node_name` was legal (#18408) it would also make sense to use it in the method signature.
The ship has sailed on rightward assignment (I think it’s a mistake and unforgivably ugly, I don’t understand the use case at all, and I suspect that I will never use it), but my comment was specifically about `=> node_name`, which is the case for `name: => node_name`.
> austin (Austin Ziegler) wrote in #note-24:
> > `def name(for: { "a" => 0, "b" => 1 } => for_target, bar: => bar_target)`
>
> You have to admit that example is a bit contrived. I've never seen such a hash used as a default value. :-)
> But really I think everyone understands that the parameters in the method signature have slightly different semantics that the same syntax elsewhere. Depending on the presence of `def`, `foo(bar=42)` is a default value, not an assignment. `foo(bar:)` is a required keyword argument, not a "hash value omission". Etc.
I have used a populated hash as a default value. I haven’t done so frequently, but there are cases where it makes sense.
----------------------------------------
Feature #15192: Introduce a new "shortcut assigning" syntax to convenient setup instance variables
https://bugs.ruby-lang.org/issues/15192#change-95352
* Author: jjyr (Jinyang Jiang)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
Motivation:
Introduce a new syntax for convenient setup instance variables for objects.
The problem:
Currently, setup instance variables in Ruby is too verbose.
Basically, we need to write the meaningless assigning code again and again to assign variables
``` ruby
class Person
def initialize(name:, age:, gender:, country:)
@name = name
@age = age
@gender = gender
@country = country
end
end
# we can use Struct to avoiding this
Person = Struct.new(:name, :age, :gender, :country, keyword_init: true)
# let's see a real-world case, which can't use Struct to describe an initializing process, from https://github.com/ciri-ethereum/ciri/blob/748985ccf7a620a2e480706a5a6b38f56409d487/lib/ciri/devp2p/server.rb#L54
# Because we want to do something more than just assigning instance variables
class Server
def initialize(private_key:, protocol_manage:, bootstrap_nodes: [],
node_name: 'Ciri', tcp_host: '127.0.0.1', tcp_port: 33033)
@private_key = private_key
@node_name = node_name
@bootstrap_nodes = bootstrap_nodes
@protocol_manage = protocol_manage
server_node_id = NodeID.new(@private_key)
caps = [Cap.new(name: 'eth', version: 63)]
@handshake = ProtocolHandshake.new(version: BASE_PROTOCOL_VERSION, name: @node_name, id: server_node_id.id, caps: caps)
@tcp_host = tcp_host
@tcp_port = tcp_port
@dial = Dial.new(bootstrap_nodes: bootstrap_nodes, private_key: private_key, handshake: @handshake)
@network_state = NetworkState.new(protocol_manage)
@dial_scheduler = DialScheduler.new(@network_state, @dial)
end
end
# Introduce a new "shortcut assigning" syntax for convenient setup
class Person
# use @ prefix to describe instance variables.
def initialize(@name:, @age:, @gender:, @country:)
end
# equal to
def initialize2(name:, age:, gender:, country:)
@name = name
@age = age
@gender = gender
@country = country
end
# it should also work on position style arguments
def initialize2(@name, @age, @gender, @country)
end
end
# Our real-world case can be rewritten as below
class Server
def initialize(@private_key:, @protocol_manage:, @bootstrap_nodes: [],
@node_name: 'Ciri', @tcp_host: '127.0.0.1', @tcp_port: 33033)
server_node_id = NodeID.new(@private_key)
caps = [Cap.new(name: 'eth', version: 63)]
@handshake = ProtocolHandshake.new(version: BASE_PROTOCOL_VERSION, name: @node_name, id: server_node_id.id, caps: caps)
@dial = Dial.new(bootstrap_nodes: @bootstrap_nodes, private_key: @private_key, handshake: @handshake)
@network_state = NetworkState.new(@protocol_manage)
@dial_scheduler = DialScheduler.new(@network_state, @dial)
end
end
# consider to keep consistency, this "shortcut assigning" syntax should work for non-initialize methods
class Foo
def bar(@still_works)
p @still_works
end
end
```
--
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>