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

Lino Espinoza
Lino Espinoza
2,639 Points

Ruby Badges - ERB Problem

Sup guys, i just trying to replicated the code that Jason writes on the Ruby classes but i got this error on the ERB example.

$ ruby erb.rb
/Users/medialab/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/erb.rb:838:in `eval': (erb):3: unknown type of %string (SyntaxError)
; _erbout.concat(( %treehouse[:location] ).to_s); _...
                      ^
(erb):3: syntax error, unexpected $end
; _erbout.concat(( %treehouse[:location] ).to_s); _...
                      ^
    from /Users/medialab/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/erb.rb:838:in `result'
    from erb.rb:22:in `<main>'

Thanks in advance.

[ed. note] Added markdown to fix code formatting

3 Answers

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Lino,

Can you paste your code here? That will make it easier to debug.

Lino Espinoza
Lino Espinoza
2,639 Points

Hello Jason, this is the code:

require 'erb'

treehouse = { 
    name: 'Treehouse',
    location: 'Treehouse Island' 
}

template = <<-TEMPLATE
From the desk of <%= treehouse[:name] %>
----------------------------------------
Welcome to <%= %treehouse[:location] %>,


We hope you enjou you stay.
----------------------------------------
<% treehouse.keys.each do |key| %>
    Key: <%= key %>
<% end %>
TEMPLATE

erb = ERB.new(template)
puts erb.result

And this is the error:

erb.rb:4: odd number list for Hash
    name: 'Treehouse',
         ^
erb.rb:4: syntax error, unexpected ':', expecting '}'
    name: 'Treehouse',
         ^
erb.rb:4: syntax error, unexpected ',', expecting $end

[ed. note] Added markdown to fix code formatting

I have the same code with different error however I checked the ruby 1.9.3 doc, and it turned out you're required to build Hash with capitalized letter the items...

like code blow

require 'erb'

Treehouse = { name: 'Treehouse', location: 'Treehouse Island' }

template = <<-TEMPLATE

From the desk of <%= Treehouse[:name] %>

Welcome to <%= Treehouse[:location] %>

We hope you enjoy your stay

<% Treehouse.keys.each do |key| %> Key: <%= key %>

<% end %>

TEMPLATE erb = ERB.new(template) puts erb.result

don't use

treehouse = { name: 'Treehouse', location: 'Treehouse Island' }

as it will give you some unexpected errors...(during search i saw 3 of them with different error message....)

"to build Hash with capitalized letter the items..." should be "to build Hash with capitalized letter name..."

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hi Lino,

What version of Ruby are you using? Can you use a different service to paste your file, such as http://gist.github.com? The formatting got weird.