Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect handling of begin/end while #575

Closed
wmnnd opened this issue Aug 27, 2014 · 9 comments · Fixed by #2255
Closed

Incorrect handling of begin/end while #575

wmnnd opened this issue Aug 27, 2014 · 9 comments · Fixed by #2255
Labels
compiler low-priority v1.1 Still an issue on v1.1

Comments

@wmnnd
Copy link
Contributor

wmnnd commented Aug 27, 2014

Opal seems to evaluate the condition in a begin/end while loop before executing the scope while MRI runs it once before checking the condition.

The following code will print "konichiwa" in MRI but not in Opal.:

begin
  puts "konichiwa"
end while false

#Prints "konichiwa" in MRI, but not in Opal

Regular while loops work fine and have their condition checked both in MRI and Opal:

while false
    puts "hello"
end

#Will never print anything
@Ajedi32
Copy link

Ajedi32 commented Aug 27, 2014

You know, now that I think about it that behavior from MRI is kind of strange:

puts "hi" while false # Prints nothing

begin
  puts "konichiwa"
end while false
# Prints "konichiwa"

puts "hi" if false # Prints nothing

begin
  puts "konichiwa"
end if false
# Prints nothing

😜

@vendethiel
Copy link
Contributor

The first one is a do-while (special form). The second one is a postfix if

@Ajedi32
Copy link

Ajedi32 commented Aug 27, 2014

@Nami-Doc Yes, I realize it's there to allow do-while. I just think it's funny that postfixing while results in the condition being checked after block execution, whereas postfixing if does not. It feels inconsistent, even though I know it's like that for a good reason.

@sergei-kucher
Copy link

@Ajedi32 My opinion is nothing strange here.

if is keyword for execute expression or not, but while is keyword for loop expression or not. So then we use if false, expression doesn't execute. And in case of while false, expression executes but loop doesn't performs.

So that behavior is very logical.

@Ajedi32
Copy link

Ajedi32 commented Aug 29, 2014

@sergey-kucher Except that Ruby's behavior is inconsistent with that interpretation in this case:

puts "hi" while false # Prints nothing

😝

Obviously I'm not saying Opal should implement this differently, I'm just saying the existing behavior feels kind of strange (even though I understand why it was done that way).

@elia
Copy link
Member

elia commented Aug 29, 2014

I think in the we'll just look at rubyspec. I know for sure that some stuff has been "marked" as bug by Matz himself.

Anyway I don't think I'll ever use this "feature" :P

I also consider this rather low-priority :)

@Ajedi32
Copy link

Ajedi32 commented Aug 29, 2014

Yeah, for more on the usage of this feature: http://stackoverflow.com/questions/136793/is-there-a-do-while-loop-in-ruby

Also, apparently Matz agrees with me: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/6745

@sergei-kucher
Copy link

@Ajedi32 puts "hi" while false # Prints nothing - that's really strange ))

@elia
Copy link
Member

elia commented May 17, 2016

Will keep this open and will find out who will come first, if the MRI that will remove this feature eventually or us implementing it 😄


Here's the response from matz, just for reference
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/6741

Subject: Re: semenatics of if/unless/while statement modifiers
From: Yukihiro Matsumoto <matz ruby-lang.org>
Date: Wed, 23 Nov 2005 10:54:34 +0900
In-reply-to: 6719
Hi,

In message "Re: semenatics of if/unless/while statement modifiers"
    on Wed, 23 Nov 2005 01:11:39 +0900, ts <decoux / moulon.inra.fr> writes:

| What is a post-test loop ? This
|
|moulon% ruby -e 'i = 0; begin puts i; end while i > 0'
|0

Don't use it please.  I'm regretting this feature, and I'd like to
remove it in the future if it's possible.

                            matz.

@elia elia added the compiler label May 17, 2016
rubys added a commit to ruby2js/ruby2js that referenced this issue Feb 27, 2021
On the plus side, this may improve performance, unfortunately it
is not in a performance critical code path.
@hmdne hmdne added the v1.1 Still an issue on v1.1 label Jul 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler low-priority v1.1 Still an issue on v1.1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants