* [ruby-core:120029] [Ruby master Feature#20914] Constructor Parameter Shortcuts
@ 2024-11-27 22:20 artemb (Artem Borodkin) via ruby-core
2024-11-28 2:25 ` [ruby-core:120032] " nobu (Nobuyoshi Nakada) via ruby-core
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: artemb (Artem Borodkin) via ruby-core @ 2024-11-27 22:20 UTC (permalink / raw)
To: ruby-core; +Cc: artemb (Artem Borodkin)
Issue #20914 has been reported by artemb (Artem Borodkin).
----------------------------------------
Feature #20914: Constructor Parameter Shortcuts
https://bugs.ruby-lang.org/issues/20914
* Author: artemb (Artem Borodkin)
* Status: Open
----------------------------------------
# Constructor Parameter Shortcuts
I propose adding a new syntax to simplify the constructor definition in the classes. Defining a constructor often involves repetitive assignments of parameters to instance variables. This pattern is verbose and can easily become cumbersome, especially in classes with many instance variables. The proposed syntax allows instance variables to be automatically assigned from constructor arguments, improving code clarity and brevity.
**Basic Usage**:
- **Current**:
```ruby
class User
def initialize(name, age, email = nil)
@name = name
@age = age
@email = email
end
end
```
- **Proposed**:
```ruby
class User
def initialize(@name, @age, @email = nil) =
end
```
**Named Parameters**:
- **Current**:
```ruby
class Product
def initialize(name:, price:, description: 'No description available')
@name = name
@price = price
@description = description
end
end
product = Product.new(name: 'Laptop', price: 1000)
```
- **Proposed**:
```ruby
class Product
def initialize(@name:, @price:, @description: 'No description available') =
end
product = Product.new(name: 'Laptop', price: 1000)
```
**Mixing Named and Positional Parameters**:
- **Current**:
```ruby
class Booking
def initialize(date, time, customer:, notes: '')
@date = date
@time = time
@customer = customer
@notes = notes
end
end
booking = Booking.new('2023-04-01', '14:00', customer: 'Alice', notes: 'First-time client')
```
- **Proposed**:
```ruby
class Booking
def initialize(@date, @time, @customer:, @notes: '') =
end
booking = Booking.new('2023-04-01', '14:00', customer: 'Alice', notes: 'First-time client')
```
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 5+ messages in thread
* [ruby-core:120032] [Ruby master Feature#20914] Constructor Parameter Shortcuts
2024-11-27 22:20 [ruby-core:120029] [Ruby master Feature#20914] Constructor Parameter Shortcuts artemb (Artem Borodkin) via ruby-core
@ 2024-11-28 2:25 ` nobu (Nobuyoshi Nakada) via ruby-core
2024-11-28 5:57 ` [ruby-core:120036] " artemb (Artem Borodkin) via ruby-core
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: nobu (Nobuyoshi Nakada) via ruby-core @ 2024-11-28 2:25 UTC (permalink / raw)
To: ruby-core; +Cc: nobu (Nobuyoshi Nakada)
Issue #20914 has been updated by nobu (Nobuyoshi Nakada).
```ruby
def initialize(@name, @age, @email = nil) =
```
What is the equal sign at the end?
`nil` is missed?
----------------------------------------
Feature #20914: Constructor Parameter Shortcuts
https://bugs.ruby-lang.org/issues/20914#change-110773
* Author: artemb (Artem Borodkin)
* Status: Open
----------------------------------------
# Constructor Parameter Shortcuts
I propose adding a new syntax to simplify the constructor definition in the classes. Defining a constructor often involves repetitive assignments of parameters to instance variables. This pattern is verbose and can easily become cumbersome, especially in classes with many instance variables. The proposed syntax allows instance variables to be automatically assigned from constructor arguments, improving code clarity and brevity.
**Basic Usage**:
- **Current**:
```ruby
class User
def initialize(name, age, email = nil)
@name = name
@age = age
@email = email
end
end
```
- **Proposed**:
```ruby
class User
def initialize(@name, @age, @email = nil) =
end
```
**Named Parameters**:
- **Current**:
```ruby
class Product
def initialize(name:, price:, description: 'No description available')
@name = name
@price = price
@description = description
end
end
product = Product.new(name: 'Laptop', price: 1000)
```
- **Proposed**:
```ruby
class Product
def initialize(@name:, @price:, @description: 'No description available') =
end
product = Product.new(name: 'Laptop', price: 1000)
```
**Mixing Named and Positional Parameters**:
- **Current**:
```ruby
class Booking
def initialize(date, time, customer:, notes: '')
@date = date
@time = time
@customer = customer
@notes = notes
end
end
booking = Booking.new('2023-04-01', '14:00', customer: 'Alice', notes: 'First-time client')
```
- **Proposed**:
```ruby
class Booking
def initialize(@date, @time, @customer:, @notes: '') =
end
booking = Booking.new('2023-04-01', '14:00', customer: 'Alice', notes: 'First-time client')
```
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 5+ messages in thread
* [ruby-core:120036] [Ruby master Feature#20914] Constructor Parameter Shortcuts
2024-11-27 22:20 [ruby-core:120029] [Ruby master Feature#20914] Constructor Parameter Shortcuts artemb (Artem Borodkin) via ruby-core
2024-11-28 2:25 ` [ruby-core:120032] " nobu (Nobuyoshi Nakada) via ruby-core
@ 2024-11-28 5:57 ` artemb (Artem Borodkin) via ruby-core
2024-11-28 9:45 ` [ruby-core:120038] " mame (Yusuke Endoh) via ruby-core
2024-11-28 16:56 ` [ruby-core:120046] " artemb (Artem Borodkin) via ruby-core
3 siblings, 0 replies; 5+ messages in thread
From: artemb (Artem Borodkin) via ruby-core @ 2024-11-28 5:57 UTC (permalink / raw)
To: ruby-core; +Cc: artemb (Artem Borodkin)
Issue #20914 has been updated by artemb (Artem Borodkin).
You’re absolutely right; this part does seem ambiguous. I initially added it as an empty block, modeled after the short syntax, like this:
```ruby
def initialize(name, age, email = nil) = (@name, @age, @email = name, age, email)
```
What do you think about this option:
```ruby
def initialize(@name, @age, @email = nil) = ()
```
----------------------------------------
Feature #20914: Constructor Parameter Shortcuts
https://bugs.ruby-lang.org/issues/20914#change-110774
* Author: artemb (Artem Borodkin)
* Status: Open
----------------------------------------
# Constructor Parameter Shortcuts
I propose adding a new syntax to simplify the constructor definition in the classes. Defining a constructor often involves repetitive assignments of parameters to instance variables. This pattern is verbose and can easily become cumbersome, especially in classes with many instance variables. The proposed syntax allows instance variables to be automatically assigned from constructor arguments, improving code clarity and brevity.
**Basic Usage**:
- **Current**:
```ruby
class User
def initialize(name, age, email = nil)
@name = name
@age = age
@email = email
end
end
```
- **Proposed**:
```ruby
class User
def initialize(@name, @age, @email = nil) =
end
```
**Named Parameters**:
- **Current**:
```ruby
class Product
def initialize(name:, price:, description: 'No description available')
@name = name
@price = price
@description = description
end
end
product = Product.new(name: 'Laptop', price: 1000)
```
- **Proposed**:
```ruby
class Product
def initialize(@name:, @price:, @description: 'No description available') =
end
product = Product.new(name: 'Laptop', price: 1000)
```
**Mixing Named and Positional Parameters**:
- **Current**:
```ruby
class Booking
def initialize(date, time, customer:, notes: '')
@date = date
@time = time
@customer = customer
@notes = notes
end
end
booking = Booking.new('2023-04-01', '14:00', customer: 'Alice', notes: 'First-time client')
```
- **Proposed**:
```ruby
class Booking
def initialize(@date, @time, @customer:, @notes: '') =
end
booking = Booking.new('2023-04-01', '14:00', customer: 'Alice', notes: 'First-time client')
```
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 5+ messages in thread
* [ruby-core:120038] [Ruby master Feature#20914] Constructor Parameter Shortcuts
2024-11-27 22:20 [ruby-core:120029] [Ruby master Feature#20914] Constructor Parameter Shortcuts artemb (Artem Borodkin) via ruby-core
2024-11-28 2:25 ` [ruby-core:120032] " nobu (Nobuyoshi Nakada) via ruby-core
2024-11-28 5:57 ` [ruby-core:120036] " artemb (Artem Borodkin) via ruby-core
@ 2024-11-28 9:45 ` mame (Yusuke Endoh) via ruby-core
2024-11-28 16:56 ` [ruby-core:120046] " artemb (Artem Borodkin) via ruby-core
3 siblings, 0 replies; 5+ messages in thread
From: mame (Yusuke Endoh) via ruby-core @ 2024-11-28 9:45 UTC (permalink / raw)
To: ruby-core; +Cc: mame (Yusuke Endoh)
Issue #20914 has been updated by mame (Yusuke Endoh).
> What do you think about this option:
Then this proposal is almost the same as #5825.
----------------------------------------
Feature #20914: Constructor Parameter Shortcuts
https://bugs.ruby-lang.org/issues/20914#change-110776
* Author: artemb (Artem Borodkin)
* Status: Open
----------------------------------------
# Constructor Parameter Shortcuts
I propose adding a new syntax to simplify the constructor definition in the classes. Defining a constructor often involves repetitive assignments of parameters to instance variables. This pattern is verbose and can easily become cumbersome, especially in classes with many instance variables. The proposed syntax allows instance variables to be automatically assigned from constructor arguments, improving code clarity and brevity.
**Basic Usage**:
- **Current**:
```ruby
class User
def initialize(name, age, email = nil)
@name = name
@age = age
@email = email
end
end
```
- **Proposed**:
```ruby
class User
def initialize(@name, @age, @email = nil) =
end
```
**Named Parameters**:
- **Current**:
```ruby
class Product
def initialize(name:, price:, description: 'No description available')
@name = name
@price = price
@description = description
end
end
product = Product.new(name: 'Laptop', price: 1000)
```
- **Proposed**:
```ruby
class Product
def initialize(@name:, @price:, @description: 'No description available') =
end
product = Product.new(name: 'Laptop', price: 1000)
```
**Mixing Named and Positional Parameters**:
- **Current**:
```ruby
class Booking
def initialize(date, time, customer:, notes: '')
@date = date
@time = time
@customer = customer
@notes = notes
end
end
booking = Booking.new('2023-04-01', '14:00', customer: 'Alice', notes: 'First-time client')
```
- **Proposed**:
```ruby
class Booking
def initialize(@date, @time, @customer:, @notes: '') =
end
booking = Booking.new('2023-04-01', '14:00', customer: 'Alice', notes: 'First-time client')
```
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 5+ messages in thread
* [ruby-core:120046] [Ruby master Feature#20914] Constructor Parameter Shortcuts
2024-11-27 22:20 [ruby-core:120029] [Ruby master Feature#20914] Constructor Parameter Shortcuts artemb (Artem Borodkin) via ruby-core
` (2 preceding siblings ...)
2024-11-28 9:45 ` [ruby-core:120038] " mame (Yusuke Endoh) via ruby-core
@ 2024-11-28 16:56 ` artemb (Artem Borodkin) via ruby-core
3 siblings, 0 replies; 5+ messages in thread
From: artemb (Artem Borodkin) via ruby-core @ 2024-11-28 16:56 UTC (permalink / raw)
To: ruby-core; +Cc: artemb (Artem Borodkin)
Issue #20914 has been updated by artemb (Artem Borodkin).
Thank you! This appears to be literally the same proposal, so it can be closed.
I will review #5825 and follow for updates.
----------------------------------------
Feature #20914: Constructor Parameter Shortcuts
https://bugs.ruby-lang.org/issues/20914#change-110782
* Author: artemb (Artem Borodkin)
* Status: Open
----------------------------------------
# Constructor Parameter Shortcuts
I propose adding a new syntax to simplify the constructor definition in the classes. Defining a constructor often involves repetitive assignments of parameters to instance variables. This pattern is verbose and can easily become cumbersome, especially in classes with many instance variables. The proposed syntax allows instance variables to be automatically assigned from constructor arguments, improving code clarity and brevity.
**Basic Usage**:
- **Current**:
```ruby
class User
def initialize(name, age, email = nil)
@name = name
@age = age
@email = email
end
end
```
- **Proposed**:
```ruby
class User
def initialize(@name, @age, @email = nil) =
end
```
**Named Parameters**:
- **Current**:
```ruby
class Product
def initialize(name:, price:, description: 'No description available')
@name = name
@price = price
@description = description
end
end
product = Product.new(name: 'Laptop', price: 1000)
```
- **Proposed**:
```ruby
class Product
def initialize(@name:, @price:, @description: 'No description available') =
end
product = Product.new(name: 'Laptop', price: 1000)
```
**Mixing Named and Positional Parameters**:
- **Current**:
```ruby
class Booking
def initialize(date, time, customer:, notes: '')
@date = date
@time = time
@customer = customer
@notes = notes
end
end
booking = Booking.new('2023-04-01', '14:00', customer: 'Alice', notes: 'First-time client')
```
- **Proposed**:
```ruby
class Booking
def initialize(@date, @time, @customer:, @notes: '') =
end
booking = Booking.new('2023-04-01', '14:00', customer: 'Alice', notes: 'First-time client')
```
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-11-28 16:57 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-27 22:20 [ruby-core:120029] [Ruby master Feature#20914] Constructor Parameter Shortcuts artemb (Artem Borodkin) via ruby-core
2024-11-28 2:25 ` [ruby-core:120032] " nobu (Nobuyoshi Nakada) via ruby-core
2024-11-28 5:57 ` [ruby-core:120036] " artemb (Artem Borodkin) via ruby-core
2024-11-28 9:45 ` [ruby-core:120038] " mame (Yusuke Endoh) via ruby-core
2024-11-28 16:56 ` [ruby-core:120046] " artemb (Artem Borodkin) via ruby-core
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 NNTP newsgroup(s).