Posts for: #Snippets

How to install ruby-debug on Ruby 1.9

One of the major problem which prevent the migration to Ruby 1.9 is the lack of the porting of ruby-debug.

Here the steps I followed to made it working on my MacBook with Multiruby:


sudo gem install rake-compiler ruby_core_source --no-rdoc --no-ri
sudo gem install ruby-debug19 --no-rdoc --no-ri

I hope it works for you too!

[]

Rails: Unobtrusive and i18n Javascript confirm

How many times do you heard you should separate content from behavior? Never? Hmmm don’t try to cheat me.. So, why do you still use :confirm option for link_to helper?

Here a little snippet to archive our goal, and as extra bonus, you get it i18n:


<script type="text/javascript">
window._authenticity_token = "";
</script>

t(:’asset.destroy’),
:class => “delete”,
:’data-confirm’ => t(:’asset.destroy_confirm’) %>


$(document).ready(function(){
$(".delete").bind("click", function() {
if(window.confirm($(this).attr("data-confirm"))) {
$.ajax({
url: $(element).attr("href"),
data: {
_method: "delete",
authenticity_token: window._authenticity_token
},
type: 'post',
dataType: 'script'
});
}
return false;
});
});

[]

Hash deep search

I’m back and I wanna share those simple snippets with you:

~

class Hash def deep_has_key?(key) self.has_key?(key) || any? {|k, v| v.deep_has_key?(key) if v.is_a? Hash} end alias :deep_include? :deep_has_key? alias :deep_key? :deep_has_key? alias :deep_member? :deep_has_key?

def deep_has_value?(value) self.has_value?(value) || any? {|k,v| v.deep_has_value?(value) if v.is_a? Hash} end alias :deep_value? :deep_has_value? end

Example:
{:a => {:c => 'c'}, :b =>{:d => {:e => 'e'}}}.deep_has_key?(:e) # => true {:a => {:c => 'c'}, :b =>{:d => {:e => 'e'}}}.deep_has_key?(:z) # => false

{:a => {:c => ‘c’}, :b =>{:d => {:e => ’e’}}}.deep_has_value?(’e’) # => true {:a => {:c => ‘c’}, :b =>{:d => {:e => ’e’}}}.deep_has_value?(‘z’) # => false

[]

Rails: Single File App

I took inspiration from the Pratik Naik post, and realized a more simplistic version of its Rails single file app. My implementation has only Rails as unique dependency.
require 'rubygems' require 'action_controller' require 'webrick' require 'webrick_server'

class HelloWorldController ‘Hello World!’ end end

ActionController::Routing::Routes.draw do |map| map.root :controller => “hello_world” end

DispatchServlet.dispatch :port => 3000, :server_root => File.dirname(FILE)

Update 2008-06-04: I just wrote another version which also uses ActiveRecord and a template.
require 'rubygems' require 'activerecord' require 'action_controller' require 'webrick' require 'webrick_server'

ActiveRecord::Base.establish_connection( :adapter => ‘sqlite3’, :database => ’tiny_rails.sqlite3’, :timeout => 5000)

[]

Ruby: Class Methods Proxy

Did you ever used class methods inside your instance methods? If yes, you probably noticed how frustrating can be to use the self.class.my_class_method syntax. A solution could be to create a private method which encapsulates the class one.
class Repository def self.path @@path end

def print_path puts path end

private def path self.class.path end end
In the above example, #print_path can print the @@code value, without worrying about it’s a class value, because we have wrapped it.

[]