Actually i'm experimenting with ruby-lint and ruboto to improve my code. ruby-lint says:
"get.rb: error: line 89, column 14: undefined constant ParseConfig"
On that place i have the marked code:
require 'parseconfig'
module PublicanCreatorsGet
def self.config
home = Dir.home
config = ParseConfig.new("#{home}/.publicancreators.cfg") <-------
end
end
But what makes this to a constant? I thought they are UPPERCASED.
A variable whose name begins with an uppercase letter (A-Z) is a constant.
Ruby treated class name as constant, if you really want to solve the error you can try following in your module
require 'parseconfig'
module PublicanCreatorsGet
include ParseConfig
def self.config
home = Dir.home
config = ParseConfig.new("#{home}/.publicancreators.cfg")
end
end
Related
Okay so I am completely new to using IRB so I have no idea what I am doing wrong.
Here is my gem that I built
# frozen_string_literal: true
require_relative "badwordgem/version"
require 'yaml' #this will need to use the badlist.yml file
module Badwordgem
class Error < StandardError; end
class Base
class << self # assign class to itself so self does not need to be used for every method
def badlist
#badlist ||= YAML.load_file(File.expand_path("badlist.yml", __dir__)) # This will load in the bad words from our YML file and it will assign it to the badlist variable
end
def sanitize(input = "sassy") # method to take in user input to check for profanity
word = input.downcase # It will change the user input and set it to lowercase to match our bad list of words
badlist.each do |key, value| # For every word in the badlist assign it a key(a bad word), and a value(then replace the bad work with * symbol)
word.gsub!(/\b#{key}\b/, value) # For each word the user has inputed replace the old word(key) with the new word(value)
end
word # return the word whether it contains profanity or not
end
end
end
end
Essentially it tests to see if a word is bad or not based on my list of badwords.
I have installed and built it but I have no idea how to test it in IRB.
I've tried running badwordgem.sanitize(hello) etc. but that clearly is wrong as it gives me this error undefined local variable or method badwordgem' for main:Object'
What command in IRB do I type to test it??
I have a module and class in a file lib/crawler/page-crawler.rb that looks like this:
require 'oga'
require 'net/http'
require 'pry'
module YPCrawler
class PageCrawler
attr_accessor :url
def initialize(url)
#url = url
end
def get_page_listings
body = Net::HTTP.get(URI.parse(#url))
document = Oga.parse_html(body)
document.css('div.result')
end
newpage = PageCrawler.new "http://www.someurl"
#listings = newpage.get_page_listings
#listings.each do |listing|
bizname = YPCrawler::ListingCrawler.new listing['id']
end
end
end
Then I have another module & class in another file lib/crawler/listing-crawler.rb that looks like this:
require 'oga'
require 'pry'
module YPCrawler
class ListingCrawler
def initialize(id)
#id = id
end
def extract_busines_name
binding.pry
end
end
end
However, when I try to run this script ruby lib/yp-crawler.rb which executes the page-crawler.rb file above and works without the YPCrawler call, I get this error:
/lib/crawler/page-crawler.rb:23:in `block in <class:PageCrawler>': uninitialized constant YPCrawler::ListingCrawler (NameError)
The issue is on this line:
bizname = YPCrawler::ListingCrawler.new listing['id']
So how do I call that other from within my iterator in my page-crawler.rb?
Edit 1
When I just do `ListingCrawler.new listing['id'], I get the following error:
uninitialized constant YPCrawler::PageCrawler::ListingCrawler (NameError)
Edit 2
Here is the directory structure of my project:
Edit 3
My yp-crawler.rb looks like this:
require_relative "yp-crawler/version"
require_relative "crawler/page-crawler"
require_relative "crawler/listing-crawler"
module YPCrawler
end
In your yp-crawler.rb file, based on the structure that you posted, you should have something like:
require 'yp-crawler/version'
require 'crawler/listing-crawler'
require 'crawler/page-crawler'
Try this, in your yp-crawler.rb add the line:
Dir["#{File.dirname(__FILE__)}/crawler/**/*.rb"].each { |file| load(file) }
That should automatically include all files in your /crawler directory at runtime. Might want to do the same for the other directories.
Let me know if that helps :)
I am following Lynda tutorial on Rspec. I was solving one of the challenges but something seems not to be working.
My file structure is:
food_finder/lib/support/number_helper.rb
food_finder/spec/support/number_helper_spec.rb
My number_helper.rb looks like:
module NumberHelper
def number_to_currency(number, options={})
#some_code
end
end
and number_helper_spec.rb is:
describe 'NumberHelper' do
include NumberHelper
describe '#number_to_currency' do
#some_test_code
end
end
on executing form food_finder directory:
rspec spec/support/number_helper_spec.rb
I get error:
in `block in ': uninitialized constant NumberHelper
(NameError)
Your code was fine. The module should be loaded with include NumberHelper, not require 'support/number_helper'. The problem came from not running rspec --init from the app's root, so none of the configuration in spec_helper.rb got loaded.
Regarding the comments mentioning Rails' number_to_currency method, the module being loaded in this exercise is modeled after the Rails version, but is self-contained:
number_helper.rb
# This module illustrates how additional functionality can be
# included (or "mixed-in") to a class and then reused.
# Borrows heavily from Ruby on Rails' number_to_currency method.
module NumberHelper
def number_to_currency(number, options={})
unit = options[:unit] || '$'
precision = options[:precision] || 2
delimiter = options[:delimiter] || ','
separator = options[:separator] || '.'
...
I'm trying to learn Ruby, but have a previous Java background, and I thought the best way to learn Ruby is to re-implement an old side project.
The problem that I am facing is that I get the following error
ATMSystem.rb:4:in `show_start_menu': uninitialized constant ATMSystem::BankComputer (NameError)
I am using two classes, the first is the BankComputer class, and the second is ATMSystem.
class BankComputer
attr_accessor :bank_id, :customer_accounts
##card_number = 1000
def initialize(bank_id)
#bank_id = bank_id
end
def self.card_number
##card_number
end
def create_card_number
##card_number += 1
end
bc = BankComputer.new(100)
puts bc.bank_id
puts BankComputer.card_number
end
The second class:
include BankComputer.rb
class ATMSystem
def show_start_menu
bank_computer_1 = BankComputer.new(1)
end
system = ATMSystem.new()
system.show_start_menu
end
Both classes are in the same directory.
Why doesn't "include BankComputer.rb" work?
How do I import this class correctly?
I found the answer, all you have to do is
require './ClassName'
I am calling a method in a module from another module and am getting a weird error.
require 'nmap'
...
module Enumeration::Hostnames
def reverse_dns ip_addrs
...
ip_addrs.each do |ip_addr|
list = ListScan.test ip_addr #this is the problem
...
end
...
ListScan is in the nmap file.
module ListScan
def ListScan.test target
target = '-sL ' + target
ListScan::parse_results Nmap::Parser.parsescan('nmap',target)
end
...
end
The error is `const_missing': uninitialized constant Enumeration::Hostnames::ListScan (NameError) on the line ListScan.test ip_addr.
Why is it assuming that ListScan is in the Enumeration::Hostnames module? Mixing in ListScan to Hostnames didn't work.
Ruby searches for constants starting from the current context, which in this case is Enumeration::Hostnames.
Try using
::ListScan.test ip_address