Reputation: 11
Is there a built-in method for writing record numbers so that I do not have to create a variable and increase it? Just developing practices at this point. file.recnumber
?
<% i = 1%>
<% @files.each do |file| %>
<%= i %>. <%= file %> (<%= file.size %>)k<br />
<% i = i + 1%>
<% end %>
Upvotes: 1
Views: 336
Reputation: 80065
Actually, io objects and file objects do have a lineno method.
File.open("test.txt") do |file|
file.each{|line| puts "#{file.lineno}: #{line}"}
end
Upvotes: 1
Reputation: 237010
I think you're looking for each_with_index
, which passes the index as the second argument to the block.
If you want to do something more sophisticated than each
, recent versions of Ruby also include a with_index
method that you can chain onto any Enumerable method to get a version with the index. For example:
('a'..'z').map.with_index {|letter, index| "#{index} #{letter.upcase}" }
Upvotes: 4
Reputation: 6520
Another alternative is to just use an HTML ordered list:
<ol>
<% ["one", "two", "three"].each do |file| %>
<li>file <%= file %></li>
<% end %>
</ol>
Will give you something like the following, which you can easily style with CSS:
1. file one
2. file two
3. file three
Upvotes: 0