るりまサーチ (Ruby 3.3)

最速Rubyリファレンスマニュアル検索!
6件ヒット [1-6件を表示] (0.013秒)
トップページ > クラス:Struct[x] > クエリ:[][x] > バージョン:3.3[x]

別のキーワード

  1. _builtin []
  2. matrix []
  3. string []
  4. rake []
  5. fiddle []

ライブラリ

キーワード

検索結果

Struct#[](member) -> object (54304.0)

構造体のメンバの値を返します。

...指定した場合に発生します。

//emlist[例][ruby]{
Foo = Struct.new(:foo, :bar)
obj = Foo.new('FOO', 'BAR')
p obj[:foo] # => "FOO"
p obj['bar'] # => "BAR"
# p obj[:baz] # => in `[]': no member 'baz' in struct (NameError)
p obj[0] # => "FOO"
p obj[1] # => "BAR"
p o...
...p obj[2] # => in `[]': offset 2 too large for struct(size:2) (IndexError)
//}

[注意] 本メソッドの記述は Struct の下位クラスのインスタンスに対して呼び
出す事を想定しています。Struct.new は Struct の下位クラスを作成する点に
注意して...

Struct.[](*args) -> Struct (54304.0)

(このメソッドは Struct の下位クラスにのみ定義されています) 構造体オブジェクトを生成して返します。

...(このメソッドは Struct の下位クラスにのみ定義されています)
構造体オブジェクトを生成して返します。

@param args 構造体の初期値を指定します。メンバの初期値は指定されなければ nil です。

@return 構造体クラスのインス...
...タンス。

@raise ArgumentError 構造体のメンバの数よりも多くの引数を指定した場合に発生します。

//emlist[例][ruby]{
Foo = Struct.new(:foo, :bar)
foo = Foo.new(1)
p foo.values # => [1, nil]
//}...

Struct#[]=(member, value) (18304.0)

構造体の member で指定されたメンバの値を value にして value を返します。

...注意] 本メソッドの記述は Struct の下位クラスのインスタンスに対して呼び
出す事を想定しています。Struct.new は Struct の下位クラスを作成する点に
注意してください。

//emlist[例][ruby]{
Customer = Struct.new(:name, :address, :zip)
joe =...

Struct.new(*args) -> Struct (9004.0)

(このメソッドは Struct の下位クラスにのみ定義されています) 構造体オブジェクトを生成して返します。

...(このメソッドは Struct の下位クラスにのみ定義されています)
構造体オブジェクトを生成して返します。

@param args 構造体の初期値を指定します。メンバの初期値は指定されなければ nil です。

@return 構造体クラスのインス...
...タンス。

@raise ArgumentError 構造体のメンバの数よりも多くの引数を指定した場合に発生します。

//emlist[例][ruby]{
Foo = Struct.new(:foo, :bar)
foo = Foo.new(1)
p foo.values # => [1, nil]
//}...

Struct.new(*args, keyword_init: nil) -> Class (9004.0)

Struct クラスに新しいサブクラスを作って、それを返します。

...
Struct
クラスに新しいサブクラスを作って、それを返します。

サブクラスでは構造体のメンバに対するアクセスメソッドが定義されています。

//emlist[例][ruby]{
dog = Struct.new("Dog", :name, :age)
fred = dog.new("fred", 5)
fred.age = 6
printf "...
.../emlist[例][ruby]{
Point1 = Struct.new(:x, :y)
Point1.new(1, 2) # => #<struct Point1 x=1, y=2>
Point1.new(x: 1, y: 2) # => #<struct Point1 x=1, y=2>
Point1.new(x: 1) # => #<struct Point1 x=1, y=nil>
Point1.new(y: 2) # => #<struct Point1 x=nil, y=2>
Point1.n...
...words: z)

Point2 = Struct.new(:x, :y, keyword_init: nil)
Point2.new(1, 2) # => #<struct Point2 x=1, y=2>
Point2.new(x: 1, y: 2) # => #<struct Point2 x=1, y=2>
Point2.new(x: 1) # => #<struct Point2 x=1, y=nil>
Point2.new(y: 2) # => #<struct Point2 x=nil, y=2...

絞り込み条件を変える

Struct.new(*args, keyword_init: nil) {|subclass| block } -> Class (9004.0)

Struct クラスに新しいサブクラスを作って、それを返します。

...
Struct
クラスに新しいサブクラスを作って、それを返します。

サブクラスでは構造体のメンバに対するアクセスメソッドが定義されています。

//emlist[例][ruby]{
dog = Struct.new("Dog", :name, :age)
fred = dog.new("fred", 5)
fred.age = 6
printf "...
.../emlist[例][ruby]{
Point1 = Struct.new(:x, :y)
Point1.new(1, 2) # => #<struct Point1 x=1, y=2>
Point1.new(x: 1, y: 2) # => #<struct Point1 x=1, y=2>
Point1.new(x: 1) # => #<struct Point1 x=1, y=nil>
Point1.new(y: 2) # => #<struct Point1 x=nil, y=2>
Point1.n...
...words: z)

Point2 = Struct.new(:x, :y, keyword_init: nil)
Point2.new(1, 2) # => #<struct Point2 x=1, y=2>
Point2.new(x: 1, y: 2) # => #<struct Point2 x=1, y=2>
Point2.new(x: 1) # => #<struct Point2 x=1, y=nil>
Point2.new(y: 2) # => #<struct Point2 x=nil, y=2...