ruby / TryRuby

This 4th iteration of TryRuby is a website where you can learn the Ruby language.
https://try.ruby-lang.org
MIT License
226 stars 97 forks source link

Different behavior between TryRuby and MRI #78

Closed taichi-ishitani closed 1 year ago

taichi-ishitani commented 4 years ago

Result of following code are different between TryRuby and MRI.

class Foo
  def call_1(*obj)
    obj.each { |o| o.foo }
  end

  def call_2(*obj)
    obj.each(&:foo)
  end

  protected

  def foo
    p 'foo !'
  end
end

obj = [Foo.new, Foo.new]
obj[0].call_1(*obj)
obj[0].call_2(*obj)

Result of TryRuby

2020-03-29 11 17 59 try ruby-lang org bf0133002b73

Result of MRI

ishitani@DESKTOP-9UGD5SR MINGW64 /c/Users/ishitani/workspace/temp
$ ruby -v
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x64-mingw32]
ishitani@DESKTOP-9UGD5SR MINGW64 /c/Users/ishitani/workspace/temp
$ ruby protected.rb
"foo !"
"foo !"
protected.rb:7:in `each': protected method `foo' called for #<Foo:0x00000000062e44c8> (NoMethodError)
Did you mean?  for
        from protected.rb:7:in `call_2'
        from protected.rb:19:in `<main>'
hmdne commented 3 years ago

Opal (https://opalrb.com) doesn't support private and protected modifiers and this support unfortunately isn't planned anytime soon.

taichi-ishitani commented 3 years ago

Hi @hmdne , Thank you for your comment and I understood.

sambostock commented 2 years ago

Rather than open another issue, I'm going to add this here.

I was trying to answer a friend's questions about Ruby from my phone and resorted to TryRuby, but it turns out some things behave fundamentally differently:

ScenarioIRB/MRITryRuby
[String and Symbol equality](https://github.com/ruby/spec/blob/f30dad390c47a436465abc2ff62c4a863bdb3299/core/symbol/equal_value_spec.rb#L12) ```ruby :a == "a" # => false ["a"].include?(:a) # => false ``` ```ruby :a == "a" # => true ["a"].include?(:a) # => true ```
String and Symbol Object IDs ```ruby :a.object_id # => 759068 "a".object_id # => 260 ``` ```ruby :a.object_id # => "a" "a".object_id # => "a" ```

These are a fundamental difference in how symbols work and very misleading for anyone trying to get an understanding of why symbols exist and how they differ from strings.

Perhaps it is worth noting for the user that there may be subtle differences between what they see here and in other implementations?


Edit: I see this appears to be because Opal doesn't actually distinguish between Symbol and String at all?

Symbol == String # => true

That explains it, but still feels weird to see when trying to show off Ruby, since Symbols are so widely used, and new to some people.

hsbt commented 2 years ago

We provided the WASM implementation of CRuby now. You can try it and get the same results of MRI.

sambostock commented 2 years ago

Ahh, I see. It is only available in the playground though. Would it make sense to use this as default on the home page?

eregon commented 2 years ago

I think the first step is to clarify Opal is the currently default, and make it possible to use CRuby on the homepage: https://github.com/ruby/TryRuby/issues/122.

Then switching to CRuby by default can be chosen after that.

hmdne commented 1 year ago

Fixed by #148