https://bugs.ruby-lang.org/issues/17768 [?] [ ] Project General Profile * Sign in * Register * Home * Projects * Help Search: [ ] Ruby master [ ] All Projects Ruby >> Ruby master * Overview * Activity * Roadmap * Issues * Wiki * Repository Custom queries * Backport 2.2 * Backport 2.3 * Backport 2.4 * Backport 2.5 * Backport 2.6 * Backport 2.7 * Backport 3.0 * bugs: unassigned * DevelopersMeeting * matz Feature #17768 [e731590022] Proposal: Downward assignments Added by mame (Yusuke Endoh) about 23 hours ago. Updated about 9 hours ago. Status: Open Priority: Normal Assignee: - Target version: - [ruby-core:103135] Tags: joke --------------------------------------------------------------------- Description Rightward assignments have been introduced since 3.0. To be honest, I'm not a big fan of the syntax because it does not add a new dimension to Ruby. Why don't we bring Ruby to the next dimension? ProposalP I propose "downward assignments". p(2 * 3 * 7) #=> 42 ^^^^^var p var #=> 6 This new syntax intercepts the intermediate value of a subexpression. In the above example, the subexpression 2 * 3 is captured to var. You can capture multiple subexpressions in one line. puts("Hello" + "World") #=> HelloWorld ^^^^^^^x ^^^^^^^y p x #=> "Hello" p y #=> "World" This proposal solves some long-standing issues in Ruby. Use case 1P Everyone has written the following code. while (line = gets) != nil p line end This code is not so bad, but there's something that has been on my mind: is it really good to put an assignment into a condition expression? I'm afraid that it makes the loop condition unclear. Unfortunately, it is difficult to keep the condition clear in Ruby. If the assignment is removed from the condition, the code becomes even more unclear as follows. while true line = gets break if line == nil p line end By using my proposal, you can make the condition crystal-clear. while gets != nil ^^^^line p line end Use case 2P Consider that we want to get from an array the last element that meets a condition. ary = [1, 2, 3, 4, 5] ary.each {|elem| found = elem if elem.even? } p found #=> 4 As you know, this code does not work. We need to add found = nil to declare the variable "found" in the outer scope. But this is unarguably dirty. My proposal allows to make the code very straightforward. ary = [1, 2, 3, 4, 5] ary.each {|elem| elem if elem.even? } ^^^^found p found #=> 4 Use case 3P When writing a constructor, we need to write each field name whopping three times. class C def initialize(foo, bar) @foo = foo @bar = bar end end My proposal mitigates the problem to two times. class C def initialize(foo, bar) ^^^@foo ^^^@bar end PatchP A proof-of-concept is attached. $ cat test.rb p(2 * 3 * 7) ^^^^^var p var while gets != nil ^^^^line p line end ary = [1, 2, 3, 4, 5] ary.each {|elem| elem if elem.even? } ^^^^found p found #=> 4 $ echo -e "foo\nbar" | ./miniruby test.rb 42 6 "foo\n" "bar\n" 4 Notes: * The syntax allows only ASCII characters because "East Asian width" is a hell. * My patch does not implement binding a method parameter (Use case 3). * There are some known bugs. Look for them. CompatibilityP A line that suddenly starts with ^ is invalid currently. This is why I chose "downward" since upward assignments are incompatible. vvvv line while gets When the previous line continues, ^ is appropriately handled as an XOR binary operator. x = 1 # The following is considered as: y = 2^x y = 2\ ^x p x #=> 1 p y #=> 3 So, I think this proposal is 100% compatible. DiscussionP I'm unsure how should we handle this. p(2 * 3 * 7) ^^^^^var --------------------------------------------------------------------- Files 2021-aprilfool.patch (9.07 KB) mame (Yusuke Endoh), 04/01/ 2021-aprilfool.patch 2021 12:02 AM * History * Notes #1 [ruby-core:103136] [308cbef6e8] Updated by ko1 (Koichi Sasada) about 23 hours ago super convenient!!1 #2 [ruby-core:103137] [8e497efbe9] Updated by znz (Kazuhiro NISHIYAMA) about 22 hours ago I think this syntax is not irb friendly. #3 [ruby-core:103138] [7cca11c525] Updated by mrkn (Kenta Murata) about 22 hours ago Is this intentional? $ cat test.rb p(2 * 3 * 7) ^^^^^var1 ^^^var2 p var1 p var2 $ ./miniruby test.rb test.rb:3: syntax error, unexpected '^', expecting end-of-input ^^^var2 #4 [ruby-core:103139] [243d1a845b] Updated by marcandre (Marc-Andre Lafortune) about 22 hours ago I'd like a shorthand for cases where we don't really need a variable... while gets != nil p ^^^^ end (It took me a while to realize the date in Japan :-) ) #5 [ruby-core:103141] [e731590022] Updated by mame (Yusuke Endoh) about 22 hours ago marcandre (Marc-Andre Lafortune) wrote in #note-4: (It took me a while to realize the date in Japan :-) ) I waited until 00:00 UTC #6 [ruby-core:103149] [a14d1bca90] Updated by shevegen (Robert A. Heiler) about 10 hours ago plot twist by mame similar to last year: * It's actually not a joke. :) plot twist by matz this year: * He actually WILL approve joke suggestions this year. #7 [ruby-core:103150] [f835d07239] Updated by Dan0042 (Daniel DeLorme) about 10 hours ago What an amazing, beautiful, and revolutionary idea! But what about using an overbar (U+203E) instead of circumflex? It's not that hard to type and it brings extra clarity and visual pleasantness to the code, while eliminating any possibility of conflict with existing syntax. p(2 * 3 * 7) #=> 42 -----var p var #=> 6 puts("Hello" + "World") #=> HelloWorld -------x -------y p x #=> "Hello" p y #=> "World" #8 [ruby-core:103152] [dce43cb815] Updated by Nakilon (Victor Maslov) about 9 hours ago Bidirectional assignments should be implemented to make swapping values: var1, var2 = var2, var1 much shorter: var1 vv^^ var2 Also available in: Atom PDF Powered by Redmine (c) 2006-2021 Jean-Philippe Lang Loading...