Not able to call method in a gem - ruby

This might be an easy question but I was unfortunately not able to find the answer on Google.
Context:
I am working on a project of my own, and I am externalizing some code in a gem (FrenchTaxSystem). It is the first I create a gem and I have difficulties using it properly.
Problem:
When calling a method (like testit) defined in the main file (french_tax_system.rb) of my gem I get a "NoMethodError: undefined method `testit' for FrenchTaxSystem:Module", though I can call constants from this same file (like FISCAL_NB_PARTS_FOR_MARRIED_COUPLE) and it puzzles me.
E.g in IRB I get that when calling a method:
[
And it is the same in my Rspecs tests inside my gem
However when calling a constant I have no error:
Main file in my gem:
french_tax_system.rb
module FrenchTaxSystem
class Error < StandardError; end
# Constants
...
FISCAL_NB_PARTS_FOR_MARRIED_COUPLE = 2
...
# Methods
## Main method
def testit
"test me"
end
end
Gem file structure:
Thank you in advance for your help,
Mth0158

This should work:
module FrenchTaxSystem
def self.testit
"test me"
end
end

Related

Cucumber: Unable to see methods of a module included in the World?

I'm working with cucumber/ruby and I wanted to create a new module with some methods to use them in my step definitions.
I was reading how to do this here, https://github.com/cucumber/cucumber/wiki/A-Whole-New-World. But when I've tried the following I get an error:
create the new module under /root_location/lib/new_module.rb
create the module as:
.
module Newmodule
def here
puts "here"
end
end
World(Newmodule)
However, when I then try to use the 'here' method from my steps definition, I just get:
undefined local variable or method `here' for # (NameError)
Any idea what I am doing wrong?
The module needs to be located in features, otherwise it won't be added into World. Cucumber does not look outside of features for anything unless you specifically tell it to.
Put this code either in features/support or features/step_definitions

Monkey-patch using modules in a gem

I'm building a Ruby gem that includes a module that's meant to monkey-patch the Hash class to add a new method. I'm following this guide to try to do it neatly: http://www.justinweiss.com/articles/3-ways-to-monkey-patch-without-making-a-mess/
I've placed the module in lib/core_extensions/hash/prune.rb, and the module is declared as such:
module CoreExtensions
module Hash
module Prune
##
# Removes all pairs from the Hash for which the value is nil. Destructive!
def prune!
self.reject! { |_, v| v.nil? }
end
end
end
end
And in order to make the monkey patch take effect, I'm calling this within the main gem file:
Hash.include(CoreExtensions::Hash::Prune)
But after building the gem and trying to require it in an irb console, I get the following error: NameError: uninitialized constant Gem::CoreExtensions (Gem is a placeholder name).
I made sure to include the prune.rb file in my gemspec's files array: s.files = ['lib/gem.rb', 'lib/core_extensions/hash/prune.rb'], so I'm not sure why it can't detect the file and its modules. Can anyone help me figure this out?
Thank you!
EDIT: In case it will help anyone else - I tried to require the module file using require 'lib/core_extensions/hash/prune' but received 'cannot load such file' errors. Sticking ./ in front of the path fixed it.

ruby unit testing at_start not found

I am using test-unit-2.5.5 with ruby 1.9.3. In http://test-unit.rubyforge.org/test-unit/en/Test/Unit.html#at_start-class_method there is a method called at_start as part of the ruby test::unit module from version 2.5.2. I tried to use it from the examples on the page like so:
class TestAOS < Test::Unit::TestCase
Test::Unit.at_start do
puts "start"
end
Test::Unit.at_exit do
puts "Exit!"
end
But when I run my test I get the following:
NoMethodError: undefined method `at_start' for Test::Unit:Module
TestAOS at unit/TestAOS.rb:8
(root) at unit/TestAOS.rb:7
Do I need to do anything first before this method can be used? I'm new to ruby
When I comment out the at_start bloack and run the test I get a different error for at_exit:
NoMethodError: private method `at_exit' called for Test::Unit:Module
TestAOS at unit/TestAOS.rb:12
(root) at unit/TestAOS.rb:7
A
In the example provided by your link the
Test::Unit.at_start do
puts "start"
end
is called outside of test class. You are calling it from inside of your test class. Just move it outside of your TestAOS

How can I get rid of the following warning: Problem while setting context on example startundefined local variable or method `selenium_driver'

Still making my first steps in Ruby (while dealing with some written code). I am getting the following warning each time I run spec (listed as is):
Problem while setting context on example startundefined local variable or method `selenium_driver' for #<Spec::Example::ExampleGroup::Subclass_1::Subclass_1:0x7f2d2cd840e0>
(Edit: Split into two lines, it says)
Problem while setting context on example start
undefined local variable or method `selenium_driver' for #<Spec::Example::ExampleGroup::Subclass_1::Subclass_1:0x7f2d2cd840e0>
While grep-ing through Ruby code - could find the following:
/home/user/.rvm/gems/ruby-1.8.7-p334#frontend/gems/selenium-client-1.2.18/lib/selenium/rspec/spec_helper.rb: STDERR.puts "Problem while setting context on example start" + e
So here is the excerpt from the source code of spec_helper.rb:
config.append_before(:each) do
begin
if selenium_driver && selenium_driver.session_started?
selenium_driver.set_context "Starting example '#{self.description}'"
end
rescue Exception => e
STDERR.puts "Problem while setting context on example start" + e
end
end
Kindly advise how can I solve the (potential) problem.
Update: This grep might be helpful as well:
user#vm-ubuntu:~/dev/branch/tests$
grep selenium_driver *
my_module.rb: #selenium_driver = driver
my_module.rb: ['TERM', 'INT'].each {|s| Signal.trap(s) { #selenium_driver.stop && Process.exit(1) } }
my_module.rb: return #selenium_driver
Update N2:
My Gemfile:
source "http://rubygems.org" # Default source
gem "hpricot", "~>0.8.4"
gem "json", "~>1.5.1"
gem "rspec", "~>1.3.2"
gem "selenium-client", "~>1.2.18"
My selenium_helper.rb file:
require 'selenium/client'
require "selenium/rspec/spec_helper"
...
The problem is that selenium-client gem expects that you name your driver object 'selenium_driver' and make it visible from the spec.
For example if you initialize selenium like this:
before(:all) do
#driver = create_driver($hub_url, $hub_port, $browser)
#driver.start_new_browser_session
end
You need to change it to look like this:
attr_reader :selenium_driver
before(:all) do
#selenium_driver = create_driver($hub_url, $hub_port, $browser)
#selenium_driver.start_new_browser_session
end
Basically it's same code just different variable name. The selenium-client uses that convention to apply context information to the tests.
It's saying it can't find the variable selenium_driver.
Problem while setting context on example startundefined local variable or method `selenium_driver' for #<Spec::Example::ExampleGroup::Subclass_1::Subclass_1:0x7f2d2cd840e0>
is made up of the string "Problem while setting context on example start" plus the exception error message (what's produced by e) of
"undefined local variable or method `selenium_driver' for #<Spec::Example::ExampleGroup::Subclass_1::Subclass_1:0x7f2d2cd840e0>"`.
Add
gem "selenium-client"
to your Gemfile (don't forget to run $ bundle install)
And add following to spec/spec_helper.rb
require "selenium/client"
require "selenium/rspec/spec_helper"

How do I change the aws-ruby log location?

I've found the method set_log in the documentation, I just can't figure out the syntax to call it. Here's what I tried:
require 'ruby-aws'
Amazon::Util::Logging.set_log('my.log')
NoMethodError: undefined method `set_log' for Amazon::Util::Logging:Module
You can see that Amazon::Util::Logging is a module and set_log is a 'Public Instance method'. So you need
class NewClass
include Amazon::Util::Logging
def foo
set_log('file.txt')
log 'debug_message'
end
end
I ran into this problem when trying to deploy a Ruby-on-Rails site that uses 'aws-ruby' to heroku (I got the "Permission denied - ruby-aws.log" error).
To change the log file location from 'ruby-aws.log' to 'log/ruby-aws.log', I added the following to an initializer. Make sure this is called before you use any of the aws-ruby library. Notice the change on the "set_log..." line.
module Amazon
module Util
module Logging
def log( str )
set_log 'log/ruby-aws.log' if ##AmazonLogger.nil?
##AmazonLogger.debug str
end
end
end
end
A simpler way would be to add this line:
set_log("/dev/null")

Resources