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

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.

Related

undefined local variable or method for extended module

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

Can I programmatically retrieve a gem's spec from the gem itself?

I have a gem. Is there a way to retrieve the gem's specification from within the gem itself?
Specifically, I have some metadata that I want to retrieve and display when running one of the gem's executables. Is there a way to retrieve that metadata from within the gem?
Since your gem is definitionally already loaded by this point, Gem.loaded_specs['name-of-your-gem'] will give you what you want.
Documentation for Gem.loaded_specs

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 to work with classes and dependences in ruby

I have changed this question to better reflect what it is I do not understand.
For example if I try to access the methods in the railties class AppBuilder.
require 'rails/generators/rails/app/app_generator'
g = Rails::AppBuilder.new
puts g.rakefile.inspect
I get an error message activesupport-3.1.3/lib/active_support/secure_random.rb:5:in `': uninitialized constant SecureRandom (NameError)
I do not understand this. Should not each class be "independent" from other classes? Is that not the whole point of object oriented programing?
And now If it is not so more importantly how can I figure out what dependences I need to add? Is it some kind of workflow to solve this? Can I somehow figure out what dependencies to add looking at the documentation? Do this problem have something to do with load path? Can I load all dependences from a gem or rails or whatever? I just don't get it!
doc: http://api.rubyonrails.org/classes/Rails/AppBuilder.html github: https://gist.github.com/rails/rails/blob/master/railties/lib/rails/generators/rails/app/app_generator.rb
there is no easy way to find out which dependencies are used within AppBuilder, cause most of the dependencies are setup somewhere else. so most of the times you need to do some trial and error to get the dependencies right.
the code that you posted has bad style. please get familiar with how to write ruby code properly. buy yourself a book i.e. eloquent ruby or just start reading ruby blogs.
dependencies in ruby is quite simple. every file that you require will be loaded and the load will recurse through the files and then load other requires. the loading will only work if all the required files are on the load path. this load path is similar to your system path and you can add directories to it to tell ruby where to look for files.
in general, there are dedicated entry-points for libraries and their dependencies. those are normally documented, so that if you use them, you get all dependencies right from the beginning. an example for this would be to require 'rails' in order to use rails or require 'active_support/all' if you just want to use active-support. if you wan't to chery-pick files/classes than you are on your own finding out which other classes you need. that part has nothing to do with oop, it's more an dependency-issue (other languages have explicit decleration of dependencies).
in your case, the next step would be to add require "securerandom" to the beginning of your file and then check wich error comes up next.

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