如何将Ruby gem加载到模块中 Gemfile 您的模块price_scraper_module.rb文件

我正在尝试使用Watir编写可按计划运行的网络抓取工具。

我的模块称为saveBox.on("click",function () { //e.preventDefault(); $("textarea").each(function () { // set a variable to select the textareas attributes var hour = $(this).attr("hour"); //console.log(hour) //set a variable to select value of users input at certain hour var plans = $(this).val(); //console.log(plans); //i saved the hour's plan to local storage localStorage.setItem(hour,plans); //var hourInput = localStorage.getItem(hour) }); }); ,但未加载。我收到此错误:

PriceScraperModule

我的模块如下:

NameError (uninitialized constant PriceScraperModule::Watir)

我的Gemfile包括:

module PriceScraperModule
  
  def self.scrape
    browser = Watir::Browser.new
  end
end

当我尝试要求它时,它也不起作用:

gem 'watir'
gem 'webdrivers'

我收到此错误:

module PriceScraperModule
  require 'watir'
  
  def self.scrape
    browser = Watir::Browser.new
  end
end

我该怎么办?

iCMS 回答:如何将Ruby gem加载到模块中 Gemfile 您的模块price_scraper_module.rb文件

我已经编写了以下代码,并且可以正常工作。

require 'watir'
module PriceScraperModule
  def self.scrape
    b = Watir::Browser.new
    b.goto 'www.google.com'
  end
end

PriceScraperModule.scrape
,

解决了。在您的终端中运行

pygame
,

我只是在检查我对Rails的评论时所做的检查,但是您没有使用Rails,因此,如果您想将其作为带有捆绑器的单个红宝石项目使用,将会起作用。

Gemfile

# frozen_string_literal: true

source 'https://rubygems.org'

git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

gem 'watir'
gem 'webdrivers'

您的模块price_scraper_module.rb文件

module PriceScraperModule
  require 'watir'

  def self.scrape
    browser = Watir::Browser.new
    browser.goto 'www.google.com'
  end
end

现在要使用的任何文件我都在使用sample.rb

#!/usr/bin/ruby
$LOAD_PATH << '.'
require 'price_scraper_module'
include PriceScraperModule

PriceScraperModule.scrape

现在只需运行以下命令即可获取所有宝石

bundle

并运行示例

ruby sample

尽管您的一个文件可以正常工作,但是如果您希望使用Gemfile将其作为单个项目的一部分。

本文链接:https://www.f2er.com/1860045.html

大家都在问