Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to achieve grep like functionality on output inside the Rails Console

In shell, I can do

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

Inside the Rails Console, can I achieve something similar, say when I run a command and the output is huge, especially say a DB query.

I am aware of outputting it as YAML but that Is not what I am looking for.

Thanks.

like image 723
subiet Avatar asked Aug 05 '11 10:08

subiet


People also ask

What can you do in rails console?

The console command lets you interact with your Rails application from the command line. On the underside, bin/rails console uses IRB, so if you've ever used it, you'll be right at home. This is useful for testing out quick ideas with code and changing data server-side without touching the website.


2 Answers

Yes, you can. The method is called gr... wait for it ...ep. Ruby's grep works on String, Array and many other built-in objects. For example to get all to_xxx methods of a number, just do:

 1.methods.grep(/to_/)
like image 132
Mario Uher Avatar answered Oct 02 '22 19:10

Mario Uher


I had the same question and wasn't very satisfied with the somewhat snarky response from ream88, so I decided to take a crack at it.

# 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

The obvious drawback of my solution is that the output is lost. I'm not sure how to get around that without calling yield twice.

EDIT I've changed my answer to only call fork once, which allows me to keep the output of the block and return it at the end. Win.

EDIT 2 You can get all of this functionality (and more!) in this gem now https://github.com/FutureAdvisor/console_util

like image 21
jaredonline Avatar answered Oct 02 '22 18:10

jaredonline