Ruby Ruby Foundations Blocks Examples

Hoormazd Kia
PRO
Hoormazd Kia
Pro Student 6,075 Points

syntax error, unexpected end-of-input, expecting keyword_end

In the Ruby foundation example video one of the examples involves building a speech class , but everytime I run the program i get the following error - "syntax error, unexpected end-of-input, expecting keyword_end"

here's my speech-example.rb code.

class Speech
    def initialize
        print "What is the speech name? "
        @title = gets.chomp
        @lines = []
        while add_line
            puts "Line added."
        end
    end

    def title
        @title
    end

    def add_line
        puts "Add a line: (blank line to exit)"
        line = gets.chomp
        if line.length > 0
            @lines.push line
            return line
        else
            return nil
        end


    def each(&block)
        @lines.each { |line| yield line}
    end
end

speech = Speech.new
speec.each do |line|
    puts "[#{speech.title}] @{line}"
end

Oh, I'm also running the following version of Ruby - ruby 2.1.2p95

Thanks guys

2 Answers

Michael Hulet
MOD
Michael Hulet
iOS Development with Swift Treehouse Moderator 43,640 Points

You're missing and end to close out your add_line method. This code works:

class Speech
    def initialize
        print "What is the speech name? "
        @title = gets.chomp
        @lines = []
        while add_line
            puts "Line added."
        end
    end

    def title
        @title
    end

    def add_line
        puts "Add a line: (blank line to exit)"
        line = gets.chomp
        if line.length > 0
            @lines.push line
            return line
        else
            return nil
        end
    #You were missing the end statement right here
    end


    def each(&block)
        @lines.each { |line| yield line}
    end
end

speech = Speech.new
speec.each do |line|
    puts "[#{speech.title}] @{line}"
end