Archive for the ‘Uncategorized’ Category
javascript closures
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); }
managing gems with rvm named gem sets
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
dfs in ruby :)
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
Academia vs. Business

Gemcutter & Jeweler
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
BDD with Cucumber by Ben Mabey
auto escaping html in Rails to protect from XSS
Tonight I found a plugin to auto escape html in order to protect from XSS attacks. I’m not sure why rails doesn’t do it out of the box (you have to use h() helper). Anyway the plugin is called xss_terminate and it can be found here. The cool thing about it is that now you can forget about h()
.
Cron in Ruby
key-value store

daemon_controller + Thinking Sphinx
I’ve created simple rails initiator in order to start Sphinx through daemon_controller based on the Thinking Sphinx configuration. I hope it will help somebody.
require 'daemon_controller' def before_start if not ThinkingSphinx.define_indexes? config = ThinkingSphinx::Configuration.instance cmd = "#{config.bin_path}#{config.indexer_binary_name} --config \"#{config.config_file}\" --all" cmd << " --rotate" if ThinkingSphinx.sphinx_running? system cmd end end if defined?(ThinkingSphinx) if not ThinkingSphinx.sphinx_running? conf_instance = ThinkingSphinx::Configuration.instance @controller = DaemonController.new( :identifier => 'Sphinx search server', :start_command => "#{conf_instance.bin_path}#{conf_instance.searchd_binary_name} --pidfile --config \"#{conf_instance.config_file}\"", :before_start => method(:before_start), :ping_command => lambda { TCPSocket.new(conf_instance.configuration.searchd.address, conf_instance.configuration.searchd.port) }, :pid_file => conf_instance.configuration.searchd.pid_file, :log_file => conf_instance.configuration.searchd.log) @controller.start end end