0

I am completely new to Ruby. I am trying to print the type and the name of a class using a method but I am getting this syntax error I cannot figure out:

Code/oop.rb:47: syntax error, unexpected end-of-input
    puts <<TYPE, <<EOF, <<NAME
               ^

Here is my code:

def whatClass()
    class_type = self.type
    class_name = self.name

    puts <<TYPE, <<EOF, <<NAME
        class_type
    TYPE
        \n
    EOF
        class_name
    NAME     
end
Marcus
  • 128
  • 2
  • 12

2 Answers2

3

There are multiple errors.

Preamble: type and nameare no standard methods. In the following example I replace it with strings.

When you use Here-Documents like this:

puts <<HEREDOC
  Heredoc
HEREDOC

Then the closing HEREDOC must start in column 0 and may have no trailing spaces. When you have leading spaces, then you must start with <<-HEREDOC:

puts <<-HEREDOC
  Heredoc
  HEREDOC

So your example is:

  def whatClass()
    class_type = 'type' #self.type undefined method `type'
    class_name = 'name' #self.name

    puts <<-TYPE, <<-EOF, <<-NAME
        class_type
    TYPE
        \n
    EOF
        class_name
    NAME
  end

And again: There may be no trailing spaces in TYPE, EOF and NAME (when I take your example with cut+paste there are trailing spaces at NAME).

The next error:

The output is

        class_type


        class_name

But I think you want the content of the two variables. So I think you need:

  def whatClass()
    class_type = 'type' #self.type undefined method `type'
    class_name = 'name' #self.name

    puts <<-TYPE, <<-EOF, <<-NAME
    #{class_type}
    TYPE
        \n
    EOF
    #{class_name}
    NAME
  end
knut
  • 27,320
  • 6
  • 84
  • 112
0

try this:

def whatClass()
    class_type = self.type
    class_name = self.name
    puts "#{class_type} \n #{class_name}"

end
Bartłomiej Gładys
  • 4,525
  • 1
  • 14
  • 24