user6589814 user6589814 - 1 year ago 73
Ruby Question

Ruby on Rails - Add limit, order and sum on

In my application I have a

file that I
on users profile view. That
file contains some numbers.

This is what i have so far:

f ="public/users/14/user.txt", "r")
- f.each_line do |d|
%p= d

I can
out all the numbers that is on that file. Is it any way to add
order (start from bottom to top)

I tried:

f.limit(4).order('DESC').each_line do |d|

and"public/users/14/user.txt", "r").limit(5)

When I try these I get error:
undefined method limit
undefined method order

Is it also possible to
all the numbers on that file?

Answer Source

This should work:

@lines = IO.readlines('public/users/14/user.txt').sort.reverse.first(4)

You should put the above in your controller and use it in your view:

- @lines.each do |line|
  %p= line
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download