MyFunnyDev

web, coding and beyond

javascript closures

without comments

For given content:

  <div id="test0">change me</div>
  <div id="test1">change me</div>
  <div id="test2">change me</div>
  <div id="test3">change me</div>

The code:

 for (var i = 0; i < 4; i++) {
   $('#test' + i).click(function(){
     $(this).html(i);
   });
}

will print (every time any of the elements is clicked):

4
4
4
4

Here is how this could be solved with the closure:

  for (var i = 0; i < 4; i++) {
    (function (i){
      $('#test' + i).click(function(){
        $(this).html(i);
      });
    })(i);
  }

Written by Michał Kuklis

June 25th, 2010 at 1:37 am

Posted in Uncategorized

managing gems with rvm named gem sets

without comments

RVM (Ruby Version Manager) is a tool which lets you install and switch between multiple ruby versions. RVM has also something called Named Gem Sets. This is pretty cool because you can create many different gem sets for different types of apps. Here is how to do it (I assume you have rvm already installed if not check this out):

  • go to your project folder and create new file called .rvmrc
  • open .rvmrc and add rvm ruby-version@your-gem-set for example rvm ruby-1.9.1@railsgems
  • close file and type: rvm gemset create your-gem-set (this will create new set)
  • type gem list (you should see empty list with no gems installed)

It’s almost as you would start with a fresh system :)

Written by Michał Kuklis

May 30th, 2010 at 11:20 pm

Posted in Uncategorized

Clojure syntax

without comments

Making a note for myself:

(1 2 3 4) // list
[1 2 3 fred] // vector
{:a 1 :b 2 :c 3}, {1 "a" 2 "b"} // map key/value
#{a b c} // set

Written by Michał Kuklis

February 2nd, 2010 at 12:36 am

Posted in Clojure

dfs in ruby :)

without comments

I wrote simple dfs in ruby:

def dfs(node, value, queue)
  return false if node.nil?	
  return true if node.data == value
 
  queue.push node.right unless node.right.nil?
  queue.push node.left unless node.left.nil?	
 
  dfs(queue.pop, value, queue)	
end

for node:

class Node
  attr_accessor :left, :right, :data
end

Written by Michał Kuklis

January 14th, 2010 at 12:38 am

Posted in Uncategorized

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

Academia vs. Business

without comments

Academia vs. Business

Written by Michał Kuklis

November 18th, 2009 at 12:05 pm

Posted in Uncategorized

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

changing function’s context in javascript

without comments

Today I learned how to change the context of a function in javascript. When we do:

function test() {
  this.foo = "Hello from " + this + " context.";
}
test();
alert(foo); // will show "Hello from [object Window] context.";

foo will belong to the global context. In other words global object foo will be created. We can change the context to some other object by doing:

var otherContext = {};
test.call(otherContext);
alert(otherContext.foo); // will show "Hello from [object Object] context."

In this case foo will belong to otherContext.

Written by Michał Kuklis

October 31st, 2009 at 12:24 am

Posted in Javascript

Gemcutter & Jeweler

without comments

More about gemcutter & jeweler can be found here.

Here are the steps how to publish patched gem cloned from github:

1. Append username to gem name in .gemspec or if you use jeweler open Rakefile and edit Jeweler::Tasks section save it and run:

rake gemspec

2. Build gem with:

gem build

3. Push new gem to gemcutter

gem push

Written by Michał Kuklis

October 17th, 2009 at 1:03 am

Posted in Uncategorized

Tagged with