Accessing the value of a hash from nested YAML

29 views
Skip to first unread message

Dave Everitt

unread,
May 27, 2020, 11:00:35 AM5/27/20
to nanoc
Really stupid question, but my usual rusty Ruby is being wheeled out again, and I can't work this out.

My YAML (example):

mystuff:
- stuff1: 'one'
- stuff2: 'two'
- stuff3: 'three'

I want to keep this as pure YAML.

The Ruby (ruby 2.6.5):

d = @item[:mystuff] # [{:stuff1=>"one"}, {stuff2:"two"…

What ERB would give me the string 'one', 'two', etc.?

I've tried way too many hacks - the last few being:

d.fetch(0) # {:stuff1=>"one"}
d
[0] # same as above
d
.values_at(0) # [{:stuff1=>"one"}]
d
.values_at(:stuff1) # TypeError: no implicit conversion of Symbol into Integer
d
[:stuff1] # same as above

in this case I just want the value of :stuff1 ("one") so it's time to be humble and just ask.

Denis Defreyne

unread,
May 27, 2020, 12:41:50 PM5/27/20
to na...@googlegroups.com
Hey Dave,

In the YAML example that you gave, the value for “mystuff” is an array
of hashes, not a hash itself. Compare:

mystuff:
- stuff1: 'one'
- stuff2: 'two'
- stuff3: 'three'

mystuff:
stuff1: 'one'
stuff2: 'two'
stuff3: 'three'

In the latter, the leading -s are removed, as each one creates a new
array element. Does this solve your issue?

Denis
> --
> You received this message because you are subscribed to the Google Groups "nanoc" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nanoc+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/nanoc/2a9f95fe-b9d9-4ced-b3ed-3a307de5b3c1%40googlegroups.com.



--
Denis Defreyne
+49 1573 1969 173
de...@denis.ws

Dave Everitt

unread,
May 27, 2020, 1:23:18 PM5/27/20
to nanoc
Thanks Denis.

YAML re-done as suggested

mystuff:
  stuff1: 'one'
  stuff2: 'two'
  stuff3: 'three'

but getting:

d = @item[:mystuff] # {:stuff1=>"one", :stuff2=>"two", :stuff3=>"three"}
d
[0]['stuff1] # NoMethodError: undefined method `[]' for nil:NilClass


Dave Everitt

unread,
May 27, 2020, 1:29:53 PM5/27/20
to nanoc
that's a mis-type - I have:
d[0]['stuff1']

Denis Defreyne

unread,
May 27, 2020, 1:51:00 PM5/27/20
to na...@googlegroups.com
This should work:

d = @item[:mystuff]
d[:stuff1] # => returns "one"

etc.

On Wed, May 27, 2020 at 7:29 PM Dave Everitt <dever...@gmail.com> wrote:
>
> that's a mis-type - I have:
> d[0]['stuff1']
>
> --
> You received this message because you are subscribed to the Google Groups "nanoc" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nanoc+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/nanoc/0a572952-c660-49f8-94d6-ad9c896e88bd%40googlegroups.com.

Dave Everitt

unread,
May 27, 2020, 2:07:33 PM5/27/20
to nanoc
Hm. Still getting:

NoMethodError: undefined method `[]' for nil:NilClass

from the compilation

Dave Everitt

unread,
May 27, 2020, 2:10:40 PM5/27/20
to nanoc
===== STACK TRACE:

  0. layout /mylayout/:23:in `get_binding' # line 23 has <%= d[:stuff] %>
  1. /usr/local/Cellar/ruby/2.6.5/lib/ruby/2.6.0/erb.rb:901:in `eval'
  2. /usr/local/Cellar/ruby/2.6.5/lib/ruby/2.6.0/erb.rb:901:in `result'
  3. /usr/local/lib/ruby/gems/2.6.0/gems/nanoc-4.11.16/lib/nanoc/filters/erb.rb:32:in `run'
  4. /usr/local/lib/ruby/gems/2.6.0/gems/nanoc-core-4.11.16/lib/nanoc/core/filter.rb:188:in `setup_and_run'
  5. /usr/local/lib/ruby/gems/2.6.0/gems/nanoc-core-4.11.16/lib/nanoc/core/executor.rb:67:in `layout'
  6. /usr/local/lib/ruby/gems/2.6.0/gems/nanoc-core-4.11.16/lib/nanoc/core/compilation_phases/recalculate.rb:34:in `block in run'
  7. /usr/local/lib/ruby/gems/2.6.0/gems/nanoc-core-4.11.16/lib/nanoc/core/action_sequence.rb:49:in `block in each'
  8. /usr/local/lib/ruby/gems/2.6.0/gems/nanoc-core-4.11.16/lib/nanoc/core/action_sequence.rb:49:in `each'
  9. /usr/local/lib/ruby/gems/2.6.0/gems/nanoc-core-4.11.16/lib/nanoc/core/action_sequence.rb:49:in `each'
  10. /usr/local/lib/ruby/gems/2.6.0/gems/nanoc-core-4.11.16/lib/nanoc/core/compilation_phases/recalculate.rb:29:in `run'
  11. /usr/local/lib/ruby/gems/2.6.0/gems/nanoc-core-4.11.16/lib/nanoc/core/compilation_phases/abstract.rb:23:in `call'
  12. /usr/local/lib/ruby/gems/2.6.0/gems/nanoc-core-4.11.16/lib/nanoc/core/compilation_phases/abstract.rb:25:in `block in call'
  13. /usr/local/lib/ruby/gems/2.6.0/gems/nanoc-core-4.11.16/lib/nanoc/core/compilation_phases/cache.rb:25:in `run'
  14. /usr/local/lib/ruby/gems/2.6.0/gems/nanoc-core-4.11.16/lib/nanoc/core/compilation_phases/abstract.rb:23:in `call'
  15. /usr/local/lib/ruby/gems/2.6.0/gems/nanoc-core-4.11.16/lib/nanoc/core/compilation_phases/abstract.rb:25:in `block in call'
  16. /usr/local/lib/ruby/gems/2.6.0/gems/nanoc-core-4.11.16/lib/nanoc/core/compilation_phases/resume.rb:14:in `block in run'

===== VERSION INFORMATION:

Nanoc 4.11.16 © 2007–2019 Denis Defreyne.
Running ruby 2.6.5 (2019-10-01) on x86_64-darwin18 with RubyGems 3.0.6.

Denis Defreyne

unread,
May 29, 2020, 2:07:56 PM5/29/20
to na...@googlegroups.com
Hey Dave,

The error “NoMethodError: undefined method `[]' for nil:NilClass”
indicates that `d` in your case is nil.

Are there items without `mystuff` attributes? That would cause such an error.

Denis
> --
> You received this message because you are subscribed to the Google Groups "nanoc" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nanoc+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/nanoc/b32653dc-1956-4bfe-9ccc-94510c5be233%40googlegroups.com.

Dave Everitt

unread,
Jun 1, 2020, 1:13:45 PM6/1/20
to nanoc
Hi Denis


The error “NoMethodError: undefined method `[]' for nil:NilClass”
indicates that `d` in your case is nil.

Are there items without `mystuff` attributes? That would cause such an error.

There are other items in the YAML outside the `mystuff:` block, but all inside is as suggested;

mystuff:
  stuff1: 'one'
  stuff2: 'two'
  stuff3: 'three'

Printing 'd' to the page on compilation, after allocating the `@item` to it shows as expected:

= @item[:mystuff] # {:stuff1=>"one", :stuff2=>"two", :stuff3=>"three"}

I just can't get any individual item, although in pure Ruby it works as expected.

So for now I've just removed the `:stuff1`, `:stuff2`… etc. just so I can process the YAML, but will come back here if trying to restore the labels in the next session on the website is still failing.

Thanks for your patience!

Reply all
Reply to author
Forward
0 new messages