ライブラリ
- ビルトイン (104)
- forwardable (22)
- monitor (11)
- thread (2)
-
webrick
/ httputils (11) - win32ole (11)
クラス
- Method (40)
- Module (11)
- Struct (44)
-
WEBrick
:: HTTPUtils :: FormData (11) -
WIN32OLE
_ TYPE (11)
モジュール
- Forwardable (22)
キーワード
-
1
. 6 . 8から1 . 8 . 0への変更点(まとめ) (11) - === (7)
- ConditionVariable (11)
- Marshal フォーマット (11)
- MonitorMixin (11)
-
NEWS for Ruby 2
. 7 . 0 (5) -
NEWS for Ruby 3
. 1 . 0 (3) - Rubyで使われる記号の意味(正規表現の複雑な記号は除く) (11)
- call (22)
-
def
_ delegator (11) -
def
_ instance _ delegator (11) -
default
_ event _ sources (11) -
rexml
/ parsers / sax2parser (11) -
rexml
/ parsers / streamparser (11) -
ruby 1
. 6 feature (11) -
ruby 1
. 8 . 3 feature (11) -
ruby 1
. 9 feature (11) - tsort (11)
-
undef
_ method (11) -
webrick
/ cgi (11) - クラス/メソッドの定義 (11)
- パターンマッチ (11)
- 演算子式 (11)
検索結果
先頭5件
-
演算子式 (60.0)
-
演算子式 * assign * selfassign * multiassign * range * range_cond * and * or * not * cond
...び出しと制御構造は演算子形
式をとります。Rubyには以下にあげる演算子があります。
高い ::
[]
+(単項) ! ~
**
-(単項)
* / %
+ -......までを
引数とする []= メソッド呼び出しに変換されます。
//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.[]=(3,1) に変換され、その結果は 1
//}
: 属性参照
//emlist{
式1 `.' 識別子 `=' 式2
//}
式 1 を評価して得られるオブジェクトに対して、
識別子= というメソッドを、式 2 を引数にして呼び出し... -
Module
# undef _ method(*name) -> self (54.0) -
このモジュールのインスタンスメソッド name を未定義にします。
...ist[例][ruby]{
class A
def ok
puts 'A'
end
end
class B < A
def ok
puts 'B'
end
end
B.new.ok # => B
# undef_method の場合はスーパークラスに同名のメソッドがあっても
# その呼び出しはエラーになる
class B
undef_method :ok
end
B.new.ok # => Nam......eError
# remove_method の場合はスーパークラスに同名のメソッドがあると
# それが呼ばれる
class B
remove_method :ok
end
B.new.ok # => A
//}
また、undef 文と undef_method の違いは、
メソッド名を String または Symbol で与えられることです。......module M1
def foo
end
def self.moo
undef foo
end
end
M1.instance_methods false #=> ["foo"]
M1.moo
M1.instance_methods false #=> []
module M2
def foo
end
def self.moo
undef_method :foo
end
end
M2.instance_methods false #=> ["foo"]
M2.moo
M2.instance_methods false #=> []
//}... -
rexml
/ parsers / streamparser (48.0) -
ストリーム式の XML パーサ。
...ソッドをオーバーライドしたクラスのオブジェクトを
コールバックオブジェクトとして REXML::Parsers::StreamParser.new
に渡します。
REXML::Parsers::StreamParser#parse を呼び出すと
パースが開始しその結果によってコールバックが呼び......あることに注意してください。
//emlist[][ruby]{
require 'rexml/parsers/baseparser'
require 'rexml/parsers/streamparser'
require 'rexml/streamlistener'
class Listener
include REXML::StreamListener
def initialize
@events = []
end
def text(text)
@events << "text[#{text}]"......="red">
<comment>comment here</comment>
</member>
<member name="banana" color="yellow"/>
</members>
EOS
listener = Listener.new
REXML::Parsers::StreamParser.new(xml, listener).parse
listener.events
# => ["tag_start[members]",
# "text[\n ]",
# "tag_start[member]",
# "text[\n ]... -
パターンマッチ (48.0)
-
パターンマッチ * patterns * variable_binding * variable_pinning * matching_non_primitive_objects * guard_clauses * current_feature_status * pattern_syntax * some_undefined_behavior_examples
...hingPatternError が発生します。
そのため、条件付きのマッチや展開に case 式が使われることがあります。
//emlist[][ruby]{
config = {db: {user: 'admin', password: 'abc123'}}
case config
in db: {user:} # ネストしてハッシュにマッチして、その値を......形は rescue 節で 『rescue ExceptionClass => var』 の形で例外をローカル変数に格納する形に似ています)
//emlist[][ruby]{
case [1, 2]
in Integer => a, Integer
"matched: #{a}"
else
"not matched"
end
#=> "matched: 1"
//}
//emlist[][ruby]{
case {a: 1, b: 2, c: 3}
in a: I......す。
//emlist[][ruby]{
class Point
def initialize(x, y)
@x, @y = x, y
end
def deconstruct
puts "deconstruct called"
[@x, @y]
end
def deconstruct_keys(keys)
puts "deconstruct_keys called with #{keys.inspect}"
{x: @x, y: @y}
end
end
case Point.new(1, -2)
in px, In... -
rexml
/ parsers / sax2parser (42.0) -
SAX2 と同等の API を持つストリーム式の XML パーサ。
...サよりは高機能です。
//emlist[][ruby]{
require 'rexml/parsers/sax2parser'
require 'rexml/sax2listener'
parser = REXML::Parsers::SAX2Parser.new(<<XML)
<root n="0">
<a n="1">111</a>
<b n="2">222</b>
<a n="3">333</a>
</root>
XML
elements = []
parser.listen(:start_element){|uri, local......name, qname, attrs|
elements << [qname, attrs]
}
as = []
parser.listen(:start_element, ["a"]){|uri, localname, qname, attrs|
as << [qname, attrs]
}
texts = []
parser.listen(:characters, ["a"]){|c| texts << c }
parser.parse
elements # => [["root", {"n"=>"0"}], ["a", {"n"=>"1"}], ["b", {"n"=>"2"}]......e--> &bar;
</root>
EOS
class Listener
#include REXML::SAX2Listener
def method_missing(name, *args)
p [name, *args]
end
def respond_to_missing?(name, include_private)
name != :call
end
end
parser = REXML::Parsers::SAX2Parser.new(xml)
parser.listen(Listener.new)
parser.parse
# >> [... -
tsort (30.0)
-
tsort はトポロジカルソートと強連結成分に関するモジュールを提供します。
...=== Example
//emlist[][ruby]{
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_c......単純な `make' に似たツールは以下のように実装できます。
//emlist[][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] = [tripl......each_strongly_connected_component_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 =... -
Method
# ===(*args) -> object (24.0) -
メソッドオブジェクトに封入されているメソッドを起動します。
...やブロックはそのままメソッドに渡されます。
self[] の形の呼び出しは通常のメソッド呼び出しに見た目を
近付けるためだけに用意されたもので、Array#[]のような
他の [] メソッドとの意味的な関連性はありません。
@param......args self に渡される引数。
@see spec/safelevel
//emlist[例][ruby]{
class Foo
def foo(arg)
"foo called with arg #{arg}"
end
end
m = Foo.new.method(:foo) # => #<Method: Foo#foo>
m[1] # => "foo called with arg 1"
m.call(2) # => "foo called with arg 2"
//}......args self に渡される引数。
@see UnboundMethod#bind_call
@see spec/safelevel
//emlist[例][ruby]{
class Foo
def foo(arg)
"foo called with arg #{arg}"
end
end
m = Foo.new.method(:foo) # => #<Method: Foo#foo>
m[1] # => "foo called with arg 1"
m.call(2) # => "foo called with arg......args self に渡される引数。
@see UnboundMethod#bind_call
//emlist[例][ruby]{
class Foo
def foo(arg)
"foo called with arg #{arg}"
end
end
m = Foo.new.method(:foo) # => #<Method: Foo#foo>
m[1] # => "foo called with arg 1"
m.call(2) # => "foo called with arg 2"
//}... -
Method
# call(*args) -> object (24.0) -
メソッドオブジェクトに封入されているメソッドを起動します。
...やブロックはそのままメソッドに渡されます。
self[] の形の呼び出しは通常のメソッド呼び出しに見た目を
近付けるためだけに用意されたもので、Array#[]のような
他の [] メソッドとの意味的な関連性はありません。
@param......args self に渡される引数。
@see spec/safelevel
//emlist[例][ruby]{
class Foo
def foo(arg)
"foo called with arg #{arg}"
end
end
m = Foo.new.method(:foo) # => #<Method: Foo#foo>
m[1] # => "foo called with arg 1"
m.call(2) # => "foo called with arg 2"
//}......args self に渡される引数。
@see UnboundMethod#bind_call
@see spec/safelevel
//emlist[例][ruby]{
class Foo
def foo(arg)
"foo called with arg #{arg}"
end
end
m = Foo.new.method(:foo) # => #<Method: Foo#foo>
m[1] # => "foo called with arg 1"
m.call(2) # => "foo called with arg......args self に渡される引数。
@see UnboundMethod#bind_call
//emlist[例][ruby]{
class Foo
def foo(arg)
"foo called with arg #{arg}"
end
end
m = Foo.new.method(:foo) # => #<Method: Foo#foo>
m[1] # => "foo called with arg 1"
m.call(2) # => "foo called with arg 2"
//}... -
Method
# call(*args) { . . . } -> object (24.0) -
メソッドオブジェクトに封入されているメソッドを起動します。
...やブロックはそのままメソッドに渡されます。
self[] の形の呼び出しは通常のメソッド呼び出しに見た目を
近付けるためだけに用意されたもので、Array#[]のような
他の [] メソッドとの意味的な関連性はありません。
@param......args self に渡される引数。
@see spec/safelevel
//emlist[例][ruby]{
class Foo
def foo(arg)
"foo called with arg #{arg}"
end
end
m = Foo.new.method(:foo) # => #<Method: Foo#foo>
m[1] # => "foo called with arg 1"
m.call(2) # => "foo called with arg 2"
//}......args self に渡される引数。
@see UnboundMethod#bind_call
@see spec/safelevel
//emlist[例][ruby]{
class Foo
def foo(arg)
"foo called with arg #{arg}"
end
end
m = Foo.new.method(:foo) # => #<Method: Foo#foo>
m[1] # => "foo called with arg 1"
m.call(2) # => "foo called with arg......args self に渡される引数。
@see UnboundMethod#bind_call
//emlist[例][ruby]{
class Foo
def foo(arg)
"foo called with arg #{arg}"
end
end
m = Foo.new.method(:foo) # => #<Method: Foo#foo>
m[1] # => "foo called with arg 1"
m.call(2) # => "foo called with arg 2"
//}... -
MonitorMixin (24.0)
-
スレッドの同期機構としてのモニター機能を提供するモジュールです。
...を追加します。
=== 例
//emlist[消費者、生産者問題の例][ruby]{
require 'monitor'
buf = []
buf.extend(MonitorMixin) # 配列にモニタ機能を追加
empty_cond = buf.new_cond # 配列が空であるかないかを通知する条件変数
# consumer
Thread.start do
loop do......に Object#extend を使って利用する場合は
自動的に初期化されます。
//emlist[extend する例][ruby]{
require 'monitor'
buf = []
buf.extend(MonitorMixin)
//}
しかし、MonitorMixin をクラス定義の際に Module#include を使って
利用する場合は、initialize メ......数を受け付けないので
super ではなく super() を呼ぶ必要があります。
//emlist[include する例][ruby]{
require 'monitor'
class MyObject
include MonitorMixin
def initialize(val)
super()
@value = val
end
def to_s
synchronize {
@value.to_s
}
e... -
Forwardable
# def _ delegator(accessor , method , ali = method) -> () (18.0) -
メソッドの委譲先を設定します。
...ator の別名になります。
例:
require 'forwardable'
class MyQueue
extend Forwardable
attr_reader :queue
def initialize
@queue = []
end
def_delegator :@queue, :push, :mypush
end
q = MyQueue.new
q.mypush 42
q.queue # => [42]
q.push 23 # => NoMeth...