Undefined method error associated with a particular method - ruby

I've defined a method in a file called utility.rb. I've tried to call the method inside another file called main.rb. In main.rb, I did require that file by saying require utility, but when I ran main.rb, it gave me the undefined method error associated with that particular method. utility.rb and main.rb are in the same directory. Any idea?

I guess you have a file utility.rb in any of your installed ruby libraries. This file is loaded instead of your one. Choose a more specific name for your file or add . to the beginning of your lib search path:
$:.unshift('.')

Related

Why does require_relative() return a "cannot load such file -- album" error message when "lib/artist" is passed as argument?

I am trying to load a file into IRB. My working directory is my project's root folder, which has a lib folder. The lib folder has album.rb and artist.rb. When I call require_relative("lib/album") or require("./lib/album") it returns "true". When I call require_relative("lib/artist") or require("./lib/artist"), it always returns the error message, "cannot load such file -- album". It seems as if it can only look for album.rb. What is causing this?
require only lets you include a library once. Try switching it around, and doing require_relative 'lib/artist' first after reloading IRB. It should work.

Using File::read in a provider's default.rb in Chef

I am trying to create an LWRP that will call the resource that is defined within itself. My cookbook's structure is as follows:
In the machine cookbook's provider, I have a code snippet as follows:
require 'chef/provisioning' # driver for creating machines
require '::File'
def get_environment_json
##environment_template = JSON.parse(File::read(new_resource.template_path + "environment.json"))
return ##environment_template
end
The code is only trying to read a json file and I am using File::read for it.
I keep getting an error as follows:
LoadError
cannot load such file -- ::File
Does anyone know how I can use File::read inside my LWRP's provider?
OK, so the prior two answers are both half right. You have two problems.
First, you can't require ::File as it's already part of Ruby. This is the cause of your error.
Second, if you call File.read you will grab Chef's File not ruby's. You need to do a ::File.read to use Ruby's File class.
require '::File'
Is incorrect and is causing the LoadError. Delete this line. You don't need it. File is part of the Ruby core and doesn't need to be required.
To further explain, the string argument to require represents the file name of the library you want to load. So, it should look like require "file", or require "rack/utils".
It happens becuase Chef already has a file resource. We have to use the Ruby File class in a recipe.We use ::File to use the Ruby File class to fix this issue. For example:
execute 'apt-get-update' do
command 'apt-get update'
ignore_failure true
only_if { apt_installed? }
not_if { ::File.exist?('/var/lib/apt/periodic/update-success-stamp') }
end
Source: https://docs.chef.io/ruby.html#ruby-class

Use Ruby Class In A Different Directory

I have written a few ruby classes. However, when trying to access one from another directory I am getting the following error:
uninitialized constant Main::AppVersion
This is what the directory structure looks like:
home --> a --> app_version.rb
home --> b --> c --> lib --> main.rb (and other classes)
Everything within "lib" can see each other. However, when trying to access app_version, it fails. I added the path to app version (home/a) to the $LOAD_PATH. So it should be available from there. I have also tried "requiring" my other class, but when I do that I get the following error:
LoadError: no such file to load -- AppVersion
Any idea on what I could be doing wrong here would be highly appreciated. Thanks!
Can try using require_relative:
require_relative '../../../a/app_version'
You error is:
LoadError: no such file to load -- AppVersion
So require is looking for a file AppVersion.rb. And you said the file name is app_version.rb. Try to load it with:
require 'app_version'
after setting up the $LOAD_PATH.
When you define Main::AppVersion class it's supposed to reside in main folder (i.e. main/app_version.rb)
So it doesn't depend on $LOAD_PATH.
You will have to require the file.
LoadError: no such file to load -- AppVersion
Do you require "app_version" ? Or require "AppVersion"? (first one is correct, and consider to require_relative)
Another option is to run ruby and give the include path like
ruby -Ia -Ib/c/lib b/c/lib/main.rb
.

Call one Ruby Script from Another

I am having a perplexing problem. I want to call one ruby script from another.
With this in mind, I create a testscript.rb and executed it, it contains this code,
require './paypal.rb'
puts paypal['L_AMT0']
This code returns a number, which is my paypal balance. It relies on a paypal.rb file which uses the ruby-paypal gem. When I do ruby testscript.rb I get the output of my paypal balance, which means it is working properly. This tells me that my method for calling one RB from another is okay, since, in the above scenario, testscript.rb is getting a variable that is returned from paypal.rb.
Using this same logic, I inserted the above code into another program which is called SiriProxy. It is a ruby program. I need it to get my paypal balance.
So Inside that program, I did a require paypal.rb, it failed because it could not find it, so I set an absolute path in require which fixed it.
However, when SiriProxy (the other ruby rb giving me an issue) trys to run puts paypal['L_AMT0'] it results in an error and ends the program.
This is the error,
[Info - Plugin Manager] Say: Checking Paypal Balance
/home/siriproxy/.rvm/gems/ruby-1.9.3-p194#SiriProxy/gems/siriproxy-0.3.0/plugins/siriproxy-example/lib/siriproxy-example.rb:47:in `block in <class:Example>': undefined local variable or method `paypal' for #<SiriProxy::Plugin::Example:0x931a228> (NameError)
from /home/siriproxy/.rvm/gems/ruby-1.9.3-p194#SiriProxy/bundler/gems/cora-1edcfb9073d5/lib/cora/plugin.rb:47:in `instance_exec'
from /home/siriproxy/.rvm/gems/ruby-1.9.3-p194#SiriProxy/bundler/gems/cora-1edcfb9073d5/lib/cora/plugin.rb:47:in `block (2 levels) in process'
In the above output it appears the issue is it does not understand "paypal", as seen here:
undefined local variable or method `paypal'
However, I do not understand why, since in testscript.rb I do the exact same thing and it works.
Can anyone help? Thank you.
Seems like #CodeGnome is right.
From the Kernel documentation:
require(name) → true or false
Loads the given name, returning true if successful and false if the
feature is already loaded.
require_relative(string) → true or false
Ruby tries to load the library named string relative to the requiring
file’s path. If the file’s path cannot be determined a LoadError is
raised. If a file is loaded true is returned and false otherwise.
Loading Files from the Current Directory
I don't know anything about your library or its internals, but it looks like your require statement may be wrong. If you want to load a file from the current directory in Ruby 1.9.3, you should use:
require_relative 'paypal'
Bundler and Gems
If it's a gem that you've installed as part of a bundle (e.g. the gem is defined in a Gemfile), then providing a path in your require statement is wrong. Instead, you need to require the bundled gem as follows:
require 'bundler/setup'
require 'paypal'

How do I require a file from inside a directory with Ruby?

I think I'm missing something here. I have a directory like this:
myapp
|-lib
|-package1
|-dostuff.rb
|-package2
|-dostuff.rb
From an irb console I'm trying to test the library before I add it to my real project (a Rails app). However, typing this:
require 'lib/package1/dostuff'
returns an error saying it can't find the file to load. I added the lib directory to the load path but I'm not able to load the file.
What am I forgetting? The two filenames don't have to be the same but that's how they are to begin with (they are auto-generated from some web services I need to call using soap4r; each package represents a different web service API group)
If the directory "lib" is in the load path, the argument to require must be relative to lib. So require 'package1/dostuff' without the lib, otherwise it will look for lib/lib/package1/dostuff.rb.
In Ruby 1.9 there's the new require_relative method, which would let you do require_relative "../package2/dostuff" from within package1/dostuff.rb.

Resources