2011-08-05 27 views
9

Trong vỏ, tôi có thể làmCó cách nào để đạt được grep như chức năng về sản lượng bên trong Rails Bảng điều khiển

$ cat name_of_file_with_a_lot_of_text | grep "What I am looking for" 

Bên Console Rails, tôi có thể đạt được một cái gì đó tương tự, nói rằng khi tôi chạy một lệnh và đầu ra là rất lớn, đặc biệt là nói một truy vấn DB.

Tôi nhận thức được rằng nó là YAML nhưng đó không phải là điều tôi đang tìm kiếm.

Cảm ơn.

Trả lời

19

Có, bạn có thể. Phương pháp này được gọi là gr ... chờ cho nó ... ep. Ruby grep hoạt động trên String, Array và nhiều đối tượng tích hợp khác. Ví dụ để có được tất cả to_xxx phương pháp của một số, chỉ cần làm:

1.methods.grep(/to_/) 
+1

Không hoạt động trên đầu ra từ các truy vấn DB. – subiet

+1

Tôi vừa cười vừa cười ... "ừm .. đợi đã .." "Giống như một sự hài hước tốt;) – luigi7up

2

Tôi có cùng một câu hỏi và không phải là rất hài lòng với phản ứng có phần quái gở từ ream88, vì vậy tôi quyết định tham gia một vết nứt ở đó.

# Allows you to filter output to the console using grep 
# Ex: 
# def foo 
#  puts "Some debugging output here" 
#  puts "The value of x is y" 
#  puts "The value of foo is bar" 
# end 
# 
# grep_stdout(/value/) { foo } 
# # => The value of x is y 
# # => The value of foo is bar 
# # => nil 
def grep_stdout(expression) 
    # First we need to create a ruby "pipe" which is two sets of IO subclasses 
    # the first is read only (which represents a fake $stdin) and the second is 
    # write only (which represents a fake $stdout). 
    placeholder_in, placeholder_out = IO.pipe 

    # This child process handles the grep'ing. Its done in a child process so that 
    # it can operate in parallel with the main process. 
    pid = fork { 
    # sync $stdout so we can report any matches asap 
    $stdout.sync 

    # replace $stdout with placeholder_out 
    $stdin.reopen(placeholder_in) 

    # we have to close both placeholder_out and placeholder_in because all instances 
    # of an IO stream must be closed in order for it to ever reach EOF. There's two 
    # in this method; one in the child process and one in the main process. 
    placeholder_in.close 
    placeholder_out.close 

    # loop continuously until we reach EOF (which happens when all 
    # instances of placeholder_out have closed) 
    read_buffer = '' 
    loop do 
     begin 
     read_buffer << $stdin.readpartial(4096) 
     if line_match = read_buffer.match(/(.*\n)(.*)/) 
      print line_match[1].grep(expression) # grep complete lines 
      read_buffer = line_match[2]   # save remaining partial line for the next iteration 
     end 
     rescue EOFError 
     print read_buffer.grep(expression) # grep any remaining partial line at EOF 
     break 
     end 
    end 
    } 

    # Save the original stdout out to a variable so we can use it again after this 
    # method is done 
    original_stdout = $stdout 

    # Redirect stdout to our pipe 
    $stdout = placeholder_out 

    # sync $stdout so that we can start operating on it as soon as possible 
    $stdout.sync 

    # allow the block to execute and save its return value 
    return_value = yield 

    # Set stdout back to the original so output will flow again 
    $stdout = original_stdout 

    # close the main instances of placeholder_in and placeholder_out 
    placeholder_in.close 
    placeholder_out.close 

    # Wait for the child processes to finish 
    Process.wait pid 

    # Because the connection to the database has a tendency to go away when calling this, reconnect here 
    # if we're using ActiveRecord 
    if defined?(ActiveRecord) 
    suppress_stdout { ActiveRecord::Base.verify_active_connections! } 
    end 

    # return the value of the block 
    return_value 
end 

Hạn chế rõ ràng của giải pháp của tôi là đầu ra bị mất. Tôi không chắc chắn làm thế nào để có được xung quanh mà không cần gọi yield hai lần.

EDIT Tôi đã thay đổi câu trả lời của mình thành chỉ gọi fork một lần, cho phép tôi giữ đầu ra của khối và trả lại ở cuối. Thắng lợi.

EDIT 2 Bạn có thể nhận được tất cả các chức năng này (và nhiều hơn nữa!) Trong đá quý này ngay bây giờ https://github.com/FutureAdvisor/console_util

Các vấn đề liên quan