67件ヒット
[1-67件を表示]
(0.031秒)
別のキーワード
モジュール
- JSON (12)
検索結果
先頭5件
-
Object
. yaml _ tag(tag) -> () (9149.0) -
クラスと tag の間を関連付けます。
...s Foo
def initialize(x)
@x = x
end
attr_reader :x
end
# Dumps Ruby object normally
p Psych.dump(Foo.new(3))
# =>
# --- !ruby/object:Foo
# x: 3
# Registers tag with class Foo
Foo.yaml_as("tag:example.com,2013:foo")
# ... and dumps the object of Foo clas......s
Psych.dump(Foo.new(3), STDOUT)
# =>
# --- !<tag:example.com,2013:foo>
# x: 3
# Loads the object from the tagged YAML node
p Psych.load(<<EOS)
--- !<tag:example.com,2012:foo>
x: 8
EOS
# => #<Foo:0x0000000130f48 @x=8>... -
Class
. new(superclass = Object) -> Class (233.0) -
新しく名前の付いていない superclass のサブクラスを生成します。
...をクラス名とします。
//emlist[例][ruby]{
p foo = Class.new # => #<Class:0x401b90f8>
p foo.name # => nil
Foo = foo # ここで p foo すれば "Foo" 固定
Bar = foo
p foo.name # => "Bar" ("Foo" になるか "Bar" になるかは不定)
//}
ブロックが......Class#initialize が行います。
@param superclass 生成するクラスのスーパークラスを指定します。
//emlist[例][ruby]{
k = Class.new{|c|
def initialize
p "in initialize"
end
def hoge
p "hoge hoge hoge"
end
}
o = k.new #=> "in initialize"
o.hoge......#=> "hoge hoge hoge"
//}... -
Class
. new(superclass = Object) {|klass| . . . } -> Class (233.0) -
新しく名前の付いていない superclass のサブクラスを生成します。
...をクラス名とします。
//emlist[例][ruby]{
p foo = Class.new # => #<Class:0x401b90f8>
p foo.name # => nil
Foo = foo # ここで p foo すれば "Foo" 固定
Bar = foo
p foo.name # => "Bar" ("Foo" になるか "Bar" になるかは不定)
//}
ブロックが......Class#initialize が行います。
@param superclass 生成するクラスのスーパークラスを指定します。
//emlist[例][ruby]{
k = Class.new{|c|
def initialize
p "in initialize"
end
def hoge
p "hoge hoge hoge"
end
}
o = k.new #=> "in initialize"
o.hoge......#=> "hoge hoge hoge"
//}... -
JSON
. create _ id -> String (125.0) -
json_create メソッドで使用するクラスを決定するために使用する値を返します。
...class User
attr :id, :name
def initialize(id, name)
@id, @name = id, name
end
def self.json_create(object)
new(object['id'], object["name"])
end
def as_json(*)
{
JSON.create_id => self.class.name,
"id" => id,
"name" => name,
}
end
def to_json(*)......as_json.to_json
end
end
json = JSON.generate(User.new(1, "tanaka"))
json # => "{\"json_class\":\"User\",\"id\":1,\"name\":\"tanaka\"}"
JSON.parse(json, create_additions: true)
# => #<User:0x0000557709b269e0 @id=1, @name="tanaka">
//}... -
Proc
. new -> Proc (119.0) -
ブロックをコンテキストとともにオブジェクト化して返します。
...pturing the given block using Proc.new is deprecated; use `&block` instead」
が出力され、Ruby 3.0 では
ArgumentError (tried to create Proc object without a block)
が発生します。
ブロックを指定しなければ、このメソッドを呼び出したメソッドが
ブロック......rg }
# => 1
//}
これは以下と同じです。
//emlist[例][ruby]{
def foo
yield(1)
end
foo {|arg| p arg }
# => 1
//}
呼び出し元のメソッドがブロックを伴わなければ、例外
ArgumentError が発生します。
//emlist[例][ruby]{
def foo
Proc.new
end
foo
# => -:2:in......`new': tried to create Proc object without a block (ArgumentError)
# from -:2:in `foo'
# from -:4:in `<main>'
//}
Proc.new は、Proc#initialize が定義されていれば
オブジェクトの初期化のためにこれを呼び出します。このことを
除けば、Kernel.#proc... -
Proc
. new { . . . } -> Proc (119.0) -
ブロックをコンテキストとともにオブジェクト化して返します。
...pturing the given block using Proc.new is deprecated; use `&block` instead」
が出力され、Ruby 3.0 では
ArgumentError (tried to create Proc object without a block)
が発生します。
ブロックを指定しなければ、このメソッドを呼び出したメソッドが
ブロック......rg }
# => 1
//}
これは以下と同じです。
//emlist[例][ruby]{
def foo
yield(1)
end
foo {|arg| p arg }
# => 1
//}
呼び出し元のメソッドがブロックを伴わなければ、例外
ArgumentError が発生します。
//emlist[例][ruby]{
def foo
Proc.new
end
foo
# => -:2:in......`new': tried to create Proc object without a block (ArgumentError)
# from -:2:in `foo'
# from -:4:in `<main>'
//}
Proc.new は、Proc#initialize が定義されていれば
オブジェクトの初期化のためにこれを呼び出します。このことを
除けば、Kernel.#proc......します。
//emlist[][ruby]{
pr = Proc.new {|arg| p arg }
pr.call(1) # => 1
//}
//emlist[][ruby]{
Proc.new # => -e:1:in `new': tried to create Proc object without a block (ArgumentError)
//}
Proc.new は、Proc#initialize が定義されていれば
オブジェクトの初期化のためにこ...