From: "mame (Yusuke Endoh)" Date: 2012-03-26T21:21:25+09:00 Subject: [ruby-core:43682] [ruby-trunk - Feature #6201][Assigned] do_something then return :special_case (include "then" operator) Issue #6201 has been updated by mame (Yusuke Endoh). Status changed from Feedback to Assigned Target version changed from 2.0.0 to 3.0 Hello, Rodrigo In short, you are proposing a syntactic sugar: A then B as (A; B) , so that you can rewrite the following idiom: (render(...); return) if condition? with: render(...) then return if condition? , right? I think it will cause compatibility issue. if A then B C end will be parsed as: if (A; B) C end So, 2.0 cannot include your idea. I mark this ticket as 3.0. Personally, I like your idea very much. I often hesitate to write *whopping* four lines: if C A B end when A, B and C are all short and simple. I feel like defeat when I use a semicolon ;-) In addition, it is cool to use "then" which is one of the least useful keywords ("for", "alias", ...) in Ruby. So, I'm sorry that I can't accept your idea in 2.0. BTW, when you write a proposal, I recommend you put a short example first to demonstrate your idea. It will not only be attractive to more people, but also make our ticket management task easier :-) Thanks, -- Yusuke Endoh ---------------------------------------- Feature #6201: do_something then return :special_case (include "then" operator) https://bugs.ruby-lang.org/issues/6201#change-25179 Author: rosenfeld (Rodrigo Rosenfeld Rosas) Status: Assigned Priority: Normal Assignee: matz (Yukihiro Matsumoto) Category: Target version: 3.0 =begin I've read several aproaches to deal with this case and this just feels like Ruby doesn't have a good idiom yet for the common use case. You want to do some special action under certain conditions and then return the method. I've once proposed some way to return from the callee: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/41681 But I agree with other that found that approach a dangerous one. One of the common idioms I see is writing code like this: render action: NOT_FOUND and return The problem with this is that it won't work if render returns nil or false. And render return value isn't supposed to have any meaning. So while reading some articles this issue got back to me, but this time I think I found a more elegant solution for improving the Ruby language in a way to better support this pretty common use case. If you read the article below, you'll see the suggested code by its author, Avdi Grimm: http://devblog.avdi.org/2012/03/23/unless-readable-else-confused/ return follow(response['location']) if response.redirect? I also prefer this style, but I don't like this other common approach to the same problem: to include a meaningless return in the front of the special case statement. What if you really want to return something else? So, I'd like Ruby to support "then" besides "and" and "or" so that they will be used for what they were intended for: save_file and return :saved # you only want to return :saved if save_file returned true # special case, it wasn't possible to save the file notify_error_by_email or raise EmailNotWorking handle_error But for the example in the mentioned article, I'd prefer to write code like this: follow response['location'] if response.redirect? then return :redirected or follow response['location'] then return :redirected if response.redirect? I'm not sure about what precedence "then" should have. It means: no matter the return value, I want this to be run after that method. Another approach, since this would be used for earlier exit, would be to have something like then_return instead (a bit more constraint, but still useful): render :redirected then_return if should_be_redirected? Any of those approaches would enable better readable code than the current alternatives. -- http://bugs.ruby-lang.org/