[#46647] RubyでWindowsの常駐サービスプログラムを作る方法 — kouichi_someya@...

はじめまして。

12 messages 2009/12/16
[#46649] Re: RubyでWindowsの常駐サービスプログラムを作る方法 — "U.Nakamura" <usa@...> 2009/12/17

こんにちは、なかむら(う)です。

[ruby-list:46584] Re: ActiveRecordとArrayのchoiceメソッドでエラー(1.8.7)

From: OZAWA Sakuro <sakuro@2238club.org>
Date: 2009-12-01 04:07:45 UTC
List: ruby-list #46584
さくです。

2009年12月1日12:47 Gen Ito <it0gn.25@gmail.com>:

> Ruby 1.8.7で、Array:choiceを使うため、逆引きRubyに載っている
> 方法を使っているのですが、

1.8.7 には Array#choice は定義されているような。

$ ruby -ve 'p 20.times.map { [1,2,3].choice }'
ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin9]
[1, 2, 3, 2, 2, 1, 2, 3, 3, 3, 1, 3, 2, 2, 2, 3, 3, 1, 2, 2]

> このchoiceメソッドを、ActiveRecordと一緒に使った場合に
> エラーになってしまいます(コードは後述)。

> in `rand': wrong number of arguments (1 for 0) (ArgumentError)

active_recordがactive_supportを使用していますが、active_supportで
choiceと同等の動作をする Array#rand (引数なし) が定義されています。
active_support/core_ext/array/random_access.rb

レシーバを略すと self(=Array)#rand が使われてしまうので、
Array#choiceを自分で定義する場合は Kernel.rand を使う必要があります。

-- 
OZAWA Sakuro

"The future will be better tomorrow." --Dan Quayle

In This Thread