Scalaでソート

List#sort List(1, 3, 2).sort(_ < _) // List(1, 2, 3) class List in scala - Scala Library Sorting.stableSort import scala.util.Sorting val ar = Array(1, 3, 2) Sorting.stableSort(ar) ar // Array(1, 2, 3) Sorting.stableSort(ar, (a: Int, b: In…

Scalaのテストを Eclipse+JUnit4でする

Eclipse 3.5.1 Galileo JUnit 4.5 (Eclipseに入っていたもの) Scala 2.7.7 プロジェクトを作成する プロジェクトのプロパティ > Javaのビルド・パス > ソース フォルダの追加 > 新規フォルダの作成 「test」フォルダを作成して、「終了」、「OK」 プロジェク…

Scala List[Option]#flatMap

val list = List(Some(1), None, Some(3)) list.flatMap { //=> List(1, 2, 3, 6) case Some(x) => List(x, x * 2) case None => List() } list.map { //=> List(List(1, 2), List(), List(3, 6)) case Some(x) => List(x, x * 2) case None => List() } lis…

Scala Actor

アクターの生成・開始 extends Actor import actors.Actor class MyActor extends Actor { def act: Unit = { ... } } val myActor = new MyActor myActor.start actorメソッド ただちに開始する import actors.Actor._ val myActor = actor { ... } メッセ…

Scala 正規表現

val p = "[0-9]+".r //=> scala.util.matching.Regex = [0-9]+ p.pattern //=> java.util.regex.Pattern = [0-9]+ find val p = "[0-9]+".r p.findAllIn("1 22") //=> scala.util.matching.Regex.MatchIterator = non-empty iterator p.findAllIn("1 22").to…

ScalaでHttpClient Get

HTTP GET Scala 2.7.7 Apache HttpComponents HttpClient 4.0 GET val page = "http://www.yahoo.co.jp/" val client = new DefaultHttpClient try { val uri = new URI(page) val get = new HttpGet(uri) val response: HttpResponse = client.execute(get)…

Apache HttpComponents HttpClient

Scala 2.7.6 Apache HttpComponents HttpClient 4.0 http://hc.apache.org/ Apache HttpComponents HttpCore 4.0.1 Apache Commons Logging 1.1.1 http://commons.apache.org/logging/ Apache Commons Codec 1.4 http://commons.apache.org/codec/ Apache Mi…

ScalaでSwing ファイルを開く・保存する

import scala.swing._ import java.io.{ File, IOException } import javax.swing.filechooser.FileNameExtensionFilter import org.apache.commons.lang.SystemUtils import org.apache.commons.io.FileUtils def getFileChooser = { val dir = if (current…

ScalaでSwing メニュー

MenuBar Menu MenuItem Action (abstract class) Action (object) CheckMenuItem ButtonGroup RadioMenuItem Menu Separator import import scala.swing._ import scala.swing.event.WindowClosing import java.awt.event.{ KeyEvent, InputEvent } import j…

順序付きのHash OrderedHash

$ script/console Loading development environment (Rails 2.3.2) >> RUBY_VERSION => "1.8.7" >> ActiveSupport::OrderedHash.new(:a => 1, :b => 2) => #<OrderedHash {}> >> ActiveSupport::OrderedHash.new([[:a, 1], [:b, 2]]) => #<OrderedHash {}> >> hash = ActiveSupport::OrderedH</orderedhash></orderedhash>…

@reminders.map(&:id)

app/controllers/reminders_controller.rb from authorNari's brushup - GitHub @reminders.map(&:id) 動作 ruby 1.8.7 irb ○ ruby 1.8.6でRails 2.3.2 script/console ○ ruby 1.8.6 irb TypeError: wrong argument type Symbol (expected Proc) 別の書き方…

Authlogic OpenID 1.0.3

Ruby ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32] Ruby on Rails 2.3.2 Authlogic 2.0.11 Authlogic OpenID 1.0.3

Authlogic 2.0.11

Authlogic Example App - Tutorial on how to create this app and easily setup Authlogic Ruby ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32] Ruby on Rails 2.3.2 Authlogic 2.0.11

Firefox3で不要なメニューを消すuserChrome.css

消したいメニューのidの見つけ方 Firefox 3.0.8 DOM Inspector 2.0.3 DOM Inspector のインストール https://addons.mozilla.org/ja/firefox/addon/6622 DOM Inspector の起動 ツール → DOM Inspector アドレス「chrome://browser/content/browser.xul」を入…

パッケージ管理ツール

aptにない時に Stow Jun Araki’s Blog » Stow によるパッケージ管理 Paco "make install"したソフトウェアを管理できる超便利ツール「Paco」 - RX-7乗りの適当な日々 CheckInstall お手軽パッケージ作成プログラムCheckInstallをインストール - jitsu102の日…

RubyGems の設定ファイル

オプションのデフォルトを設定するには、~/.gemrc か /etc/gemrc を作成する $ gem help environment (省略) Command line argument defaults and some RubyGems defaults can be set in ~/.gemrc file for individual users and a /etc/gemrc for all users…

coLinuxのインストール

coLinux 0.7.3 ディスクイメージ Debian 4.0 Etch OS Windows XP Home Edition SP3 ネットワーク ワイヤレス ネットワーク 参考 coLinuxのメモ - coLinuxのインストール(0.7.x) ネットワークの設定 coLinuxのメモ - coLinuxのインストール(0.7.x) TAP-Win32…

NetBeans 6.5.1用のパッチ

Rails 2.3.2 で Mongrel や WEBrick をサーバーとして使用するとその起動が NetBeans から認識できず、ブライザが起動しなかったり、すでに起動してあるブラウザの場合はページを更新できないという問題があります。 … 担当の Mononen が NetBeans 6.5.1 用…

Polymorphic Association

class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end class Article < ActiveRecord::Base has_many :comments, :as => :commentable end class Photo < ActiveRecord::Base has_many :comments, :as => :commentable #.…

Rails 2.3 : Nested Attributes / Nested Object Forms

accepts_nested_attributes_for / fields_for Nested attributes allow you to save attributes on associated records through the parent. automatic (and atomic) saving of a record together with its associated children, child-aware validations, a…

Rails 2.3 : Nested Transactions

transaction(:requires_new => true) If you want a transaction to be nested, you must explicitly add the :requires_new option. User.transaction do User.create(:username => 'Admin') User.transaction(:requires_new => true) do User.create(:user…

Rails 2.3 : Dynamic Scopes

scoped_by_ Order.scoped_by_customer_id(12) Order.scoped_by_customer_id(12).find(:all, :conditions => "status = 'open'") Order.scoped_by_customer_id(12).scoped_by_status("open") Ruby on Rails 2.3 Release Notes - 4.3 Dynamic Scopes

Rails 2.3 : Default Scopes

default_scope(options = {}) class Article < ActiveRecord::Base default_scope :order => 'created_at DESC' end Article.find(:all) #=> "SELECT * FROM `articles` ORDER BY created_at DESC" Article.with_exclusive_scope { find(:all) } #=> "SELECT…

Rails 2.3 : Batch Processing

find_in_batches / find_each Invoice.find_in_batches(:include => :invoice_lines) do |invoices| export.add_invoices(invoices) end User.find_each do |user| NewsLetter.weekly_deliver(user) end Ruby on Rails 2.3 Release Notes - 4.5 Batch Proces…

Rails 2.3 : Object#try

you can avoid nil-checking @person.try(:name) # @person ? @person.name : nil @people.try(:collect) {|p| p.name} Person.try(:find, 1) Product.find_by_price(4.95).try(:name) Ruby on Rails 2.3 Release Notes - 7.1 Object#try Class: Object Rail…

rescue_from

Rescue exceptions raised in controller actions. class ApplicationController < ActionController::Base rescue_from ActionController::RoutingError, :with => :route_not_found private def route_not_found(exception) render :text => exception.mes…

Rails 2.2 : Memoization

Memoization is a pattern of initializing a method once and then stashing its value away for repeat use. extend ActiveSupport::Memoizable def full_name "#{first_name} #{last_name}" end memoize :full_name Ruby on Rails 2.2 Release Notes - 9.…

send_file

ファイルのダウンロード send_file '/path/to.jpeg', :type => 'image/jpeg' Module: ActionController::Streaming 画像やファイルをダウンロードさせる | メモ帳

RSpec on Rails で spec_server is already running.

rake spec:server:startで、spec_serverが動いていないのに spec_server is already running.と言われたら、tmp/pids/spec_server.pid を削除する

RSpec 1.2.0

RSpecとRSpec on Railsが1.2.0にバージョンアップして、設定方法が変更された config.gem (for Rails) - rspec - GitHub Upgrades - rspec - GitHub spec_server + autospec == nearly pure BDD joy - rspec - GitHub テスト $ rake spec:server:start $ rak…