Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Foundations Blocks Examples

Hoormazd Kia
PLUS
Hoormazd Kia
Courses Plus 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
Michael Hulet
47,912 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