111件ヒット
[1-100件を表示]
(0.049秒)
ライブラリ
- ビルトイン (53)
- fiddle (11)
- logger (1)
- psych (11)
-
rdoc
/ markup (11) -
ripper
/ filter (11) - thread (2)
- timeout (11)
キーワード
- Application (1)
- BasicObject (11)
- Closure (11)
- ConditionVariable (11)
- Error (11)
- Filter (11)
- Location (11)
- Method (11)
- Parser (11)
- UnboundMethod (11)
検索結果
-
Fiddle
:: Closure (55.0) -
コールバック関数を表すクラスです。
...を作って
そのサブクラスに call メソッドを定義し、
new でオブジェクトを生成することで利用します。
require 'fiddle'
include Fiddle # TYPE_* を使うために include する
class Compare < Fiddle::Closure
# qsort の比較関数は 型が int(......s = Fiddle::Function.new(libc["qsort"],
[TYPE_VOIDP, TYPE_INT, TYPE_INT, TYPE_VOIDP],
TYPE_VOID)
s = "7x0cba(Uq)"
qs.call(s, s.size, 1, Compare.new(TYPE_INT, [TYPE_VOIDP, TYPE_VOIDP]))
p s # => "()07Uabcqx"
Class.new を使うことで......できます。
require 'fiddle'
include Fiddle # TYPE_* を使うために include する
compare = Class.new(Fiddle::Closure){
def call(x, y)
x.to_s(1) <=> y
end
}.new(TYPE_INT, [TYPE_VOIDP, TYPE_VOIDP])
単に Ruby のブロックを C の(コールバック)関数に変換... -
Method (55.0)
-
Object#method によりオブジェクト化され たメソッドオブジェクトのクラスです。
...くと言えます。
既存のメソッドを Method オブジェクト化する。
//emlist[例][ruby]{
class Foo
def foo(arg)
"foo called with arg #{arg}"
end
end
m = Foo.new.method(:foo)
p m # => #<Method: Foo#foo>
p m.call(1) # => "foo called with arg 1"
//}
名前......//emlist[例][ruby]{
pr = Proc.new {|arg|
"proc called with arg #{arg}"
}
p pr # => #<Proc:0x401b1fcc>
p pr.call(1) # => "proc called with arg 1"
//}
Method オブジェクトが有用なのは以下のような場合。
//emlist[例][ruby]{
class Foo
def foo() "foo" end
def......bar() "bar" end
def baz() "baz" end
end
obj = Foo.new
# 任意のキーとメソッドの関係をハッシュに保持しておく
methods = {1 => obj.method(:foo),
2 => obj.method(:bar),
3 => obj.method(:baz)}
# キーを使って関連するメソッドを呼び出す... -
Thread
:: ConditionVariable (55.0) -
スレッドの同期機構の一つである状態変数を実現するクラスです。
...レッド a に対して条件が成立したことを通知します。これが典型的な
使用例です。
mutex = Mutex.new
cv = ConditionVariable.new
a = Thread.start {
mutex.synchronize {
...
while (条件が満たされない)
cv.wait(m......tion Variable を使って wait しています。
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 e......E__ == $0
q = TinyQueue.new(1)
foods = 'Apple Banana Strawberry Udon Rice Milk'.split
l = []
th = Thread.new {
for obj in foods
q.send(obj)
print "sent ", obj, "\n"
end
q.send nil
}
l.push th
th = Thread.new {
while obj = q.recv... -
UnboundMethod (37.0)
-
レシーバを持たないメソッドを表すクラスです。 呼び出すためにはレシーバにバインドする必要があります。
...とができます。
//emlist[例: Method クラスの冒頭にある例を UnboundMethod で書くと以下のようになります。][ruby]{
class Foo
def foo() "foo" end
def bar() "bar" end
def baz() "baz" end
end
# 任意のキーとメソッドの関係をハッシュに保持して......bind(Foo.new).call # => "foo"
p methods[2].bind(Foo.new).call # => "bar"
p methods[3].bind(Foo.new).call # => "baz"
//}
//emlist[例: 以下はメソッドの再定義を UnboundMethod を使って行う方法です。普通は alias や super を使います。][ruby]{
class Foo
d......ef foo
p :foo
end
@@orig_foo = instance_method :foo
def foo
p :bar
@@orig_foo.bind(self).call
end
end
Foo.new.foo
# => :bar
# :foo
//}... -
Psych
:: Parser (31.0) -
YAML のパーサ。
...を表示します。
# Handler for detecting scalar values
class ScalarHandler < Psych::Handler
def scalar value, anchor, tag, plain, quoted, style
puts value
end
end
parser = Psych::Parser.new(ScalarHandler.new)
parser.parse(yaml_document)
次の例は Psych::Emitter に......からの入力をパース→YAMLフォーマットで STDERR に出力
という流れになっています。
parser = Psych::Parser.new(Psych::Emitter.new($stderr))
parser.parse($stdin)
Psych::Parser と Psych::TreeBuilder を組み合わせると
YAML の AST を構築することがで... -
RDoc
:: Markup (25.0) -
RDoc 形式のドキュメントを目的の形式に変換するためのクラスです。
...require 'rdoc/markup/to_html'
h = RDoc::Markup::ToHtml.new
puts h.convert(input_string)
独自のフォーマットを行うようにパーサを拡張する事もできます。
例:
require 'rdoc/markup'
require 'rdoc/markup/to_html'
class WikiHtml < RDoc::Markup::ToHtml
# W......ントを赤く表示。
def handle_special_WIKIWORD(special)
"<font color=red>" + special.text + "</font>"
end
end
m = RDoc::Markup.new
# { 〜 } までを :STRIKE でフォーマットする。
m.add_word_pair("{", "}", :STRIKE)
# <no> 〜 </no> までを :STRIKE でフォ......IWORD)
wh = WikiHtml.new(m)
# :STRIKE のフォーマットを <strike> 〜 </strike> に指定。
wh.add_tag(:STRIKE, "<strike>", "</strike>")
puts "<body>#{wh.convert ARGF.read}</body>"
変換する形式を変更する場合、フォーマッタ(例. RDoc::Markup::ToHtml)
を変更、... -
Ripper
:: Filter (25.0) -
イベントドリブンスタイルで Ruby プログラムを加工するためのクラスです。
...e 'ripper'
require 'cgi'
class Ruby2HTML < Ripper::Filter
def on_default(event, tok, f)
f << CGI.escapeHTML(tok)
end
def on_comment(tok, f)
f << %Q[<span class="comment">#{CGI.escapeHTML(tok)}</span>]
end
def on_tstring_beg(tok, f)
f << %Q[<span class="string">#{CGI.escapeHT......ML(tok)}]
end
def on_tstring_end(tok, f)
f << %Q[#{CGI.escapeHTML(tok)}</span>]
end
end
Ruby2HTML.new(ARGF).parse('')
//}
Ruby プログラムを解析して、Ripper::SCANNER_EVENTS にあるスキャナ
イベントを実行します。イベントはプログラムに書いた順番... -
Timeout
:: Error (25.0) -
timeout で定義される例外クラスです。 関数 timeout がタイムアウトすると発生します。
...r のサブクラスを
定義して使用した方が無難です。
==> foo.rb <==
require 'timeout.rb'
class Foo
FooTimeoutError = Class.new(Timeout::Error)
def longlongtime_method
Timeout.timeout(100, FooTimeoutError) {
.........}
end
end
==> main.rb <==
require 'foo'
Timeout.timeout(5) {
Foo.new.longlongtime_method
}... -
Thread
:: Backtrace :: Location (19.0) -
Ruby のフレームを表すクラスです。
...emlist[例2][ruby]{
# foo.rb
class Foo
attr_accessor :locations
def initialize(skip)
@locations = caller_locations(skip)
end
end
Foo.new(0..2).locations.map do |call|
puts call.to_s
end
//}
例2の実行結果:
init.rb:4:in `initialize'
init.rb:8:in `new'
init.rb:8:in `<main>'... -
BasicObject (13.0)
-
特殊な用途のために意図的にほとんど何も定義されていないクラスです。 Objectクラスの親にあたります。Ruby 1.9 以降で導入されました。
...合にだけ BasicObject から派生してください。
=== 例
//emlist[例][ruby]{
class Proxy < BasicObject
def initialize(target)
@target = target
end
def method_missing(message, *args)
@target.__send__(message, *args)
end
end
proxy = Proxy.new("1")
proxy.to_i #=> 1
//}...