Skip to content

Instantly share code, notes, and snippets.

@wtnabe
wtnabe / array_to_orderedhash.rb
Created April 6, 2011 02:24
A example for generating OrderedHash from Array. Useful for Rails FormHelper, isn't it ?
#
# Convert Array to OrderedHash
#
# [param] Array arr
# [return] OrderedHash
#
def to_oh( arr )
ActiveSupport::OrderedHash[*arr.zip((0...arr.size).to_a).flatten]
end
@wtnabe
wtnabe / delete_all_mails.rb
Created May 6, 2011 05:50
delete your all mails from pop server
#! /usr/bin/env ruby
require 'net/pop'
require 'nkf'
limit = 700 # nantonaku :-)
num = 1
pop = Net::POP3.new( SERVER, PORT )
pop.start( USER, PASS ) { |c|
puts 'connecting ...'
@wtnabe
wtnabe / .irbrc
Created May 10, 2011 16:04
A model dumper for rails console ( Rails 3 or later ) and .irbrc for rails env
# -*- mode: ruby; coding: utf-8 -*-
if defined? Rails
railsrc = __FILE__ + '.rails'
load railsrc if File.exist? railsrc
envrc = File.join( Rails.root, '.irbrc' )
load envrc if File.exist? envrc
end
@wtnabe
wtnabe / gist:996460
Created May 28, 2011 00:40
ActiveModel required fields
@model.class.validators.select { |e|
e.is_a? ActiveModel::Validations::PresenceValidator
}.map { |e| e.attributes }.flatten
# => ['foo', 'bar', 'baz']
@wtnabe
wtnabe / gist:1028633
Created June 16, 2011 03:39
A trap of OSX 10.6's pre-installed gem command
$ /usr/bin/gem install sass
WARNING: Installing to ~/.gem since /Library/Ruby/Gems/1.8 and
/usr/bin aren't both writable.
WARNING: You don't have /Users/USER/.gem/ruby/1.8/bin in your PATH,
gem executables will not run.
Successfully installed sass-3.1.2
1 gem installed
Installing ri documentation for sass-3.1.2...
Installing RDoc documentation for sass-3.1.2...
@wtnabe
wtnabe / clear_mails.rake
Created June 16, 2011 10:19
'mail-clear' task for rails development
namespace :tmp do
if ( File.exist?('tmp/mails') )
namespace :mails do
desc "Clears all files in tmp/mails"
task :clear do
FileUtils.rm_rf(Dir['tmp/mails/[^.]*'])
end
end
end
end
@wtnabe
wtnabe / gist:1033606
Created June 18, 2011 23:49
latest ( 3.0.9 ) ActiveSupport usage with Bundler
gem 'activesupport', :require => 'active_support'
gem 'i18n'
@wtnabe
wtnabe / to_php.rb
Created July 8, 2011 05:07
Translate from your Ruby object to PHP literal
class Object
def to_php
self.inspect.gsub( /[\[{]/, 'array(' ).gsub( /[}\]]/, ')' )
end
end
@wtnabe
wtnabe / example-spec.js
Created July 22, 2011 08:12
yet another jasmine example
describe('Example', function() {
it('give a block to expect', function() {
expect(function() {
return 'foo';
}()).toEqual('foo'); // <- function should end with ()
});
});
@wtnabe
wtnabe / jasmine.rake
Created July 22, 2011 12:03
rake task that install jasmine-jquery helper for Rails and pure jasmine inited env
# -*- mode: ruby -*-
#
# put this code #{Rails.root}/lib/tasks or jasmine Rakefile
#
namespace :jasmine do
desc "Install jasmine-jquery"
task "install-jquery" do
require "open-uri"