MyFunnyDev

web, coding and beyond

Archive for the ‘Ruby’ Category

class variables, class instance variables and instance variables in ruby

with one comment

This was covered multiple times already. I’ve created this little snippet to remember the difference between different types of variables in ruby:

class A
  @@foo = "class variable of the class A"
  @foo = "class instance variable of the class A"
 
  def instance_method
   @foo = "instance variable of the class A"
  end
 
  def self.class_method1
    # class variables are visible to and shared by the instance and class methods
    @@foo
  end
 
  def self.class_method2
    # class instance variables are visible to and shared by the class methods
    @foo
  end	
end
 
p A.new.instance_method # instance variable of the class A
p A.class_method1 # class variable of the class A
p A.class_method2 # class instance variable of the class A
 
class B < A
  @@foo = "class variable of the class B"
  @foo = "class instance variable of the class B"
end
 
p B.class_method1 # class variable in B
# class variable in A is overwritten by one in B !!!
p A.class_method1 # class variable in B
 
p B.class_method2 # class instance variable of the class B
# class instance variable in A is NOT overwritten by one in B !!!
p A.class_method2 # class instance variable of the class A

Written by Michał Kuklis

January 13th, 2010 at 1:03 am

Posted in Ruby

anemone with hpricot

with one comment

Anemone is a pretty cool DSL used for web crawling. I used it with Hpricot to get a feeling for what’s possible. Below is a simple example which crawls and scrappes data from a popular polish real estate website otodom:

require 'rubygems'
require 'sanitize'
require 'anemone'
require 'open-uri'
require 'hpricot'
 
#otodom.pl
Anemone.crawl("http://otodom.pl/index.php?mod=search&act=searchResults&qid=46911208", 
{:storage => Anemone::Storage.PStore("crawl1.pstore")}) do | anemone |
 
  # filter out useless pages
  anemone.focus_crawl do |page|
   page.links.delete_if do |x|
    (x.to_s =~ /mod=search&act=searchResults&qid=/).nil? and
    (x.to_s =~ /[a-zA-Z]+-id[0-9]*\.html$/).nil?
   end
  end
 
  # process details pages
  anemone.on_pages_like(/[a-zA-Z]+-id[0-9]*\.html$/) do | page |
     doc = Hpricot(page.doc)
     price =  doc.at("//strong[@id='offerPrice']")
     location = doc.at("//dl[@class='stripeMe'] > dd")
     desc = doc.at("//div[@id='offerDesc'] > p")
     offer_no = doc.at("//div[@id='offerFoot'] p[@class='toLeft']/span/strong")
     created_at = doc.at("//div[@id='offerFoot'] p[@class='toRight']/span/strong")
     photos = doc.search("//div[@id='imageList']/p/a")
  end
end

Written by Michał Kuklis

January 11th, 2010 at 12:06 am

Posted in Ruby

blocks, procs and lambdas in ruby

without comments

Nice post about block, procs and lambdas in ruby by Robert Sosinski.

Written by Michał Kuklis

November 17th, 2009 at 11:12 pm

Posted in Ruby

ruby maxins in rails plugins

without comments

Very often when looking at the code in rails plugins you can run into this:

module Taggable 
  def self.included(base)
    base.extend(ClassMethods)
  end
  module module ClassMethods
    #methods here
  end
end

This is a part of a bigger pattern which is shown below:

module ModuleA
  def self.included(base)
    # add class methods from ModuleB
    base.extend(ModuleB)
  end
end
 
module ModuleB
  def act_as_hello
    p "hello from module B"
  end
end
 
class ClassC
 #class body here
end
 
# include moduleA in classC
ClassC.send(:include, ModuleA)
 
class ClassD < ClassC
  act_as_hello
end
 
classD = ClassD.new

The pattern is used often when developing plugins with ActiveRecord. What we gain by inheriting from ClassC (class ClassD < ClassC) are instance methods from ModuleA. This is done by:

ClassC.send(:include, ModuleA)

Moreover since ModuleA is included in ClassC, ModuleA’s initializer def self.included(base) will be invoked at the time ModuleA is mixed with ClassC. The invocation will call base.extend(ModuleB). In this case base represents ClassC which will be extended by adding class methods from ModuleB. The ModuleA’s init method is shown again below:

def self.included(base)
  # add class methods from ModuleB to ClassC
  base.extend(ModuleB)
end

At the end our ClassD has now access to all class methods defined in ModuleB. act_as_hello will be called during ClassD initialization:

class ClassD < ClassC
  act_as_hello
end

Written by Michał Kuklis

July 20th, 2009 at 9:16 pm

Posted in Ruby, ruby on rails

Tagged with ,

Class and Instance Variables In Ruby

without comments

Written by Michał Kuklis

June 13th, 2009 at 9:27 pm

Posted in Ruby

few more ruby links

without comments

Written by Michał Kuklis

May 18th, 2009 at 7:24 pm

Posted in Ruby

Tagged with

going back to ruby again…

without comments

Written by Michał Kuklis

April 4th, 2009 at 11:05 am

Posted in Ruby

Tagged with

stage deployment with capistrano and passenger

without comments

I’m still looking around for a stage deployment setup with Capistrano.
Here are few ideas/links I found useful.

Update:
It turned out this is pretty simple with the capistrano-ext gem:

http://weblog.jamisbuck.org/2007/7/23/capistrano-multistage

set :default_stage, "development"
set :stages, %w(production testing development)
require 'capistrano/ext/multistage'

Written by Michał Kuklis

March 14th, 2009 at 3:20 pm

Posted in Ruby

Tagged with ,

heroku

without comments

http://heroku.com/ – awesome work…

Written by Michał Kuklis

February 19th, 2008 at 9:25 pm

Posted in Ruby, ruby on rails

Rails 1.1.2 + Apache2 + FCGI on Windows XP

without comments

Setting up Rails on Windows can be pain in the butt. I spent couple hours reading posts and changing Apache and Rails configuration and finally I found WARR.

Just follow directions from this post and don’t forget to add:

LoadModule rewrite_module modules/mod_rewrite.so

to your httpd.conf file.
I had some problems after all but I created new project and everything works great!

ROR

Written by Michał Kuklis

April 11th, 2006 at 11:49 pm

Posted in Ruby