Using a class inside module - ruby

I am new to ruby and trying to use a facility provided by a ruby gem 'combine_pdf'. As described in the documentation, I am trying to do a CombinePDF.load("file1.pdf").
However , I am getting an error
Uninitialized constant CombinePDF in X::Y ( or something similar).
The class inside which I am using combine pdf is present inside a module X::Y. And ruby is somehow also trying to look for CombinePDF in the same package. This is actually a rails project and I have combinepdf in the gemfile.

Use double colon:
::CombinePDF
It is all about constants resolution mechanizm - double colon means, you want to reference the constant, defined in the outermost scope.

It sounds like although you included combine_pdf in the Gemfile, you did not require it in the file in which you use it. You should have this in that file:
require 'combine_pdf'
Do you?

Related

Clean monkey patching in ruby

I was looking at this blog entry: 3 Ways to Monkey-Patch Without Making a Mess and I noticed something strange:
# Actually monkey-patch DateTime
DateTime.include CoreExtensions::DateTime::BusinessDays
I've never seen this type of include before and I can't seem to find any documentation on it. How is it supposed to work and am I still supposed to still use a require call to bring in the file? I don't see how it could know the definite path and filename otherwise.
include is a standard module method in Ruby.
You will have to require the relevant file (containing the module definition) unless you have some sort of autoloading mechanism as in Ruby on Rails.

How to correctly use the Site-prism URL expansion

I have a page object with the following setup:
class StudynoteShowPage < SitePrism::Page
set_url "/studynotes{/studynote}"
end
When I use that page
ssp = StudynoteShowPage.new
ssp.load(studynote: #s1)
I get
ArgumentError:
wrong number of arguments (given 1, expected 0)
Any ideas about that?
I've already tried single and double quotes in the set_url, but that makes no difference.
I can see that the addressable gem is included in my Gemfile.lock.
This "should" work. Could you please check the following.
You have a version of site_prism > 2.12 (Or better > 3.0),
You have addressable 2.5+
You're using Ruby 2.2+ (Better 2.5/2.6)
If you are and can re-create a SSCCE raise a Github issue (Ideally with all the code in a small clonable repo here: https://github.com/natritmeyer/site_prism/issues
We have a variety of unit tests and a couple of feature tests that validate this code works (We may have missed something though)

What does it mean to "pollute the global namespace"?

In ruby, some gems choose to "pollute the global namespace".
What does this mean?
How can I see where it's happening?
Why would a gem need to do this?
When faced with two gems that are polluting the global namespace and conflicting, what tradeoffs am I making when I choose to "isolate" one?
For example:
I'm using two gems that are both polluting the global namespace: pry and gli so I'm not able to place my binding.prys where I want anymore.
One solution is to wrap the entire cli in a module:
module Wrapper
include GLI::App
extend self
program_desc "..."
...
exit run ARGV
end
Now I'm able to use my binding.prys wherever I want.
Why did this work?
What tradeoffs am I making when I choose to do "isolate gli"? Or is it "isolate the GLI::App module"?
Ruby has a singular root namespace shared by all code and any constants and globals you define there are universal through the whole application. This makes conflict inevitable if you're not careful about namespacing things.
The module construct is there as a namespace primitive, all constants will be local to that, all classes defined within it. You can also use a class as a namespace if you prefer, it's up to you.
Forcing the include of something into the root namespace is a big problem. That's usually only done in quick scripts that are fairly tiny and self-contained. That's a bad habit to get into when you're doing anything non-trivial as it mashes together all the constants and methods in those two contexts, potentially over-writing them.

Short namespace acronym in ruby

I'm very new to ruby. I use IronRuby and my ruby code has long namespaces:
Company:: Division::Group::Product::Package.new
since I use this ns multiple times is there a way to create a shortcut? In c# I add a using clause so I'm not required to specify the full prefix.
You can simply assign it to another constant, like:
Package = Company::Division::Group::Product::Package
Package.new
You can also use the "include" method, which is more Ruby-esk:
include Company::Division::Group::Product
Package.new
The difference between this and the current answer is that this one pulls in all constants under the namespace, where the current answer only pulls in that name.

how to run a spec file with ruby without spec

How to run a spec file with ruby without spec?
How do I inherit the spec base class to the current ruby spec file?
I think all you need are the files required in the spec_helper.rb you should be able to call the specs with
ruby -Ispec my_spec.rb #=> assuming you have a spec folder and there is a spec helper inside.
That's two questions.
1) "how to run a spec file with ruby without spec?"
either put
require "rubygems"
require "spec"
require "spec/autorun"
in the file, or run
ruby -rrubygems -rspec -rspec/autorun whatever_spec.rb
from the command line. But spec is easier.
2) "How do i inherit the spec base class to the current ruby spec file?"
Basically, you don't. RSpec is an internal DSL which means that it generates objects for you based on your describe and it blocks. These objects are instances of real classes (e.g. Spec::Example::ExampleGroup), but they're very complicated and magical and you shouldn't try to extend them unless you really know what you're doing. What are you trying to accomplish?

Resources