undefined method `gsub' for nil:NilClass ruby - ruby

i am a newbie in ruby... I'm trying to convert media into a scorm package using what i found on github but i got an error while trying to run the script in the command prompt undefined method `gsub' for nil:NilClass. I guess it may be due to a defined method. any idea on how i can remove this error ?
dir = ARGV.shift.gsub(/\/+$/, '')
index = nil
media = []
Dir["#{dir}/media/*.json"].each do |file|
id = JSON.parse(File.read(file))
base = file.gsub(/\/media\/.*\.json$/, '')
index = "#{base}/index.html"
name = File.basename file
media.push [name,id]
puts "#{name}: #{id}"
end

As the error says, you are calling the method gsub on an object that is an instance of NilClass, in other words you are calling gsub on nil.
The error message tells you in which method and on which line this happens and how you got to that line of code. You will have to examine the error message to find the place in your code where you are calling gsub on an object that is nil, then you have to examine your code to find out why that object is nil instead of a String as you expect it to.

Related

Proper usage of 'end_with?'?

I'm attempting to gather all files in my local directory with a .xlsx file format and pull them into my AWS S3 Bucket, however, my method for doing that
attr_reader :bucket, :object_key, :input_file_path
def initialize(bucket:, object_key:, input_file_path:)
#bucket = bucket
#object_key = object_key
#input_file_path = input_file_path
end
def call
return unless input_file_path.end_with?(".xslx")
object = s3_resource.bucket(bucket).object(object_key)
object.upload_file(input_file_path)
end
returns me
[5] pry(main)> Accounting::Datev::Utils::ExcelUtil.new(bucket: nil, object_key:nil, input_file_path: nil).call
NoMethodError: undefined method `end_with?' for nil:NilClass
Am I using end_with? incorrectly?
As #razvans points out, the error message here is great.
The Error
You're initializing your object with input_file_path: nil, and then inside of call you're checking if #input_file_path (set to input_file_path in the initializer, AKA nil) ends with a string.
There is no .end_with? method on nil. That's causing the NoMethodError error.
The Solution
You could guard against nil in your end_with? call, or give it a default value in the initializer.

undefined method constantize in rake task

I'm trying to run this code"
FACTORY = %w(ProcessedTransaction Chargeback).freeze
FACTORY.constantize.each do |factory|
factory.public_send(:delete_all)
end
end
But I get this error: NoMethodError: undefined methodconstantize' for #`
Do you know how I can solve the issue?
In ruby uppercased variables are constants by default so you can't call constantize on it. In your case it's just an array so this should work:
FACTORY = %w(ProcessedTransaction Chargeback).freeze
FACTORY.each do |factory|
factory.constantize.public_send(:delete_all)
end
You can call String#constantize only on strings but you are calling it on array FACTORY.
Remove FACTORY.constantize and add factory.constantize.public_send(:delete_all)
Also make sure you have ActiveSupport::Inflector required

I have a Ruby undefined local variable or method error

When I try to run this code:
class Message
##messages_sent = 0
def initialize(from, to)
#from = from
#to = to
##messages_sent += 1
end
end
my_message = Message.new(chicago, tokyo)
I get an error that tells me that one of my parameters is undefined local variable. I was just trying to create an instance using Message, and was curious as to why this was not working. I thought this would work as I am calling the class.
Problem
With your current code, you get this error:
undefined local variable or method `chicago' for main:Object (NameError)
because the way you instantiated the Message class:
my_message = Message.new(chicago, tokyo)
chicago and tokyo are interpreted as variable or method that you did not actually define or declare, that's why you got that error.
Solution
I think, you just wanted to pass two string objects (putting chicago and tokyo in the quotes) as the arguments of Message.new call like this:
my_message = Message.new('chicago', 'tokyo')
This will solve your problem.
Hope this makes it clear why you are getting the error and how to solve the problem.
The 'undefined local variable' error is showing up because there's no value associated with chicago or tokyo. If you want to just pass them as strings, wrap them in quotation marks instead, like this:
class Message
##messages_sent = 0
def initialize(from, to)
#from = from
#to = to
##messages_sent += 1
end
end
my_message = Message.new("chicago", "tokyo")

NoMethodError Occurred, undefined method for nil:NilClass

First of all, I know it has been posted, I've seen most of the question posts but I still don't understand how it works.
So I'm getting the error:
Script 'Terrain Tag Detection ~' line 115: NoMethodError occurred.
undefined method '[]' for nil:NilClass
My 'Terrain Tag Detection ~' Script looks like this: http://pastebin.com/PUypTwJs (can't paste the code here correctly, and yes it's about Pokemon).
That means your #map = WildPokemon.fetch($game_map.map_id) method doesn't return and value and you want to access #map variable.
You can add a check in your code like below:
#map = WildPokemon.fetch($game_map.map_id)
if #map.present?
#enemy = #map[0][rand(#map[0].size)]
#level = #map[1][rand(#map[1].size)]
end

undefined method `at_css' for #<String:0x007fe9ac5ae310> (NoMethodError)

I'm trying to get all of my prices in my array of URLS, getting the values with a CSS selector.
The thing is, the method at_css is giving me the following error:
undefined method `at_css' for string (NoMethodError)
Can anyone help me? Thanks
test = ["www.myweb.com/1", "www.myweb.com/2"]
test.each do |item|
Nokogiri::HTML(open(item))
puts item.at_css('.itemprice').text
puts item.at_css('.description').text
puts "Empty Line"
end
The item variable in your block is a string i.e. an element from your test array variable. And strings don't have a to_css method in Ruby. You probably wanted to call the to_css on some Nokogiri-related object.
I think you need the following:
some_var = Nokogiri::HTML(open(item))
some_var.at_css('.itemprice').text

Resources