Project

General

Profile

Actions

Feature #17290

closed

Syntax sugar for boolean keyword argument

Added by sawa (Tsuyoshi Sawada) over 3 years ago. Updated about 1 year ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:100630]

Description

We frequently use keyword arguments just to pass true value out of the truthy/falsy options given. And in many such cases, the falsy option is set as the default, and only the truthy value is ever passed explicitly. I propose to have a syntax sugar to omit the value of a keyword argument. When omitted, it should be interpreted with value true.

gets(chomp:)
CSV.parse(" foo var ", strip:)

should be equivalent to

gets(chomp: true)
CSV.parse(" foo var ", strip: true)

Additionally, we may also extend this to pragmas.

# frozen_string_literal:

to be equivalent to:

# frozen_string_literal: true

Updated by marcandre (Marc-Andre Lafortune) over 3 years ago

I'm personally hoping that strip: could be syntax sugar for strip: strip...

Updated by baweaver (Brandon Weaver) over 3 years ago

I would concur with Marc-Andre on this one, I believe punning would be a more valuable feature, especially for keyword arguments.

Examples:

def method_name(foo: 1, bar: 2, baz: 3)
  foo + bar + baz
end

foo = 1
bar = 2
baz = 3

method_name(foo:, bar:, baz:) # 1, 2, 3
# => 6

# Also punning in hashes which would be distinct from block syntax:
{ foo:, bar:, baz: }
# => { foo: 1, bar: 2, baz: 3 }

May open a separate ticket on those ideas or amend a current punning proposition

Edit - opened a ticket

Updated by sawa (Tsuyoshi Sawada) over 1 year ago

I think Marc-Andre and Brandon's idea can be limited to be relevant only for receiving the parameters (in a definition), and it will still make sense. My proposal is is about sending the parameters (in a method call). I think they can be kept distinct.

I rarely see people using gets(chomp: true), and I think it is because it is shorter to write gets.chomp. It is a pity that such a useful feature was introduced, but no one uses it because the old school way is more convenient.

Updated by sawa (Tsuyoshi Sawada) about 1 year ago

I withdraw this proposal. Please close it. I came up with a better idea #19559.

Actions #5

Updated by jeremyevans0 (Jeremy Evans) about 1 year ago

  • Status changed from Open to Closed
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0