Skip to main content

Posts

Showing posts with the label Ruby

Screen Scraping in Ruby with Watir and Nokogiri

I was given an interesting challenge to scrape some data from a specific site.  Not to write a completed, packaged solution, but rather just to scrape the data.  The rub being, the site uses Javascript paging, so one couldn't simply use something like Mechanize.  While a self-contained product would require inclusion of V8 (as the Javascript would need to be run and evaluated), to just scrape the data allows making use of whatever is easy and available.  Enter Watir . Watir allows "mechanized/automated" browser control.  Essentially, we can script a browser to go to pages, click links, fill out forms, and what have you.  It's mainstay is in testing, but it's also pretty damned handy in cases where we need some Javascript on a page processed... like in this case.  Keep in mind though, it is literally automating a browser, so you'll see your browser open and navigate to pages, etc. when the script runs.  But, there is also a headless browser opti...

Passing Functions and Lambdas into Functions with Ruby

Ruby's New Style of Lambda Functions f = ->( m ) { p m } f.call( 1 ) #=> 1 Which of course means the same thing as: f = lambda { |n| p n } f.call(1) #=> 1 Ruby Proc Objects p = Proc.new { |n| p n + 2 } p.call(2) #=> 4 Using a Function as a Closure in Ruby def domo( k ) ->(m) { p m + k } end z = domo( 5 ) z.call( 5 ) #=> 10 Function :domo takes a single parameter. Within :domo , we create a lambda that takes a single parameter, and adds that parameter to the value :domo takes in as its parameter. Then, we assign z to be the result of the lambda in :domo with its 'k' parameter loaded with 5. When z is called, we pass (another) 5 to it. This parameter loads the lambdas n parameter. The lambda executes, essentially adding n (5) + k (5) and yielding the result of 10. The thing about closures such as this is, we can load the initial value of the lambda to be whatever we want it to be when assigning the function :domo '...

Creating Incrementing Variables in Code with Ruby

I had a project I was working on that required me to assign values to incremental variables from an array.  A quick solution to the problem, using very little meta-coding is below in its most basic form.  One class was my data or value class.  It held the incremental variables I wanted to assign to.  Here It's called, "Foo".  The class that is using this data is "Bar".  Just for spice, I randomized the array values to assign to the data variables. class Foo attr_accessor :i0, :i1, :i2, :i3 initialize @i0 = 0; @i1 = 0; @i2 = 0; @i3 = 0 end end class Bar attr_accessor :f def initialize @f = Foo.new @image_sort = %w{ 1 2 3 4 }.shuffle end def setup @image_sort.each_with_index do | i, idx | @f.instance_variable_set( :"@i#{idx}", i ) end puts @f.i0 puts @f.i1 puts @f.i2 puts @f.i3 end end g = Bar.new g.setup