0

I have the following function

def fcn(lst)
  if (lst.size == 0) 
      return [] of Int32
  end
  lc = lst.map{|a| a[1]}
end

When doing:

lst = [] of Int32
puts(fcn(lst))

I get:

Error in line 8: instantiating 'fcn(Array(Int32))'
in line 5: undefined method '[]' for Int32

It seems "if" is not taken into account. How can I resolve this error?

1
  • 2
    Could you please see the documentations about Crystal Syntax ?
    – Furtiro
    Sep 5, 2016 at 12:41

1 Answer 1

1

In lst.map{|a| a[1]}, lst is Array(Int32), so a is Int32, and Int32 doesn't have a [] method. In Ruby Fixnum does have the [] method, to return 1 if the bit in that position is set. I don't know if that's what you want to do. If so, use the bit method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.