Ruby:检查对象是否为零 [英] Ruby: check if object is nil

查看:41
本文介绍了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)

据我所知,它不是在字符串上调用 split,而是在 nil 上调用.这部分是解析 web 服务器日志.但我不明白为什么它会得到 nil.据我所知,它是空的.

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?

顺便说一下,我如何在 ruby​​ 中写入文件?我在windows下的这个cmd窗口中无法正常阅读.

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

推荐答案

你在这里似乎有几个问题,所以我将尝试一下似乎是主要问题:

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

如果您想查看某个内容是否为零,只需使用 .nil? - 所以在您的示例中,您可以只说 request.nil?,它返回 true 如果它是 nilfalse 否则.

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.

这篇关于Ruby:检查对象是否为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆