Richard Brason
Richard Brason

Reputation: 61

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?

Upvotes: 3

Views: 250

Answers (2)

virullius
virullius

Reputation: 979

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.

Upvotes: 4

Richard Brason
Richard Brason

Reputation: 61

The crystal playground cannot execute this code for some reason.

Don't use the crystal playground. It is currently close to useless.

Put the code in a file and do:

crystal each.cr

And you will see the array elements.

Upvotes: -3

Related Questions