Posted by chad
on November 28, 2007
Highly recommended gem does exactly what it says. Here’s the code to turn an entire worksheet from an excel doc into a multi-dimensional array:
wb = Spreadsheet::ParseExcel.parse(filename)
rows = wb.worksheet(worksheet).map() { |r| r }.compact
grid = rows.map() { |r| r.map() { |c| c.to_s('latin1')}.compact rescue nil }
Just ‘gem install parseexcel’ (though i installed from source before i realized it was a pre-packaged gem).
Posted by chad
on November 15, 2007
I have a project that requires a standalone server for background processing. I could have used backgroundrb but it seems like overkill, and it has this comment on the documentation page, which is a year old, and which tells me the gem is not in active development.
WARNING: start/stop/restart is broken in 0.2.1, please use the server script directly until we have figured out the issue.
So, in development mode, rather than start a standalone server, I put this code in development.rb:
Thread.new do
Server.start()
end
For reasons i’ve yet to determine, because the thread is started in the initialization code, the file include logic doesn’t work properly. Now, this could be a problem with DRb, which is how the server talks with the rest of the app, or it could be a problem with the file include logic in rails. Either way, the first attempt to contact the server works, and on the second attempt to contact the server, we get this error:
A copy of Server has been removed from the module tree but is still active!
For now, I’m just running the server using script/runner in a separate process, and it works fine.
Posted by chad
on November 08, 2007
For one of my projects, we have a list of around 100 domains in a ‘blacklist’. Because of the way the blacklist works, we need to allow non-programmers to enter sites using simple wildcards. Then I convert that into a regular expression:
Regexp.new("^#{regexp.gsub(".","\\.").gsub("*",".*")}$",Regexp::IGNORECASE)
When spidering sites, if a site we find is in this list, we exclude it. I was building a regular expression of all these sites and ‘unioning’ them all together into one mega-regular expression. Then, to see if a domain is on the ‘blacklist’, i just do:
domain =~ my_mega_regex
However, what I found is that a regular expression longer than about 159 separate predicates causes ruby to segfault. This happens on ruby 1.8.4 and 1.8.5. Here’s the simplist code I can repro this with:
r=Regexp.new(/^$/);1.upto(1000) { |i| r= Regexp.union(r,Regexp.new("^#{i}$"));puts i unless "foo" =~ r }
Posted by chad
on November 07, 2007
Don’t name your variable @url, then use any of the url helpers such as url_for, link_to_remote, etc. If you do, you’ll get this error:
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.rewrite
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:522:in `url_for'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/helpers/url_helper.rb:27:in `send'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/helpers/url_helper.rb:27:in `url_for'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/helpers/prototype_helper.rb:242:in `remote_function'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/helpers/prototype_helper.rb:133:in `link_to_remote'
#{RAILS_ROOT}/app/views/seo/_listtable.rhtml:103:in `_run_rhtml_47app47views47seo47_listtable46rhtml'
#{RAILS_ROOT}/app/views/seo/list.rhtml:3:in `_run_rhtml_47app47views47seo47list46rhtml'
/usr/bin/rdebug-ide:16:in `load'
/usr/bin/rdebug-ide:16
-e:4:in `load'
-e:4
Posted by chad
on November 06, 2007
I had lunch with a friend today, and he convinced me that i should start blogging again. So, here it is, the re-launch of rubyrescue.com/blog.