√477
2012-08-23
_ [rails] Testing behavior of ApplicationController with Rails3 and test-unit
There is a great article for RSpec. Here is how to do it with test-unit.
1. Create test/functional/application_controller_test.rb
2. Define example controller
class FooController < ApplicationController
def exmaple
# ...
end
end
3. Define test case for this
class FooControllerTest < ActionController::TestCase
4. Define routes in setup (otherwise you will get 'No route matches')
def setup
Rails.applicaiton.routes.draw do
get "/foo" => "foo#example"
end
end
5. Reload routes in teardown (otherwise all your other tests will fail)
def teardown
Rails.application.reload_routes!
end
6. Write test
test "foo" do
# ...
get :exmaple
end
7. That's all!
end
2011-11-13
_ [ruby] Coding projects introduced in RubyConf AR/UY Lightning Talks
Here is a list of interesting projects introduced in Lightning Talks on RubyConf Argentina and RubyConf Uruguay 2011. Let me know if something is missing.
From RubyConf Argentina:
- AudioVerb
- "Learn English with hundreds of thousands of sentences with audio, transcriptions and translations."
- coco-debugger
- Ruby debugger GUI for Mac
- MacRuby
- "MacRuby is an implementation of Ruby 1.9 directly on top of Mac OS X core technologies"
- Cuba
- Chef Surfing
- "Chef Surfing connects local chefs to hungry customers."
- dom-perignon
- Example of jquery, knockout and angular
- http://dom-perignon.heroku.com/
- crystal
- "Crystal is a programming language that compiles to native code. It has a syntax strongly inspired by Ruby"
- Cacique
- "Cacique is a GPL automation tool easy to use, powerful, collaborative and free!"
- OpenData
From RubyConf Uruguay:
- Galia
- Gesture controlled gallery (powered by Kinect)
- https://github.com/outbox/galia
- OpenData
- http://www.google.com/publicdata/home , etc.
- http://desarrollandoamerica.org/ 03 Dec Open Data Day
- Jasmine
- Storm
- "Distributed and fault-tolerant realtime computation: stream processing, continuous computation, distributed RPC, and more"
- http://mcdlr.com/resp/
- Demo of responsive web design (only HTML & CSS!)
- Resize your window and it detects the viewport size.
- RConfCronogram-
- Android app for RubyConf (screenshot)
Both
- Adhearsion
- "Adhearsion is a new way to write voice-enabled applications."
- horaextra
- From @eto:
2011-10-16
_ [javascript][mac][ruby] How to access original "window" from dotjs
Recently I installed dotjs. dotjs executes JavaScript files in ~/.js every time you open a web page. It's like greasemonkey etc., but it is as easy as putting a .js file in your $HOME.
Then I noticed that I cannot call a function defined globally in the original HTML from ~/.js/(domain-name).js. This is because dotjs is built with Chrome extension and Chrome executes the script as "Content script". Even you declare a function foo() in original page, window.foo evaluates to undefined in dotjs files.
I googled and found two solutions in Japanese tech site:
Solution 1
Rewrite location.href with javascript: scheme.
location.href = "javascript:some_func(123);";
Solution 2
Create a scirpt tag and append it to the document.
$("head").append("<script type='text/javascript'>some_func(123);</script>");
Unfortunately, Solution 2 did not work in my environment (Mac 10.6.8 + Chrome 14.0.835.202).
2011-05-26
_ [mac][ruby] homebrew prints a lot of "WARNING: Invalid .gemspec format in ..."
If you are using Ruby which is not the pre-installed one, homebrew may prints a lot of warnings:
WARNING: Invalid .gemspec format in ...
This is because /usr/local/bin/brew starts with:
#!/usr/bin/ruby
This means that homebrew uses the pre-installed Ruby and RubyGems, which may very old and not compatible to gemspecs generated by recent RubyGems.
Solution A
Upgrade system rubygems to newer version.
$ /usr/bin/gem -v 1.1.1 $ wget http://rubyforge.org/frs/download.php/70695/rubygems-update-1.3.7.gem $ sudo /usr/bin/gem i rubygems-update-1.3.7 --local $ sudo /usr/bin/update_rubygems RubyGems 1.3.7 installed ...
(I could not install RubyGems 1.8.4 due to minitest, which it requires.
/Users/yhara % sudo /usr/bin/gem i ~/DOWN/minitest-2.1.0.gem --local
ERROR: Error installing /Users/yhara/DOWN/minitest-2.1.0.gem:
minitest requires minitest (>= 2.0.2)
what's happening!?)
Solution B
Edit the first line of the brew command like this:
#!/usr/bin/env ruby
Then brew uses the Ruby and RubyGems you are using.
Note that homebrew does not support Ruby 1.9 officially; homebrew 0.8 looks working fine with Ruby 1.9.2 on my machine, but maybe you should use Ruby 1.8.7.
2011-04-21
_ [sinatra] How to reload modular-style Sinatra apps with shotgun
As the FAQ sais, you need to write config.ru to enable auto-reloading with shogtun.
# config.ru require 'app.rb' run MyApp
$ shotgun config.ru -p 12345
Or you can use sinatra-reloader, which is a litter faster than shotgun (version 0.5.0 is working fine with Sinatra 1.2.3).
class Server < Sinatra::Base
configure :development do
require 'sinatra/reloader'
register Sinatra::Reloader
#also_reload "lib/**/*.rb"
end
...
2011-01-25
_ [rails] Colorizing Test::Unit with turn
I noticed that the redgreen gem does not work with Ruby 1.9.2 (and Rails 3.0.3).
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- test/unit/ui/console/testrunner (LoadError)
RSpec users don't need redgreen to colorize outputs, but I am trying Test::Unit this time.
Then I found turn via this ruby-forum thread:
Install
Edit Gemfile
group :test do gem 'turn' end
and
$ bundle install
Edit test/test_helper.rb
require 'turn'
Run
$ rake test:units
Output:

With autotest
If you require turn in test_helper.rb, the output of autotest is also colorized.
$ gem i ZenTest autotest-rails
$ autotest
loading autotest/rails
/Users/yhara/.rvm/rubies/ruby-1.9.2-p136/bin/ruby -I.:lib:test -rubygems -e "%w[test/unit test/unit/helpers/posts_helper_test.rb test/unit/translation_request_test.rb test/unit/helpers/translation_requests_helper_test.rb test/functional/posts_controller_test.rb test/unit/helpers/sessions_helper_test.rb test/functional/admin/translations_controller_test.rb test/functional/translation_requests_controller_test.rb test/unit/post_test.rb test/functional/admin/users_controller_test.rb test/functional/admin/posts_controller_test.rb test/unit/blade_test.rb test/unit/user_test.rb test/unit/translation_test.rb test/functional/translations_controller_test.rb test/functional/sessions_controller_test.rb test/unit/helpers/translations_helper_test.rb].each { |f| require f }" | unit_diff -u
...
2011-01-21
_ [rails] How to use yield in Rails helpers
Example view:
<%= foo(@posts) do |post| %> <%= post.title %> <% end %>
Wrong definition of foo:
def foo(posts)
"<div>" +
posts.map{|post| yield post} +
"</div>"
end
I like this funcitonal-style, but it does not work.
Correct definition of foo:
def foo(posts)
concat "<div>"
posts.each{|post| yield post}
concat "</div>"
end
- yield in helpers has a side effect!
- never use the return value of yield, it is not what you expect*1
- use TextHelper#concat to render static text
*1 the content of buffer is returned.
2011-01-19
_ [ruby] Downloading non-UTF8 pages with open-uri
In Ruby 1.9, you can specify the encoding of the HTML to open-uri explicitly.
irb> require 'open-uri' irb> url = "http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-dev/43008" irb> html = open(url, "r:euc-jp").read
Then encode it to UTF-8:
irb> html.encode("utf-8")
Note that this conversion may raise Encoding::UndefinedConversionError, for example when the page contains invalid byte sequences.
String#encode takes options to surpless this exception.
irb> html.encode("utf-8", :invalid => :replace, :undef => :replace)
In Rails ...
Within a Rails3 app, this code may raise UndefiendConversionError:
html = open(url, "r:euc-jp").read
Rails3 sets Encoding.default_internal to UTF-8, so any string read from outer world is automatically converted to UTF-8 (and may fail if it contains broken chars).
You can skip the conversion by specifying "binary" (or "ascii-8bit") to open-uri, and then convert it manually.
html = open(url, "r:binary").read.encode("utf-8", "euc-jp", :invalid => :replace, :undef => :replace)
2011-01-13
_ [ruby] Using git-hub gem instead of github gem
The github gem provides the github command, which is very useful to create a repository on github. But it does not support Ruby 1.9 yet :-(
You can use the git-hub gem instead.
Install
$ gem install git-hub
Setup
If you are using the github gem, you may have done this already:
$ git config --global github.user YOUR_USER $ git config --global github.token API_TOKEN
See https://github.com/account for your api token.
Usage
Create a github repository from a local git repo: (corresponds to `gh create-from-local')
$ hub create
Fork the github repository:
$ hub fork
The hub command works as a wrapper of git. If you set an alias like
$ alias git=hub
then you can do the above with `git create' and `git fork'.
2010-12-25
_ [biwascheme] Released 0.5.5.1
Released BiwaScheme 0.5.5.1.
BiwaScheme is a Scheme interpreter written in pure JavaScript. I call it NoJS (Not only JavaScript) :-)
This release includes support for R6RS Records and several bug fixes.