Skip to content

Instantly share code, notes, and snippets.

@chrisseaton
Created July 11, 2015 15:23
Show Gist options
  • Select an option

  • Save chrisseaton/4464807d93b972813e49 to your computer and use it in GitHub Desktop.

Select an option

Save chrisseaton/4464807d93b972813e49 to your computer and use it in GitHub Desktop.
$ jt run --graal test/truffle/can-we-fold-yet.rb test/truffle/can-we-fold-yet.rb
Can Truffle constant fold yet?
> 14
Yes! Truffle can constant fold this to 14
> 14 + 2
Yes! Truffle can constant fold this to 16
> eval([1, 2, 3].inspect).sort[1] * 2
Yes! Truffle can constant fold this to 4
> eval(rand < 0.5 ? '14 - 2' : '10 + 2')
Yes! Truffle can constant fold this to 12
> eval('rand')
No :( Truffle can't constant fold that
@mikehearn

Copy link
Copy Markdown

'foo' + 'bar'
No :( Truffle can't constant fold that

Huh? I am not a Rubyist (anymore) but is there some bizarre gotcha in this example? How comes you can constant fold 14.send((')'.ord + 1).chr, 2) but not two strings being concatenated?

@chrisseaton

Copy link
Copy Markdown
Author

It's because strings are mutable. A dynamic value 14 is the same as a constant value 14, but 'foo' + 'bar' generates a new value each time, so it's not clear how to make that constant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment