I got a quite annoying problem tonight, and maybe someone here will be able to help me.
I'm building a static blog with nanoc, and currently making some helpers for next/previous articles (I included some tests I made and the return):
# lib/helpers.rb
include Nanoc3::Helpers::Blogging
include Nanoc3::Helpers::LinkTo
# Returns a link to the next article
# If the article in param is the most recent, returns nothing
def next_article article
articles = sorted_articles # returns an array of article ordered by date
pos = articles.index(article) # returns the expected integer
pos.class # returns Fixnum
pos.nil? # returns false
pos + 1 # NoMethodError: undefined method `+' for nil:NilClass
return if pos.zero? # NoMethodError: undefined method `zero?' for nil:NilClass
link_to(articles[pos+1][:title], articles[pos+1]) # Fails too, obviously
end
I have absolutely no idea of why I can't use the "pos" variable, but can still perform some read on it. If anyone has an insight, I'll take it. Thanks in advance !
(I use ruby-1.9.3p194, with rvm, on OSX Lion, if it may have any relation)
Update : I should have precised that the returned value for pos is the expected value when just read. Strangely enough, setting
pos = articles.index(article).to_s.to_i
seems to work though. I just don't understand how & why it happens.
#layout/post_navigation.haml
.post_navigation
.next
=next_article @item