레일즈에서 해시 작업을 시도하고 있습니다. each
을 실행하면 예기치 않은 결과가 발생합니다. 각 요소의 '이름'에 액세스하려고합니다.해시에서 각각을 사용할 때의 오류
나는 some_files
라는 해시, 그것은 단지 한 요소와이 시간에 시작 :
logger.debug some_files
# Outputs this: {"0"=>{"name"=>"index.html", "contents"=>""}}
# Ok cool, the first level of this array seems to only have 1 element called "0".
가 지금은 (때로는 1 개 이상의 요소가 때문에) 그것을 통해 반복합니다.
some_files do |some_file|
logger.debug some_file
# Outputs this: ["0", {"name"=>"index.html", "contents"=>""}]
# Weird, why do I get "0" still? And why does it appear to be a separate element?
logger.debug some_file.name
# Outputs an error: NoMethodError (undefined method `name' for ["0", {"name"=>"index.html", "contents"=>""}]:Array):
end
downvote 경우 적어도 downvoting 이유에 대한 의견을주십시오. 나는 문서를보고 내 실수를 보지 못했다. http://ruby-doc.org/core-2.1.1/Array.html#method-i-each –
'some_file.last [ "name"]'이 아니어야합니까? – PericlesTheo
어떻게'each_'을 사용하지 않고'some_files'를 반복 할 수 있습니까? 나는'some_files do | some_file | ... ' – mdesantis