Marcus Marcus - 2 months ago 7
Ruby Question

Ruby ending if block

I am a total beginner to Ruby. I am trying to print if an object is an array or an integer, but I am getting a syntax error I cannot figure how to solve it.

D:\Ruby>ruby -c Learning-Ruby\loops_stuff.rb
Learning-Ruby/loops_stuff.rb:9: syntax error, unexpected keyword_else, expecting keyword_end
Learning-Ruby/loops_stuff.rb:11: syntax error, unexpected end-of-input, expecting keyword_end


This is my code

obj = ["a", 1, 3.6]

if object.is_a(obj)
puts "Is array: "
obj.each do |index|
puts index
elseif object.is_i(obj)
puts "Is integer: {#obj}"
else
puts "Is neither array or integer"
end

Answer

the keyword is elsif (without the e in the middle)

obj = ["a", 1, 3.6]

if obj.is_a?(Array)
  puts "Is array: "
  obj.each do |index|
    puts index
  end
elsif obj.is_a?(Integer)
  puts "Is integer: #{obj}"
else
  puts "Is neither array or integer"
end

also stumbled over this in my first ruby sessions