ライブラリ
クラス
- BasicObject (84)
- Class (36)
- Data (18)
- ERB (24)
- File (12)
- Object (144)
- Proc (14)
- Struct (4)
- Thread (24)
-
Thread
:: Backtrace :: Location (48) - Tracer (24)
-
WIN32OLE
_ EVENT (12) -
WIN32OLE
_ TYPE (12)
モジュール
- Enumerable (96)
-
Fiddle
:: Importer (12) - Forwardable (48)
- JSON (12)
-
JSON
:: Generator :: GeneratorMethods :: Object (12) - Kernel (12)
- ObjectSpace (36)
キーワード
- ! (12)
- != (12)
-
1
. 6 . 8から1 . 8 . 0への変更点(まとめ) (12) - == (12)
- Application (1)
- BasicObject (12)
- ConditionVariable (12)
- DelegateClass (12)
- Location (12)
- Marshal フォーマット (12)
- MonitorMixin (12)
-
NEWS for Ruby 2
. 5 . 0 (8) -
NEWS for Ruby 3
. 0 . 0 (5) -
NEWS for Ruby 3
. 1 . 0 (4) - Observable (12)
- RSA (12)
- Ruby用語集 (12)
- [] (7)
-
_ dump (12) -
absolute
_ path (12) - allocate (12)
-
base
_ label (12) -
cgi
/ session (12) - clone (12)
-
create
_ id (12) -
def
_ class (12) -
def
_ delegator (12) -
def
_ instance _ delegator (12) -
default
_ event _ sources (12) - define (6)
-
define
_ finalizer (24) - delegate (12)
-
drb
/ extservm (12) -
drb
/ gw (12) - dup (12)
- fork (12)
- handler= (12)
-
initialize
_ copy (12) - inspect (24)
-
instance
_ delegate (12) -
instance
_ eval (24) -
instance
_ exec (12) -
instance
_ variable _ defined? (12) -
instance
_ variable _ get (12) - logger (12)
-
marshal
_ dump (12) - max (48)
-
method
_ missing (12) - min (48)
- new (59)
- path (12)
- rdoc (12)
-
rdoc
/ generator / json _ index (12) -
rdoc
/ parser (12) -
rexml
/ parsers / streamparser (12) -
ruby 1
. 6 feature (12) -
ruby 1
. 8 . 2 feature (12) -
ruby 1
. 8 . 3 feature (12) -
ruby 1
. 8 . 4 feature (12) -
ruby 1
. 9 feature (12) -
set
_ get _ line _ procs (24) -
singleton
_ method (12) - start (12)
- struct (12)
-
to
_ json (12) -
to
_ s (24) - tsort (12)
-
undefine
_ finalizer (12) - yaml (12)
-
yaml
_ tag (12) - クラス/メソッドの定義 (12)
- セキュリティモデル (12)
- パターンマッチ (12)
- 演算子式 (12)
検索結果
-
演算子式 (114.0)
-
演算子式 * assign * selfassign * multiassign * range * range_cond * and * or * not * cond
...= メソッド呼び出しに変換されます。
//emlist[例][ruby]{
class C
def initialize
@ary = [0,1,2,3,4,5,6,7]
end
def [](i)
@ary[i * 2]
end
def []=( i, v )
@ary[i * 2] = v
end
end
c = C.new
p c[3] # c.[]( 3 ) に変換され、その結果は 6
p c[3] = 1 #......C
def foo
@foo
end
def foo=( v )
@foo = v
end
end
c = C.new
c.foo = 5 # c.foo=( 5 ) のように変換される
p c.foo # => 5
//}
属性は Module#attr を使って同じように定義できます。
//emlist[例][ruby]{
class C
attr :foo, true
end
c = C.new
c.foo = 5......代入式も多重代入にすることができます。
//emlist[][ruby]{
class C
def foo=( v )
@foo = v
end
def []=(i,v)
@bar = ["a", "b", "c"]
@bar[i] = v
end
end
obj = C.new
obj.foo, obj[2] = 1, 2 # @foo = 1; @bar = ["a", "b", 2]
//}
左辺が `,' で終る場合や... -
Observable (102.0)
-
Observer パターンを提供するモジュールです。
...コード
require "observer"
class Ticker ### Periodically fetch a stock price.
include Observable
def initialize(symbol)
@symbol = symbol
end
def run
last_price = nil
loop do
price = Price.fetch(@symbol)
print "Current price: #{price......now, price)
end
sleep 1
end
end
end
class Price ### A mock class to fetch a stock price (60 - 140).
def self.fetch(symbol)
60 + rand(80)
end
end
class Warner ### An abstract observer of Ticker objects.
def initialize(ticker, li......imit = limit
ticker.add_observer(self)
end
end
class WarnLow < Warner
def update(time, price) # callback for observer
if price < @limit
print "--- #{time.to_s}: Price below #@limit: #{price}\n"
end
end
end
class WarnHigh < Warner
def update(t... -
rdoc (90.0)
-
RDoc は Ruby のドキュメント生成を行うためのライブラリです。rdoc という ドキュメント生成のためのコマンドも含んでいます。
...ントも使え
ますし、=begin/=end でのコメントも使えます。=begin/=end を使う場合は、
以下のように =begin の行に 'rdoc' タグを付ける必要があります。
=begin rdoc
Documentation to
be processed by RDoc.
=end
パラグラフは左のインデン......したい場合は all 修飾
子を加えます。
//emlist{
module SM #:nodoc:
class Input
end
end
module Markup #:nodoc: all
class Output
end
end
//}
以上のコードでは、SM::Input のドキュメントのみが出力されます。
: :stopdoc......に含めたい場合に便利です。
: :enddoc:
以降の内容を一切ドキュメントに出力しません。
: :notnew:
これはインスタンスメソッドの initialize にのみ適用できます。通常、
RDoc は initialize メソッドのドキュメントやパラメ... -
ruby 1
. 8 . 3 feature (84.0) -
ruby 1.8.3 feature *((<ruby 1.8 feature>)) *((<ruby 1.8.2 feature>))
...た。
$ cat mthd_taint.rb
th = Thread.new{
$SAFE = 3
class Hoge
def foo
puts "safe level: #{$SAFE}"
end
end
}
th.join
p $SAFE
Hoge.new.foo
$ ruby-1.8.2 mthd_taint.rb
0
"safe level: 0"
$ ruby-1.8.3 mthd_ta......るバグを修正しました。
$ cat r.rb
p /[\c\\]/ =~ "\c\\"
p /\c\\/ =~ "\c\\"
$ ruby-1.8.2 r.rb
r.rb:1: premature end of regular expression: /[\c\\]/
r.rb:2: invalid regular expression; '\' can't be last character: /\c\\/
$ ruby-1.8.3 r.rb
0
0
=......あるという警告は、
オプションに -w を付けた時に出ます。((<ruby-dev:26201>))
=== 2005-05-22
: OpenSSL::SSL::SSLServer#initialize(svr, ctx, session_id=nil)
session_id を受け付けるようになりました。((<ruby-core:4663>))
=== 2005-05-19
: REXML::Encoding#decod... -
Data
. [](**kwargs) -> Data (78.0) -
(このメソッドは Data のサブクラスにのみ定義されています) 値オブジェクトを生成して返します。
...す。
new に渡す引数の数がメンバの数より少ない場合は new ではエラーにならず、そのまま initialize に渡されます。
ユーザが initialize のオーバーライドを通して、少ない引数のときの適切な振舞いを実装可能とするためです......、
残りのケースではエラーの発生箇所は new ではなく initialize であることに注意してください。
//emlist[例][ruby]{
Point = Data.define(:x, :y)
Point.new(1) # => in `initialize': missing keyword: :y (ArgumentError)
Point.new(1, 2, 3) # =......下の例のように、initialize メソッドをオーバーライドすることで new のオプション引数を実現できます。
//emlist[オプション引数を実現する例][ruby]{
Point = Data.define(:x, :y) do
def initialize(x:, y: 0)
super
end
end
Point.new(x: 1)... -
Data
. [](*args) -> Data (78.0) -
(このメソッドは Data のサブクラスにのみ定義されています) 値オブジェクトを生成して返します。
...す。
new に渡す引数の数がメンバの数より少ない場合は new ではエラーにならず、そのまま initialize に渡されます。
ユーザが initialize のオーバーライドを通して、少ない引数のときの適切な振舞いを実装可能とするためです......、
残りのケースではエラーの発生箇所は new ではなく initialize であることに注意してください。
//emlist[例][ruby]{
Point = Data.define(:x, :y)
Point.new(1) # => in `initialize': missing keyword: :y (ArgumentError)
Point.new(1, 2, 3) # =......下の例のように、initialize メソッドをオーバーライドすることで new のオプション引数を実現できます。
//emlist[オプション引数を実現する例][ruby]{
Point = Data.define(:x, :y) do
def initialize(x:, y: 0)
super
end
end
Point.new(x: 1)... -
Data
. new(**kwargs) -> Data (78.0) -
(このメソッドは Data のサブクラスにのみ定義されています) 値オブジェクトを生成して返します。
...す。
new に渡す引数の数がメンバの数より少ない場合は new ではエラーにならず、そのまま initialize に渡されます。
ユーザが initialize のオーバーライドを通して、少ない引数のときの適切な振舞いを実装可能とするためです......、
残りのケースではエラーの発生箇所は new ではなく initialize であることに注意してください。
//emlist[例][ruby]{
Point = Data.define(:x, :y)
Point.new(1) # => in `initialize': missing keyword: :y (ArgumentError)
Point.new(1, 2, 3) # =......下の例のように、initialize メソッドをオーバーライドすることで new のオプション引数を実現できます。
//emlist[オプション引数を実現する例][ruby]{
Point = Data.define(:x, :y) do
def initialize(x:, y: 0)
super
end
end
Point.new(x: 1)... -
Data
. new(*args) -> Data (78.0) -
(このメソッドは Data のサブクラスにのみ定義されています) 値オブジェクトを生成して返します。
...す。
new に渡す引数の数がメンバの数より少ない場合は new ではエラーにならず、そのまま initialize に渡されます。
ユーザが initialize のオーバーライドを通して、少ない引数のときの適切な振舞いを実装可能とするためです......、
残りのケースではエラーの発生箇所は new ではなく initialize であることに注意してください。
//emlist[例][ruby]{
Point = Data.define(:x, :y)
Point.new(1) # => in `initialize': missing keyword: :y (ArgumentError)
Point.new(1, 2, 3) # =......下の例のように、initialize メソッドをオーバーライドすることで new のオプション引数を実現できます。
//emlist[オプション引数を実現する例][ruby]{
Point = Data.define(:x, :y) do
def initialize(x:, y: 0)
super
end
end
Point.new(x: 1)... -
tsort (78.0)
-
tsort はトポロジカルソートと強連結成分に関するモジュールを提供します。
...]{
require 'tsort'
class Hash
include TSort
alias tsort_each_node each_key
def tsort_each_child(node, &block)
fetch(node).each(&block)
end
end
{1=>[2, 3], 2=>[3], 3=>[], 4=>[]}.tsort
#=> [3, 2, 1, 4]
{1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}.strongly_connected_components
#=> [[4], [2, 3], [1......mlist[][ruby]{
require 'tsort'
class Make
def initialize
@dep = {}
@dep.default = []
end
def rule(outputs, inputs=[], &block)
triple = [outputs, inputs, block]
outputs.each {|f| @dep[f] = [triple]}
@dep[triple] = inputs
end
def build(target)
each_strongly_connect......_from(target) {|ns|
if ns.length != 1
fs = ns.delete_if {|n| Array === n}
raise TSort::Cyclic.new("cyclic dependencies: #{fs.join ', '}")
end
n = ns.first
if Array === n
outputs, inputs, block = n
inputs_time = inputs.map {|f| File.mtime f}.max... -
MonitorMixin (72.0)
-
スレッドの同期機構としてのモニター機能を提供するモジュールです。
...lude したり、オブジェクトに
Object#extend したりすることでそのクラス/オブジェクトに
モニタ機能を追加します。
=== 例
//emlist[消費者、生産者問題の例][ruby]{
require 'monitor'
buf = []
buf.extend(MonitorMixin) # 配列にモニタ機能を追......行
end # ロックを開放
end
end
# producer
while line = ARGF.gets
buf.synchronize do # ロックする
buf.push(line) # 配列を変更(追加)
empty_cond.signal # 配列に要素が追加されたことを条件変数を通して通知
end # ここでロックを開放
end
/......bject#extend を使って利用する場合は
自動的に初期化されます。
//emlist[extend する例][ruby]{
require 'monitor'
buf = []
buf.extend(MonitorMixin)
//}
しかし、MonitorMixin をクラス定義の際に Module#include を使って
利用する場合は、initialize メソ... -
cgi
/ session (72.0) -
CGI のセッション管理を行うライブラリ。
...クリプト。
ソースコード
#!/usr/bin/ruby
require 'kconv'
require 'cgi'
require 'cgi/session'
class SessionDemo
def initialize
@cgi = CGI.new
File.umask(0077) # セッションファイルは誰にも読まれたくないよ......'start' if @cmd.empty?
@header = { "type" => "text/html", "charset" => "euc-jp" }
__send__("cmd_#{@cmd}")
end
def cmd_start
@cgi.out(@header) {
<<-END
<html><head><title>CGI::Session Demo</title></head>
<body>
<form action="#{CGI......hidden" name="cmd" value="hello">
<input type="submit" value="です。">
</p>
</form>
</body></html>
END
}
end
def cmd_hello
name = Kconv.toeuc(@cgi['name'].first)
@session['name'] = name # セッシ... -
Data
. define(*args) -> Class (66.0) -
Data クラスに新しいサブクラスを作って、それを返します。
...y: "Current time is #{Time.now}")
else
NotFound.new
end
end
end
def fetch(url)
fetcher = HTTPFetcher.new
case fetcher.get(url)
in HTTPFetcher::Response(body)
body
in HTTPFetcher::NotFound
:NotFound
end
end
p fetch("http://example.com/") # => "Current time is 2......ess) do
def greeting
"Hello #{name}!"
end
end
p Customer.new("Dave", "123 Main").greeting # => "Hello Dave!"
//}
なお、Dataのサブクラスのインスタンスを生成する際にオプション引数を使用したいときは、
initialize メソッドをオーバーライドする......ことで実現できます。
//emlist[例][ruby]{
Point = Data.define(:x, :y) do
def initialize(x:, y: 0)
super
end
end
p Point.new(x: 1) # => #<data Point x=1, y=0>
p Point.new(x: 1, y: 2) # => #<data Point x=1, y=2>
//}... -
Data
. define(*args) {|subclass| block } -> Class (66.0) -
Data クラスに新しいサブクラスを作って、それを返します。
...y: "Current time is #{Time.now}")
else
NotFound.new
end
end
end
def fetch(url)
fetcher = HTTPFetcher.new
case fetcher.get(url)
in HTTPFetcher::Response(body)
body
in HTTPFetcher::NotFound
:NotFound
end
end
p fetch("http://example.com/") # => "Current time is 2......ess) do
def greeting
"Hello #{name}!"
end
end
p Customer.new("Dave", "123 Main").greeting # => "Hello Dave!"
//}
なお、Dataのサブクラスのインスタンスを生成する際にオプション引数を使用したいときは、
initialize メソッドをオーバーライドする......ことで実現できます。
//emlist[例][ruby]{
Point = Data.define(:x, :y) do
def initialize(x:, y: 0)
super
end
end
p Point.new(x: 1) # => #<data Point x=1, y=0>
p Point.new(x: 1, y: 2) # => #<data Point x=1, y=2>
//}... -
Thread
:: ConditionVariable (66.0) -
スレッドの同期機構の一つである状態変数を実現するクラスです。
....new
a = Thread.start {
mutex.synchronize {
...
while (条件が満たされない)
cv.wait(mutex)
end
...
}
}
b = Thread.start {
mutex.synchronize {
# 上の条件を満たすための操作......す。
require 'thread'
class TinyQueue
def initialize(max=2)
@max = max
@full = ConditionVariable.new
@empty = ConditionVariable.new
@mutex = Mutex.new
@q = []
end
def count
@q.size
end
def enq(v)
@mutex.synchronize{
@ful......@empty.signal if count == 1
}
end
def deq
@mutex.synchronize{
@empty.wait(@mutex) if count == 0
v = @q.shift
@full.signal if count == (@max - 1)
v
}
end
alias send enq
alias recv deq
end
if __FILE__ == $0
q = TinyQue...