English 中文(简体)
如何在物体不为零时才使用一种方法
原标题:Ruby how to call a method only when the object is not nil

我撰写一份简单的文字,从德比亚网站收集一些包裹的细节。 我在处理虚拟一揽子计划时遇到问题,因为实际上一揽子计划没有与它们挂钩。

我收到以下错误信息:

undefined method `first  for nil:NilClass (NoMethodError)

专线

version = doc.css( #content h1 ).text.strip.scan( /(([^>]*))/).last.first

我试图把它当作一种条件,但这样做却不可行。

if doc.css( #content h1 ).text 
         version = doc.css( #content h1 ).text.strip.scan( /(([^>]*))/).last.first
      end

因此,我很想知道,我如何检查该物体是否为零,然后试图从中提取分号。

此处为整个文字,除非加起来。

require  rubygems 
require  nokogiri 
require  open-uri 


# additional code to make sure that we can resume after a break seamlessly
last_package = 0
File.open( lastbreak , r ) { |fptr| last_package = fptr.gets.to_i }
puts "Resuming from package:#{last_package}" if last_package != 0

# to read each package from packageslist.txt and fetch the required info
# also to store this into a file that can easily be read by the c++ program
BASE_URL = "http://packages.debian.org/stable/"

File.open( packages_list.txt , r ) do | fptr |
  while line = fptr.gets
    package_id = line.split[0].to_i
    package = line.split[1]
    dependencies = ""
    url = BASE_URL + package
    if package_id >= last_package
      doc = doc = Nokogiri::HTML(open(url))
      doc.css(".uldep a").each do |dependency|
        dependencies << dependency.text + ","
      end
      dependencies = dependencies.split( , ).uniq.join( , )
      description = doc.css( #pdesc ).text.strip
      version = ""
      unless doc.css( #content h1 ).nil?
          version = doc.css( #content h1 ).text.strip.scan( /(([^>]*))/).last.first
      end

      File.open("packages/#{package}","w") do |wfptr|
      wfptr.puts "PackageId:#{package_id}"
      wfptr.puts "Name:#{package}"
      wfptr.puts "Version:#{version}"
      wfptr.puts "Deps:#{dependencies}"
      end
      File.open("packages/#{package}.description", w ) {|wf| wf.write(description.capitalize)}

      package_id += 1
      puts "Now Processing #{package_id}"
      File.open( lastbreak , w ) { |fptr| fptr.puts "#{package_id}" }
    end
  end
end

现在错误的信息是

/Users/ccuser008/Documents/oops_project/repo/repobuilder.rb:30:in `block : undefined method `first  for nil:NilClass (NoMethodError)
    from /Users/ccuser008/Documents/oops_project/repo/repobuilder.rb:15:in `<main> 
最佳回答

Depends what is being returned nil doc.css( #content h1 ) or doc.css( #content h1 ).text or doc.css( #content h1 ).text.strip.scan( /(([^>]*))/).last

例:

unless doc.css( #content h1 ).text.strip.scan( /(([^>]*))/).last.nil?
    version = doc.css( #content h1 ).text.strip.scan( /(([^>]*))/).last.first
end

除非条款另有规定,否则你可以列入适当条件。

从您的例外情况来看,doc.css ( #content h1 ).text.strip.scan( /([^>]*)/.last似乎是无。 因此,你可以检查。

问题回答

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

foo&.bar

It will return nil if foo is nil, rather than raising NoMethodError.

你们是否利用了尝试?

类似于

doc.css( #content h1 ).text.strip.scan( /(([^>]*))/).last.try(:first)

如果你重新积极支持你的项目,或如果你在铁路上工作,这项工作将继续下去。

研究“工业”方法的辛迪加和文件。

更新:

x = doc.css( #content h1 ).text.strip.scan( /(([^>]*))/).last
version = x.first if !x.nil? # why bother.

Just FYI:我知道这确实适用于你的问题,但是,如果不对文件结构进行不实的检查,那么这个时间也许会连续不断地使用方法。 如果是,案文无效,那么案文是什么?

用途

try(method, *args, &block)

阁下:

doc.css( #content h1 ).text.strip.scan( /(([^>]*))/).last.try(:first)

详见:





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签