require 'command_line_argument_parser'? - ruby

I'm following a tutorial here for a simple web crawler (http://www.skorks.com/2009/07/how-to-write-a-web-crawler-in-ruby/) and one of the lines is to require 'command_line_argument_parser'.
My system is unable to find the gem. The post was written in 2009. Without the gem, the code I downloaded from this guy doesn't work.
Any idea how to proceed?

If the main point of the code is Web crawling, the cli arg parser can probably be switched out without upsetting much. I would just use Ruby's build-in optparse library. http://ruby-doc.org/stdlib-1.9.3/libdoc/optparse/rdoc/OptionParser.html

I found command_line_argument_parser in the ZIP archive for the tutorial. If you've extracted the file into the same directory as your crawler and it still doesn't work, try changing the require line to require_relative.
I'd also recommend switching to optparse if you have the time though, it's much more robust.

Related

Xzing recognizes Barcode only in Java version

I'm trying to bulk scan some jpg files with barcodes on them. I've used the ruby bindings for the c++ port of xzing. When I have this file:
scanned by the Web-Version of Xzing (https://zxing.org/w/decode.jspx) everything turns out fine. When I try to scan this one in ruby (using https://github.com/glassechidna/zxing_cpp.rb) nothing is recognized. I already tried cranking up the contrast, but it did not help. It's not my ruby setup because it works for loads of other nearly identical codes. The only thing I can think of is any difference between the Java version and the C++ port, but this is absolute poking in the dark, I've started using zxing just today.
Could anyone get this code recognized in ruby? Thank you very much.
The gem you're using and/or it's dependencies our out of date. If you want to still use Ruby for your project, you can try using one of the online services in the comments for the decoding. You could either try to use the
mechanize gem or roll your own using other http ruby tools such as httparty or Ruby's Net::HTTP

Is there a significant difference between the "open-uri" gem and "httparty" gem?

I am scripting a few scrapers for fun and trying to learn the best way to go about it, and I stumbled across these two gems. On the surface they seem to do the same thing but I was curious if I am missing something glaring that separates the two?
actually open-uri is a part of standard library (it's enough when you write require "open-uri" in your ruby code, but you don't have to declare dependency to it in gemspec,
whereas httparty is external gem.
Answering your question - if working with open-uri is easy enough for you just stick with it. All http libraries eventually are just wrappers for net library so you can't be wrong using one or another. I use httparty on a daily basis because I'm accustomed to it.

PhantomJS + CasperJS from Ruby - Reuse Code?

I am calling CasperJS from the backend of my Ruby on Rails application using Open3.popen3 to make a command line call. The filename (in my case CoffeeScript) is the first argument followed by options.
Many of my coffee files do similar tasks. I see examples of of how to reuse code with modules, but I think that's a NodeJS only thing.
Any suggestions how I might reuse common code in my situation? I'm really getting horribly un-DRY.
UPDATE:
hexid's answer is correct. What I was missing when I tried it before is that you need the rooted file path, not relative the current file path:
my_module = require('/rooted/path/to/the/file.coffee')
PhantomJS has support for CommonJS' require.
You won't, however, be able to require NodeJS modules because PhantomJS doesn't run on NodeJS, but instead on a version of Webkit that is included in QT.

Using packages in Ruby?

I just started learning Ruby coming from Java. In Java you would use packages for a bigger projects. Is there anything equivalent to that in Ruby? Or what is the best way to achieve a package like setting?
The way I'm doing it right now is 'load'ing all the needed class into my new Ruby file. Isn't there a way to tell my current Ruby class to use all other Ruby classes in the same folder?
Cheers,
Mike
There's three kinds of package loading in Ruby:
Explicitly loading a library with require
Implicitly loading a library using autoload
Importing a library with gem
The require method is the most direct and has the effect of loading in and executing that particular file. Since that file may go on to require others, as a matter of convenience, you may end up loading in quite a lot at once.
The autoload method declares a module that will be loaded if you reference a given symbol. This is a common method to avoid loading things that you don't need, but making them automatically available if you do. Most large libraries use this method of loading to avoid dumping every single class into memory at once.
The gem approach is a more formalized way of packaging up a library. Although it is uncommon for applications to be split up into one or more gems, it is possible and provides some advantages. There's no obligation to publish a gem as open-source, you can keep it private and distribute it through your own channels, either a private web site or git repository, for instance, or simply copy and install the .gem file as required.
That being said, if you want to make a library that automatically loads a bunch of things, you might take this approach:
# lib/example.rb
Dir.glob(File.expand_path('example/**/*.rb', File.dirname(__FILE__))).each do |file|
require file
end
This would load all the .rb files in lib/example when you call require 'example'.
You probably want to use require rather than load, since it should take care of circular references.
If you want to grab all the files in a given folder, that's easy enough:
Dir.foreach("lib"){|x| require x}
Your other option is to have a file that manually requires everything, and have your other files require that.
You should also look at wrapping the code in your libraries with a module block, to give them their own namespaces.
That said: rightly or wrongly, I tend to feel that this is the one area -- perhaps the only one -- where Ruby is less powerful than Python, say, or Java.
I understand your feeling. It's an ordinary problem you have to face when coming from another language like Java. I'd say try to study Ruby modules but you deserve a longer reply. So my advice is reading a good Ruby book like Eloquent Ruby.

What is the canonical mechanism for creating a Ruby gem?

I've looked in a lot of places - including www.rubygems.org - but can't find any tutorial that describes an easy, straightforward, technique for producing gems that doesn't rely on other (non-standard-Ruby) components, such as newgem and hoe.
I have several requirements for gem production, from the simplest case of one library file+one test file, to complex ones involving C source files and multiple utility .rb files.
All help gratefully received!
I was researching gem making recently and was also surprised that there wasn't a single, obvious way that everyone does it like how RubyGems is the one-stop shop for managing gems. I discovered that you can actually use Bundler to create gems, and I've chosen this route for my own gems. Take a look at this guide on gem development with Bundler by Radar.
look into Jeweler or one of these options:
http://ruby-toolbox.com/categories/gem_creation.html
its seems like overkill, but you don't need to use all the options, you can use it just to create the skeleton of the gem.
I've recently been looking into the same thing. Here are a few sources I found useful Walk through of a simple gem, Gem spec reference. Also I found it useful to check out large projects on github and model after them Thor's Gem spec.
Don't know if you've seen "Gemcutter Is The New Official Default RubyGem Host", but it's a good starting point. RubyGems.org is a good second stop to read Creating Your Own Gem.

Resources