Search code examples
crystal-lang

Why does array "each" do not work anymore in Crystal 1.3.0? What should I use instead?


The code below does not print the elements of the array. Why is that?

array = [1, 2, 3]

array.each do |x|
  puts x
end

array.each { |x| puts x }

Crystal Play Output

It used to work in previous versions. I know Crystal is not Ruby, but the code above works in Ruby. What is the best practice to iterate through an array in Crystal? And why did the Crystal developers decide to make Crystal incompatible with Ruby in such a basic scenario?


Solution

  • Short answer: click "SHOW OUTPUT" below right-hand side box.

    There is an apparent difference between the local version of Crystal Playground you get from running crystal play and the one running at play.crystal-lang.org. The code works, but the presentation of the results is different.