The Crystal Programming Language Forum

How to iterate through an array using "each" in Crystal?

I’m using Crystal 1.3.0.

The array is array = [1, 2, 3]

The code below works with Ruby:

array.each do |x|
  puts x

array.each { |x| puts x }

But fails miserably with Crystal.


It works just fine: Carcin

What error are you seeing?

It does not work when I use crystal play on my machine. When I go to and paste this code, I get:

I looks like I have to click in “SHOW OUTPUT” to see it. Sorry about that, but I thought the output was being shown by default. It never occurred to me that I had to click on that button :frowning:

Screen Shot 2022-01-12 at 12.47.02 PM

What you’re seeing on the right there is the type of each line, not what it outputs. Personally i find it easier to just define a file and edit/run that repeatedly. But the playground can be helpful for getting a feel for the types of everything.