Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: check if object is nil

Tags:

ruby

  def parse( line )
    _, remote_addr, status, request, size, referrer, http_user_agent, http_x_forwarded_for = /^([^\s]+) - (\d+) \"(.+)\" (\d+) \"(.*)\" \"([^\"]*)\" \"(.*)\"/.match(line).to_a

    print line
    print request
    if request && request != nil
      _, referrer_host, referrer_url = /^http[s]?:\/\/([^\/]+)(\/.*)/.match(referrer).to_a if referrer
      method, full_url, _ = request.split(' ')

in parse: private method 'split' called for nil:NilClass (NoMethodError)

So as i understand it's calling split not on a string, but on nil. This part is parsing web server log. But I can't understand why it's getting nil. As I understand it's null.

Some of the subpatterns in regex failed? So it's the webserver's fault, which sometimes generates wrong logging strings?

By the way how do I write to file in ruby? I can't read properly in this cmd window under windows.

like image 528
Somebody Avatar asked May 26 '11 18:05

Somebody


1 Answers

You seem to have a few questions here, so I'll take a stab at what seems to be the main one:

If you want to see if something is nil, just use .nil? - so in your example, you can just say request.nil?, which returns true if it is nil and false otherwise.

like image 61
Chris Bunch Avatar answered Sep 17 '22 14:09

Chris Bunch