trying to build a pptx to scorm converter. I get an error NoMethodError Undefined method `+' for NilClass. I guess it may be due to a defined method. any idea on how i can remove this error ?
dir = ARGV.shift
dest = ARGV.shift
pptx = dir + "/presentation.pptx"
lis = []`enter code here`
STDERR.puts "Copy template => #{dest}"
FileUtils.cp_r "template", dest
Dir["#{dir}/*.PNG"].each do |file|
STDERR.puts "Copy #{file} => #{dest}/img"
FileUtils.cp file, "#{dest}/img/"
STDERR.puts "Creating thumb #{file} => #{dest}/img/thumb"
name = file.split(/\//).last
system "/usr/bin/convert", "-scale", "200x", file, "#{dest}/img/thumb/#{name}"
lis.push name
end
ordered = lis.sort_by { |x| x[/\d+/].to_i }
DIR is nil
If you debug your code as follows:
puts dir.nil? # true
So, in order to run this code you must provide the ruby shell with 2 arguments, as follows:
ruby test.rb DIRECTORY_NAME DESTINATION_NAME
Related
So, I'm new to Ruby and I'm trying to install this photo gallery to my Jekyll blog. However, when I'm trying to run
jekyll build
I get this error message:
Liquid Exception: undefined method `[]' for nil:NilClass in photography/index.html
jekyll 3.7.2 | Error: undefined method `[]' for nil:NilClass
With --trace it points me to:
/Users/hal9000/Desktop/Plommonstop/_plugins/jekyll-exiftag-mod.rb:18:in `render': undefined method `[]' for nil:NilClass (NoMethodError)
But now I don't understand how to proceed. jekyll-exiftag-mod looks like this:
require 'exifr/jpeg'
#Based on https://github.com/benib/jekyll-exiftag by Beni Buess (MIT License)
#Edited to work as a Liquid-Block instead of a Liquid-Tag, reads the filename from between the
#brackets. --T.Winter
module Jekyll
class ExifTag < Liquid::Block
def initialize(tag_name, params, token)
super
#args = self.split_params(params)
end
def render(context)
#abort context.registers[:site].config['source'].inspect
sources = Array.new(context.registers[:site].config['exiftag']['sources'])
# first param is the exif tag
tag = #args[0]
# if a second parameter is passed, use it as a possible img source
if #args.count > 1
sources.unshift(#args[1])
end
# the image can be passed as the third parameter
img = super
# first check if the given img is already the path
if File.exist?(img)
file_name = img
else
# then start testing with the sources from _config.yml
begin
source = sources.shift
file_name = File.join(context.registers[:site].config['source'], source, img)
end until File.exist?(file_name) or sources.count == 0
end
# try it and return empty string on failure
begin
exif = EXIFR::JPEG::new(file_name)
return tag.split('.').inject(exif){|o,m| o.send(m)}
rescue
"ERROR, EXIF-Tag RB"
end
end
def split_params(params)
params.split(",").map(&:strip)
end
end
end
Liquid::Template.register_tag('exiftag', Jekyll::ExifTag)
With row 18:
sources = Array.new(context.registers[:site].config['exiftag']['sources'])
What does it mean with "Undefined method `[]' for nil:NilClass?" And what exactly is making this problem occur?
The error could simply mean that you have not set ['exiftag']['sources'] in your config file.
Your config file should have something similar to below (entry1 and entry2 are just examples):
exiftag:
sources:
- entry1
- entry2
Do note, indentation is important in YAML as well..
I want to download almost 2GB size zip files (builds) on the target server. For that I have written the following piece of code:
def download_file(src, dest, build_name)
begin
if !File.file?("#{dest}/#{build_name}")
$LOG.debug "#{dest}/#{build_name} does not exist"
$LOG.info "Downloading #{build_name} to #{dest}"
destination = "#{dest}/#{build_name}"
download = open(src)
IO.copy_stream(download, destination)
$LOG.info "Downloading #{build_name} completed succesfully.\n"
rescue => e
puts e
end
end
But it is giving the following error message:
undefined method `copy_stream' for IO:Class
Need help in fixing this. Thanks in advance.
I'm creating a app that sends mass texts using a JSON file with the numbers and names. Every time I test load the app in IRB I get the error:
NameError: undefined local variable or method `data_from_file' for main:Object
from /home/qc/tep/Coding Stuff/Ruby/text app/main.rb:14:in `contacts_from_file'
I understand what the error means, but I don't understand why I'm getting the error, here's the source code:
require 'json'
def sanatize(numbers)
"+1" + number.gsub(/^1|\D/, "")
end
def numbers_from_file
file = open('numbers.json').read
JSON.parse(file)
end
def contacts_from_file
contacts= { }
data_from_file['feed']['entry'].each do |entry|
name = entry['gsx$name']['$t']
number = entry['gsx$number']['$t']
contacts[sanatize(number)] = name
end
contacts
end
def contact_numbers
contacts_from_file.keys
end
def contact_name
contacts_from_file[number]
end
And here's the JSON file:
{
'1**********' => 'Big Bird'
'1**********'} => 'Josh'
}
If anybody could help me and tell me why the data_from_file is "undefined" it would be extremely helpful, thank you ahead of time.
You never define data_from_file, you just try to read from it in the contects_from_file method.
Perhaps you meant numbers_from_file instead of data_from_file?
This is my code:
file = File.open('result.txt', 'w+').read
path = Dir[ENV['HOME'] + '/Desktop/Test/*.txt']
file.puts "this is a #{path} test: "
It comes up with an error:
C:/Users/User/RubymineProjects/Comparison/test.rb:5:in `<top (required)>': private method `puts' called for "":String (NoMethodError)
from -e:1:in `load'
from -e:1:in `<main>'
my intended result is:
this is a C:/Users/User/Desktop/Test/new_1.txt test:
i've tried this:
puts "this is a #{path[0]} test: "
which achieves what i want but as soon as i do file.puts it comes up with the same error again.
When you do file.puts here, you're sending a method #puts to a string object that's now stored in the variable file. This is because File#read method returns a string. So, on the first line, file takes the contents of the result.txt and then stores it in the variable file. Then you're callingputson that string. AndString#puts` is a private method, so you can't use it the way you used it in the code above.
If your intention is to write the result this is a C:/Users/User/Desktop/Test/new_1.txt test:, then you need to use the File.open method this way:
File.open('result.txt', 'w+') do |file|
path = Dir[ENV['HOME'] + '/Desktop/Test/*.txt']
file.puts "this is a #{path} test: "
# whatever else needs to be written goes here
end
Or, if you prefer the imperative style instead of the block style:
file = File.new('result.txt', 'w+')
# If you prefer `open`, that works too!
# file = File.open('result.txt', 'w+')
path = Dir[ENV['HOME'] + '/Desktop/Test/*.txt']
file.puts "this is a #{path} test: "
# ensure this is closed, or you'll have memory issues if you do this often
file.close
I am reading about how to create custom types and providers in Puppet.
But I am getting the error:
Error: Could not autoload puppet/provider/createfile/ruby: undefined method `[]' for nil:NilClass
when running the below code:
mymodule/lib/puppet/type/filecreate.rb
require 'fileutils'
Puppet::Type.newtype(:filecreate) do
ensurable do
defaultvalues
defaultto :present
end
#doc = "Create a file."
newproperty(:name, :namevar => true) do
desc "The name of the file"
end
newproperty(:path) do
desc "The path of the file"
end
end
mymodule/lib/puppet/provider/filecreate/ruby.rb
require 'fileutils'
Puppet::Type.type(:filecreate).provide(:ruby) do
desc "create file.."
puts resource[:name] # this line does not seem to work, why?
puts resource[:path] # this line does not seem to work, why?
def create
puts "create file..."
puts resource[:name]
end
def destroy
puts ("destroy file...")
FileUtils.rm resource[:path]+resource[:name]
end
# Exit method never seems to be called
def exists?
puts "is method beeing called???"
File.exists?(resource[:path])
end
end
I guess the way of fetching the parameter values, puts resource[:name] not is correct. So how can I fetch the filename file.txt declared as the namevar for my custom type filecreate (see below)?
Also, method exists does not seem to be called. Why?
And my init.pp contains this simple code:
class myclass {
filecreate{'file.txt':
ensure => present,
path => '/home/myuser/',
}
}
Your puts calls do not work because you try and access an instance attribute (resource) on the class level. It makes no semantic sense to access the values in this context. Remove those calls.
Generally, it is better to use Puppet.debug instead of puts to collect this kind of information.
To find out where such errors come from, call puppet with the --trace option.