Posts Tagged ‘Ruby’

GC and ObjectSpace

Friday, August 24th, 2007

I saw this at the Rails Edge conference and thought it was cool; aside from its really expensive to implement in JRuby.

1
2
3
4
5
6
7
8
9
10
>> GC.disable
=> false
>> instances = 0
=> 0
>>  ObjectSpace.each_object(Artist) { |a| instances += 1 }
=> 0
>> Artist.find(:all, :limit => 5)
=> [#<Artist:0x327d348 @attributes={....
>>  ObjectSpace.each_object(Artist) { |a| instances += 1 }
=> 5

Markup, CSS and Helper cataloging (Part deux)

Monday, June 25th, 2007

I should be using ERB instead of eval. But because you can’t use <% inside a string block in your view (that I can figure out; <<EOF didn’t seem to be working in a template). And if you don’t want to specify the code string in your controller you can use [% ... %]. Here is the source I am using for our code helper now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  def erb_code(code) 
    ERB.new(code).result(binding)    
  end
  
  def render_code(code, options = {}) 
    %{ <pre class="helper">\n<code class="ruby">\n#{h(code)}\n</code>\n</pre> }
  end
  
  def render_markup(html)
    doc = REXML::Document.new(html)
    markup = ""
    doc.write(markup, 2)
    
    %{ <pre class="markup">\n<code class="html">\n#{h(markup)}\n</code>\n</pre>\n }    
  end
  
  def render_eval(html)
    %{ <div class="eval">#{html}</div> }
  end
    
  def code_helper(code)
    code.gsub!(/\[%/, "<%").gsub!(/%\]/, "%>")    
    html = erb_code(code)
    render_code(code) + render_markup(html) + render_eval(html)
  end

Then you can do things like:


  <%= code_helper %{ [%= rating_field(:rating, :effectiveness) %] } %>

and you could also dump it in a helper:

1
2
3
4
5
6
7
8
9
def code_rating_form_field 
  <<-EOF
   <% form_for :rating, @rating2 do |f| %>
     <%= f.rating_field(:effectiveness) %>
   <% end %>
  EOF
end

  // And in your view: <%= code_helper(code_rating_form_field) %>

Network Facade API

Friday, June 8th, 2007

I was looking at the NetworkFacade api, (check it out at network-facade.rubyforge.org) and noticed:

1
2
3
  # Or declate the Foo class and set an uri
  class Foo < NetworkFacade::Client 'nf://localhost:5042'
  end

where the uri is defined with the Foo declaration. Foo descends from the class returned by the Client class method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module NetworkFacade

    def self.Client(uri = nil)
        TCP::Client.uri = uri
        TCP::Client
    end

    class Client < TCP::Client
    end


    class Server < TCP::Server
    end

end

That is cool.

Rake test task for a custom FileList

Friday, April 6th, 2007

I work on a very large rails project, and rake test has become unruly mostly due to the size of our codebase. Anyway, I wrote this task to run only the tests in the products/namespace I work on.

rake test:products PRODUCTS=product1,product2

Runs all unit tests in test/**/product1/*test*.rb, test/**/product2/*test*.rb

The task:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
namespace :test do
  task :products do      
    raise "Usage: rake test:products PRODUCTS=product1,product2" if ENV['PRODUCTS'].blank?
    
    products = ENV['PRODUCTS'].split(/,/)
    files = []
    
    products.each do |product|
      dir = "#{RAILS_ROOT}/test/**/#{product}"
      files += FileList["#{dir}/*test*.rb"]
    end    
    
    test_task = Rake::TestTask.new("test_products") do |t|
      t.libs << "test"
      t.test_files = files
      t.verbose = true
    end
    
    task("test_products").execute           
  end
end

There is probably an easier way, but this works for me. Also I wouldn’t mind someone patching rake to accept opts style arguments, like –opt=blah, or -o blah. The ENV stuff seems really not so friendly.