1
請讓我知道這些操作之間有什麼區別。第一個代碼工作正常:For和Ruby之間的區別
1) for elem in(mr)
elem.shedule = Date.new(date.year, date.month, date.day)
end
,但我想用map
:
2) mr.map!{ |elem| elem.shedule = Date.new(date.year, date.month, date.day) }
和第二代碼返回錯誤:
NoMethodError in Events#index
Showing C:/Sites/calend/app/views/events/_calendar.html.erb where line #9 raised:
undefined method `shedule' for Thu, 04 Apr 2013:Date
Extracted source (around line #9):
6: </h2>
7: <%= calendar_for(@repeats, :year => @date.year, :month => @date.month) do |calendar| %>
8: <%= calendar.head('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday') %>
9: <%= calendar.day(:day_method => :shedule) do |date, events| %>
10: <%= date.day %> <%= link_to '+', new_event_path(date: date) %>
11: <ul>
12: <% for event in events %>
'for'和'each'的區別在於,'each'接收一個塊(因此是一個新的範圍),而'for'保留在周圍的一個。這意味着,如果在塊中引入新的局部變量,在「結束」之後它將不可見。用'for',情況並非如此。 – 2013-04-05 11:30:05
感謝您的指出。 – toro2k 2013-04-05 11:39:15
感謝您的解釋! – Gabi 2013-04-05 12:22:59