Ruby-Processing : Undefined method error - ruby

I am trying to run a ruby-processing file
vishrut#vishrut-XPS-L501X:~/Twitter-Emotion-Graphs/samples/contributed$ rp5 run sand_traveler.rb
undefined method `current' for Processing::App:Class
./sand_traveler.rb:63:in `initialize'
./sand_traveler.rb:43:in `reset_all'
org/jruby/RubyFixnum.java:256:in `times'
./sand_traveler.rb:40:in `reset_all'
./sand_traveler.rb:22:in `setup'
/var/lib/gems/1.8/gems/ruby-processing-1.0.11/lib/ruby-processing/app.rb:211:in `handleDraw'
Context and code for sand_traveler.rb : http://ashkenas.com/codework/ruby-processing.html
I am pretty sure all necessary modules have been installed (java, ruby, ruby-processing)
I am able to run other other ruby-processing .rb files using rp5. Only this one is showing errors.
PS: Does the file location matter? The file is in my home folder.

Try replacing line 63 of sand_traveler.rb:
#app = Processing::App.current
with:
#app = $app
I get the same error as you, and this fixed it for me. ($app is global handle to the running sketch, which is also what Processing::App.current should do -- see CHANGELOG entry for v0.3).
As to why it doesn't work -- it seems this method has been removed from the latest version of ruby-processing. If you look at version 0.8 for example, Processing::App defines a class method on line 40 of ruby-proccessing.rb:
def self.current; #current_app; end
Such a method is missing in the latest version.

Related

How to read values from _config.yml file in Jekyll hook

I am trying to read specific part of _config.yml in my Jekyll hook method something like this:
Jekyll::Hooks.register :site, :after_init do
lm = Jekyll.config("latex-macros")
end
in _config.yml is:
latex-macros:
- ["\\RR", "\\mathbb{R}"]
so in lm variable should be:
[["\\RR", "\\mathbb{R}"]]
I already tried to to use Jekyll.configuration({})["latex-macros"] and it kinda worked but it ignores --config terminal option and reads file everytime it is called. This makes it unusable for me.
I also tried
Jekyll::Hooks.register :site, :after_init do
lm = context.registers[:site].config["latex-macros"]
end
but it throws run time error:
katex.rb:8:in '<top (required)>': undefined local variable or method 'context' for main:Object (NameError)
My question is, how to read _config.yml values in jekyll hook properly? How do I fix second method?
Thank you for your help
I am writing this from top of my head since it's been a long time I used Jekyll. You need to pass site variable into hook.
Jekyll::Hooks.register :site, :after_init do |site|
# Access using site.config[key]
puts site.config['latex-macros']
end

Cannot open file ruby.png in Gosu using Learn Game Programming with Ruby

I'm using the Learn Game Programming with Ruby book and I'm trying to just execute the sample code.
I get the following error, using the sample code.
❯ ruby WhackARuby/WhackARuby_1/whack_a_ruby.rb code
/Users/noahclark/.rvm/gems/ruby-2.2.1/gems/gosu-0.10.4/lib/gosu/patches.rb:40:in `initialize': Cannot open file ruby.png (RuntimeError)
from /Users/noahclark/.rvm/gems/ruby-2.2.1/gems/gosuu0.10.4/lib/gosu/patches.rb:40:in `initialize'
from WhackARuby/WhackARuby_1/whack_a_ruby.rb:15:in `new'
from WhackARuby/WhackARuby_1/whack_a_ruby.rb:15:in `initialize'
from WhackARuby/WhackARuby_1/whack_a_ruby.rb:70:in `new'
from WhackARuby/WhackARuby_1/whack_a_ruby.rb:70:in `<main>'
The sample code looks like this:
require 'gosu'
class WhackARuby < Gosu::Window
def initialize
super(800, 600)
self.caption = 'Whack the Ruby!'
#image = Gosu::Image.new('ruby.png')
end
end
Any thoughts on what could be going on here? I've tried changing the offending line to #image = Gosu::Image.new('./ruby.png') for example and that didn't help.
I doubt this is the cause, but my ruby version is ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]
EDIT includes file path:
Invariably the problem is because the file doesn't exist where you think it is.
There are many ways to reference a file. The File documentation has expand_path, realpath, absolute_path, all of which make it easy to reference a file based on an absolute or relative path, and relative to the currently running file, application or a particular directory. How to use them is covered in their examples.
It's important to make sure you know what directory the code considers it's current-working-directory, and/or where the file is. The first is important when using a relative path, and the second is if you don't want to care about your current path and know that the file ALWAYS exists in a certain place.
And, then there's also the case when the file's name is different than what you think it is, or it doesn't even exist.
In my case the solution was using a more explicit filepath:
Gosu::Image.new("#{__dir__}/ruby.png")

Rspec tests not running because of initializer Yaml files

I'm integrating tests with Rspec into quite a large / developed app. When I run the test I'm getting very odd errors from some of my initializers.
For example, my carrierwave.rb loads a yaml file, when I run the test command I get:
carrierwave.rb:11:in `block in <top (required)>': undefined method `[]' for nil:NilClass
which is referring to the second line of code below:
fog_config = YAML::load_file(Rails.root.join 'config/fog.yml')[Rails.env.to_s]
config.fog_directory = fog_config['directory']
Momentarily if I remove that line of code in carrierwave, omniauth starts to complain too:
omniauth.rb:4:in `block in <top (required)>': undefined method `symbolize_keys' for nil:NilClass (NoMethodError)
Again, another yaml file:
fb_config = YAML::load_file(Rails.root.join 'config/fb_app_version.yml')[Rails.env].symbolize_keys
I'm guessing the solution is to change my yaml files or include something in the tests, but I'm not sure what. Any ideas would be most appreciated, thanks.
It looks like you need to include test entries in the Yaml files in question.
For example, your config/fog.yml probably looks something like this:
production:
directory: the_prod_directory
other_keys: ...
development:
directory: the_dev_directory
other_keys: ...
You need to add a test key:
test:
directory: the_test_dir
other_keys: appropriate values

Ruby NoMethodError in Redmine from script/runner

I have some Ruby code I need to execute in the Redmine Ruby app in order to enable a module in all projects at once.
Surprisingly, ruby does access the Projects, but raises a NoMethodError when accessing a few particular methods inside each of the "Project" objects.
Here is the code:
Project.find(:all).each do |project|
print "Enabling modules for project '#{project.identifier}' ... "
puts project.methods.sort # this does print "enabled_module_names"
puts project.enabled_module_names
end
This fails with:
hostname:/srv/apps/redmine# script/runner vendor/plugins/customplugin/lib/enable_modules.rb
/var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/attribute_methods.rb:260:in `method_missing': undefined method `enabled_module_names' for #<Project:0x7f28985c1cb0> (NoMethodError)
from vendor/plugins/customplugin/lib/enable_modules.rb:14
from vendor/plugins/customplugin/lib/enable_modules.rb:7:in `each'
from vendor/plugins/customplugin/lib/enable_modules.rb:7
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `eval'
from /var/lib/gems/1.8/gems/rails-2.3.5/lib/commands/runner.rb:46
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
from script/runner:3
I have scratched my head a lot, but I can't grasp why would the code find the "Project" symbol but not the methods within, especially as "project.methods" does indeed list "enabled_module_names".
Any help most welcome.
Are you sure that enable_module_names is an instance method?
Is it within Project.instance_methods?
Edit (summary of the comments below):
In earlier versions, you have to use following:
enabled_module_names = project.enabled_modules.collect(&:name)
The getter is present in later versions only (see rev.4460 for details of this change)

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'

Resources