Question

I'm completely new to both ruby and rails and have a very basic problem testing some code from the irb command line.

I want to take an arbitrary html string and be able to css selectors on it using the rails HTML module: http://api.rubyonrails.org/classes/HTML/Selector.html

I am in the root directory or my rails app and want to simply include this module, create an "element" from the string, then run a selector to see if it matches.

What I want to do is something the following:

irb> require 'html'
irb> elem = HTML::Document('<div class="nothing"></div>')
irb> sel = HTML::Selector('div.nothing')
irb> matches = sel.select(elem)

However, my naive attempt to require 'html' has failed:

irb(main):016:0> require 'html'
LoadError: cannot load such file -- html
    from /usr/local/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
    from /usr/local/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
    from (irb):16
    from /usr/local/bin/irb:12:in `<main>'
Was it helpful?

Solution

So the thing is, you want to use it from irb where you would want to use the rails console.

If you need to use it from irb you will have to require the right path:

require "action_view"
=> true
require "action_view/vendor/html-scanner"
=> true
HTML::Selector.new "form.login[action=/login]"
=> #<HTML::Selector:0x007f8b5c0fb268 @source="form.login[action='/login']", @tag_name="form", @attributes=[["class", /(^| )login($| )/], ["action", /^\/login$/]], @pseudo=[], @negation=[], @alternates=[], @depends=nil>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top