18
  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.

9
  • 1
    "As I understand it's null." There is no such thing as null in Ruby. Just nil. Commented May 26, 2011 at 18:59
  • Why what is getting nil? The function is not listed to the end, and you define several variables... Commented May 26, 2011 at 19:00
  • Mark i know. Amadan it's not my script mate. :) As i understand regex is throwing subpattern matches into listed variables. Like list function in php and when subpattern fails it just assign nil object to it right? Commented May 26, 2011 at 19:01
  • Actually, wait, it does not make any sense. Are you sure you pasted correctly? If request is nil as your error says, it could not have passed the if request guard. (BTW: if request && request != nil is redundant - if request != nil will always be true if request, which makes the latter sufficient). Commented May 26, 2011 at 19:10
  • Indeed. I don't know Ruby, so the statement should be different. Maybe !== instead of != should be used? I'll try that out. Commented May 26, 2011 at 19:13

3 Answers 3

41

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'll catch request that fails and see why pattern is failing. Questiong is still opened. Why it passes statement? :)
12

Ruby 2.3.0 added a safe navigation operator (&.) that checks for nil before calling a method.

request&.split(' ')

This is functionally* equivalent to

!request.nil? && request.split(' ')

*(They are slightly different. When request is nil, the top expression evaluates to nil, while the bottom expression evaluates to false.)

Comments

3

To write to a file:

File.open("file.txt", "w") do |file|
  file.puts "whatever"
end

As I write in a comment above - you didn't say what is nil. Also, check whether referrer contains what you think it contains. EDIT I see it's request that is nil. Obviously, regexp trouble.

Use rubular.com to easily test your regexp. Copy a line from your input file into "Your test string", and your regexp into "Your regular expression", and tweak until you get a highlight in "Match result".

Also, what are "wrong logging strings"? If we're talking Apache, log format is configurable.

14 Comments

Mate... :) Why it's passing through this? if request && request != nil
Yeah, I know, I just commented on that. That should be impossible.
How to check in Ruby that some variable, in our case it's request is not an instance of nil?
if !request.nil? - but if request suffices (in Ruby, nil and false are the only values that will fail an if, and you can't get false here to confuse the issue).
No, the current directory. If you are located in /path/to/my/prompt but start your script with /path/to/my/script.rb, then it will write the file as /path/to/my/prompt/file.txt. It will only write in the directory of the ruby script if you're there as well.
|

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.