undefined local variable or method for extended module - ruby

NOTE: the question was edited to reflect the issue.
i would like to work with knife-api rubygem. enclosed a snippet of my code
require 'chef'
require 'chef/knife'
require 'knife/api'
module X
module Y
module Z
include Chef::Knife::API
def self.foo
resp = knife_capture :search, ['*:*']
puts resp
end
end
end
end
when X::Y::Z.foo is called, it returns
gems/knife-api-0.1.7/lib/knife/api.rb:41:in `ensure in knife_capture': undefined local variable or method `revert_io_channels' for X::Y::Z (NameError)
it appears that he enclosing scope functions (Chef::Knife within knife-api) are inaccessible within X::Y::Z.foo.
how should i make this work?

In a complete non-answer, do not use the knife-api gem. It should not exist. The correct gem to use if you want to access Chef API data from Ruby code is chef-api (I know, we're real creative with names). If you want to do something very small, check out the knife exec command which just exposes the raw Chef object API (not as refined as chef-api, but built in).

This gem is a fork of another project that is no longer maintained. Looking at the gem code, it appears there were several issues introduced to the latest version (0.1.7) through some bad refactoring of the original code. That version was released a year ago and it hasn't had any commits since. The repo also does not accept issue tickets, and it has no tests. If you must use this gem, I would try a pessimistic constraint gem 'knife-api', '< 0.1.7' and see if that works. However it might be a better idea to skip it entirely. I made an attempt to fix the issues and submit a PR. You can also try pulling the gem from my forked repo https://github.com/msimonborg/knife-api

Related

Making a change to mysql2_adapter.rb in activerecord gem

I want to change mysql2_adapter.rb located in /home/jimcgh/.rvm/gems/ruby-2.0.0-p353#myproject/gems/activerecord-3.2.13/lib/active_record/connection_adapters.
I want to redefine the connect method as follows
def connect
#connection = Mysql2::Client.new(#config)
#connection.options(Mysql::OPT_LOCAL_INFILE, 1) #Im adding this line
configure_connection
end
If i just make this change, will it reflect in my rails app ?. Is there a better way to do this change?
Please Help
Thank You
the best thing to do is fork the github repository, then commit the changes.
then, on your gem file reference it like this:
gem 'activerecord', :git => '##New git Repo##'
this way you wont have problems in production afterwards
Another solution is try to override the gem method on your application, here is another SO question on the topic:
Overriding a module method from a gem in Rails

confluence4r add

We use Jira/Confluence as our wiki site. I've had a difficult time trying to figure out how to use the add. I'm guessing i'm missing something very obvious. When I go to this site: https://confluence.atlassian.com/display/DISC/Confluence4r to download the confluence4r file, not sure what I'm supposed to do thereafter. The file contains a module which makes sense why it doesn't do anything when running it. But should I being using the gem install functionality in some way? When I simply try to use it in a ruby script, i get the following error:
conf.rb:15:in `<main>': uninitialized constant Confluence (NameError)
Where I am supplying the information required per the script (URL, user & pass contained the correct values when used):
server = Confluence::Server.new("https://collab.sitename.com")
server.login("user", "pass")
puts server.getSpaces()
Any information how to get the working is appreciated.
Confluence4r isn't distributed as a rubygem, it's just a ruby script you can drop onto your filesystem and reference directly.
If you put Confluence4r.rb in the same directory as your own script, you'd need to require it like this:
require './confluence4r.rb'
You shouldn't need the "confluence" and "confluence-client" rubygems to use confluence4r; it's just a very thin wrapper around the Confluence XML-RPC API.

How do i implement factual in Ruby?

I chose "factual-api" gem after looking at the factual website. May I know how do I set it up and use the function to get the locations of those areas? Can I write it in the controller/model?
After trying to install the gem, I get this when I look at my gemfile:
Gem "factual-api" is unavailable in SDK 'RVM: ruby-1.9.2-p290
This inspection warns about unavailable gems inside the current SDK. The IDE needs the gems to provide come completion, navigation and analysis. An appropriate quick-fix is available to install the required gems.
When I run the command bundle install, I do not have any errors. But when I called:
factual = Factual.new("YOUR_KEY", "YOUR_SECRET")
I get this:
NameError: uninitialized constant Factual
What am I doing wrong now? What should be the right way to do so?
This answer is a bit late, but I'm posting it anyway so it can be marked as answered.
As mentioned in the comments the recommended approach is to use Factual's Ruby Driver which can be found at http://github.com/Factual/factual-ruby-driver or installed with the following in your Gemfile:
gem 'factual-api', :require => 'factual'
If not using a Gemfile remember to manually require 'factual' somewhere. I also like to put an initializer in the config folder with the OAuth details if it's a private repository that looks something like this:
class Factual
KEY = 'YOUR-OAUTH-KEY'
SECRET = 'YOUR-OAUTH-SECRET'
end
This allows you to access the Factual API anywhere in your app with
factual = Factual.new Factual::KEY, Factual::SECRET

What is the origin of System::Process.new?

Where is the origin of Process.new and where is it doccumented? I have looked in the Ruby docs at the process module and I cannot figure out how this is declared.
The code I am trying to replicate is in the Ruby God gem in lib/god/conditions/memory_usage.rb:66:
process = System::Process.new(self.pid)
#timeline.push(process.memory)
System::Process isn't part of Ruby, it comes from God (the gem) itself. You can view its source if you want.
It's referable as System::Process in the file you reference because you're already in the God module, so Ruby resolves it within that namespace.

What does the World() method do in ruby?

I'd like to know what does the following line do:
World(::Cucumber::Rails::Capybara::SelectDatesAndTimes)
Because I'm getting
uninitialized constant Cucumber::Rails::Capybara::SelectDatesAndTimes::XPath (NameError)
And I don't understand what it is supposed to do to tell what's wrong :-)
EDIT: I gisted my Gemfile: https://gist.github.com/822480 and my gem list on windows vista 32-bit (https://gist.github.com/822483) and ubuntu maverick 32-bit https://gist.github.com/822491. Both are running ruby 1.8.7. Notice that I'm using this fork of cucumber-rails: https://github.com/johnf/cucumber-rails because my ultimate goal is to get these datetime selectors to work with capybara.
I also found that on Linux I get another error message:
Unable to find '#<XPath::Union:0xb6e078b8>' (Capybara::ElementNotFound)
The World() method isn't one from Ruby, it's one specifically from Cucumber, regrettably they haven't even documented it.
http://rubydoc.info/github/aslakhellesoy/cucumber-rails/master/Cucumber/Rails/World:initialize
In this case, it looks like something it missing from your Environment, maybe Caprybara, please give more info, and share your Ruby/Rails/Cucumber/Bundler/etc versions, and your Gemfile.
World is just an instance of Object that you can use in every step definition, its like a library that you expose without the need to import (or require in every step definition file).
see more at: https://github.com/cucumber/cucumber/wiki/Configuring-the-Scenario-Execution-Context-(World)
(Note that I'm still learning, so this may be wrong.)
A world-level method involves multiple objects versus a class- or object-specific method.
If you are having an issue with calling a world method, then check to see if the world method is specifying an object that can't be found.

Resources