1

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 %>

4 Answers 4

4

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}" }
Sign up to request clarification or add additional context in comments.

Comments

3
<% files.each_with_index do |file, index| %>
    ...
<% end %>

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.