Unable to get sample YAML-data example to work "out of the box"

341 views
Skip to first unread message

Rick Karpinski

unread,
Apr 29, 2011, 1:24:34 PM4/29/11
to middlem...@googlegroups.com
When I try to incorporate the example YAML-data example from "Middleman 1.1" in a toy change to the supplied index.html.haml file:


- content_for :head do
  %title The Middleman!

%h1 The Middleman is watching.
%h2 This is a test!
= some_helper(1)
%br
= trial_helper(2)
%br
= lorem.sentence
%br
%br
%h1 Friends
%ul
  - x = data.people
  = "X: " + x.inspect
  %br
  = x["friends"].inspect
  -x["friends"].each do |f|
    %li= f
%br
%br
%h2 YAML:
%h3 Friends
%ul
  - data.people.friends.each do |f|
    %li= f


I get:

NoMethodError at /

undefined method `friends' for {"friends"=>["tom", "dick", "harry"]}:Hash



However, with the example ruby code dismantled a little, it (inelegantly) works:

- content_for :head do
  %title The Middleman!

%h1 The Middleman is watching.
%h2 This is a test!
= some_helper(1)
%br
= trial_helper(2)
%br
= lorem.sentence
%br
%br
%h1 Friends
%ul
  - x = data.people
  = "X: " + x.inspect
  %br
  = x["friends"].inspect
  -x["friends"].each do |f|
    %li= f


Produces:

The Middleman is watching.

This is a test!

Helper 1 
Trial Helper 
Sed aut consequatur iure autem 

Friends

    X: {"friends"=>["tom", "dick", "harry"]} 
    ["tom", "dick", "harry"]
  • tom
  • dick
  • harry
What am I missing/doing wrong?

Thanks,

--Rick

Thomas Reynolds

unread,
Apr 29, 2011, 1:29:16 PM4/29/11
to middlem...@googlegroups.com
This is a rubyism. The data that comes back from the Yaml file is a Hash. It's keys are strings.

so: data.people => { "friends" => [1,2,3] }

to access friends, you use a string key.

data.people["friends"] and not data.people.friends as your first example shows.

I might be useful to augment the built-in Hash so you could use methods (.friends) in addition to symbols (:friends) so there isn't really an option to mess up.

-Thomas

Richard Karpinski

unread,
Apr 29, 2011, 3:07:21 PM4/29/11
to middlem...@googlegroups.com
Thanks, Thomas. I think I had sort of figured that out; but it leads me to ask if the example on your blog is correct (that is the copy/paste source of my non-functioning code)?

--Rick
--
Richard H. S. Karpinski, MD, FACS
200 Central Park South
New York, NY  10019
(212) 977-9797

rh...@columbia.edu
www.karpinskimd.com

Thomas Reynolds

unread,
Apr 29, 2011, 3:33:50 PM4/29/11
to middlem...@googlegroups.com
Well, the good news is I just updated the data feature to be as permissive as possible.

data.people.friends should work just fine now (in addition to the formerly working data.people["friends"]).

gem install middleman

And see if that feels more natural

-Thomas

Richard Karpinski

unread,
Apr 30, 2011, 11:51:57 AM4/30/11
to middlem...@googlegroups.com
Yea! It works.

--RK
Reply all
Reply to author
Forward
0 new messages