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

Destructuring assignment #2841

Open
jbreeden opened this issue Jun 20, 2015 · 3 comments
Open

Destructuring assignment #2841

jbreeden opened this issue Jun 20, 2015 · 3 comments

Comments

@jbreeden
Copy link
Contributor

Noticing some strange behavior when mruby is performing destructuring assingment inside of an interpolated string, and the assigned value comes from a local variable. Not sure if this belongs in the "not implemented" list or not. If so, let me know and I'll happy to close this and make a comment there instead.

Showing the issue below, copied from my terminal (comments added after). I'm running on Windows, for what it's worth.

C:\projects\mruby-bindings\mrbgems\mruby-nanomsg>..\..\mruby-1.1.0\bin\mirb.exe --version
mruby 1.1.0 (2014-11-19)

C:\projects\mruby-bindings\mrbgems\mruby-nanomsg>..\..\mruby-1.1.0\bin\mirb.exe
mirb - Embeddable Interactive Ruby Shell

# Assigning from array literal, everything looks good
> puts "#{@one, @two = [1, 2]}"  
[1, 2]
 => nil

# Making a variable to hold the array
> a = [1, 2]  
 => [1, 2]

# Perform the same `puts` as before.
 # With the variable, there seems to be some confusion and the current `this` is returned.
> puts "#{@one, @two = a}" 
main 
 => [main]

# Now things are really wacky, `a` seems to have lost it's value
> @one, @two = a  
 => nil

# Yep, that `a` is nil
> a  
 => nil

# Fixing `a`
> a = [1, 2]  
 => [1, 2]

# OK, sure enough, the assignment works outside of the string once `a` is restored
> @one, @two = a  
 => [1, 2]

Just for reference, MRI seems to handle this fine. I know MRuby isn't MRI (and I love that about it :), but I just wanted to make sure I wasn't doing something syntactically weird...

C:\projects\mruby-bindings\mrbgems\mruby-nanomsg>irb

# With an array literal...
irb(main):001:0> puts "#{@one, @two = [1, 2]}"
[1, 2]
=> nil
irb(main):002:0> a = [1, 2]
=> [1, 2]

# with an array variable
irb(main):003:0> puts "#{@one, @two = a}"
[1, 2]
=> nil
@jbreeden
Copy link
Contributor Author

This may be a separate issue, but it seems closely related. Another test shows that being in an instance method is enough to throw off the destructuring.

Here we can see that the array_variable_test struggles with the deconstruction, returning only the last value in the array (instead of the entire array). This was also confirmed to work on MRI.

C:\projects\mruby-bindings\mrbgems\mruby-nanomsg>..\..\mruby-1.1.0\bin\mirb.exe
mirb - Embeddable Interactive Ruby Shell

> class Test
*   attr_accessor :a, :b
*
*   def array_lit_test
*     @a, @b = [1, 2]
*   end
*
*   def array_variable_test
*     ary = [1, 2]
*     @a, @b = ary
*   end
* end
 => :array_variable_test
> t = Test.new
 => #<Test:0xbcd970>
> t.array_lit_test
 => [1, 2]

# Why isn't this `[1, 2]` as in the previous test?
> t.array_variable_test
 => 2

The same test works outside of any methods on MRuby

C:\projects\mruby-bindings\mrbgems\mruby-nanomsg>..\..\mruby-1.1.0\bin\mirb.exe
mirb - Embeddable Interactive Ruby Shell

> ary = [1, 2]
 => [1, 2]
> one, two = ary
 => [1, 2]

@jbreeden jbreeden changed the title Destructuring assignment in Destructuring assignment Jun 20, 2015
@cremno
Copy link
Contributor

cremno commented Jun 21, 2015

Your first bug seems to be fixed on master. I can reproduce the second one. It's caused by the codegen's optimization (workaround: set mrbc_context::no_optimize)

@jbreeden
Copy link
Contributor Author

Thanks, @cremno. Next time I'll check master before posting an issue.

By the way, does anybody know when another release might get tagged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants