るりまサーチ

最速Rubyリファレンスマニュアル検索!
82件ヒット [1-82件を表示] (0.051秒)
トップページ > クエリ:ruby[x] > クエリ:Ruby[x] > クエリ:class[x] > クエリ:initialize[x] > 種類:特異メソッド[x]

別のキーワード

  1. rbconfig ruby
  2. fiddle ruby_free
  3. fiddle build_ruby_platform
  4. rubygems/defaults ruby_engine
  5. rake ruby

ライブラリ

クラス

モジュール

キーワード

検索結果

Class.new(superclass = Object) -> Class (9293.0)

新しく名前の付いていない superclass のサブクラスを生成します。

...ていない superclass のサブクラスを生成します。

名前のないクラスは、最初に名前を求める際に代入されている定数名を検
索し、見つかった定数名をクラス名とします。

//emlist[例][ruby]{
p foo = Class.new # => #<Class:0x401b90f8>
p fo...
...コードと同じです。

//emlist[例][ruby]{
klass = Class.new(superclass)
klass.module_eval {|m|
# ...
}
klass
//}

この場合も生成したクラスを返します。
ブロックの実行は 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 (9293.0)

新しく名前の付いていない superclass のサブクラスを生成します。

...ていない superclass のサブクラスを生成します。

名前のないクラスは、最初に名前を求める際に代入されている定数名を検
索し、見つかった定数名をクラス名とします。

//emlist[例][ruby]{
p foo = Class.new # => #<Class:0x401b90f8>
p fo...
...コードと同じです。

//emlist[例][ruby]{
klass = Class.new(superclass)
klass.module_eval {|m|
# ...
}
klass
//}

この場合も生成したクラスを返します。
ブロックの実行は 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"
//}...

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

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

...れを返します。

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

//emlist[例][ruby]{
dog = Struct.new("Dog", :name, :age)
fred = dog.new("fred", 5)
fred.age = 6
printf "name:%s age:%d", fred.name, fred.age
#=> "name:fred age:6" を...
...体を定義します。
Ruby
3.1 では互換性に影響のある使い方をしたときに警告が出るため、
従来の挙動を期待する構造体には明示的に false を指定してください。

//emlist[例][ruby]{
Point = Struct.new(:x, :...
...: z)
//}

//emlist[警告が出る例][ruby]{
Point = Struct.new(:x, :y)
Point.new(x: 1, y: 2) # => #<struct Point x={:x=>1, :y=>2}, y=nil>
# warning: Passing only keyword arguments to Struct#initialize will behave differently from Ruby 3.2. Please use a Hash literal like ....

Data.define(*args) {|subclass| block } -> Class (281.0)

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

...ッドが定義されています。

//emlist[例][ruby]{
Dog = Data.define(:name, :age)
fred = Dog.new("Fred", 5)
p fred.name # => "Fred"
p fred.age # => 5
//}

メンバの値を書き換えることはできません。

//emlist[例][ruby]{
Dog = Data.define(:name, :age)
fred = Dog.new("Fred",...
...ンバを持たないサブクラスも定義可能です。
以下のように、パターンマッチに利用できます。

//emlist[例][ruby]{
class
HTTPFetcher
Response = Data.define(:body)
NotFound = Data.define

def get(url)
# ダミーの実装
if url == "http://example.com/...
...[ruby]{
Customer = Data.define(:name, :address) do
def greeting
"Hello #{name}!"
end
end
p Customer.new("Dave", "123 Main").greeting # => "Hello Dave!"
//}

なお、Dataのサブクラスのインスタンスを生成する際にオプション引数を使用したいときは、
initialize
...

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

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

...れを返します。

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

//emlist[例][ruby]{
dog = Struct.new("Dog", :name, :age)
fred = dog.new("fred", 5)
fred.age = 6
printf "name:%s age:%d", fred.name, fred.age
#=> "name:fred age:6" を...
...体を定義します。
Ruby
3.1 では互換性に影響のある使い方をしたときに警告が出るため、
従来の挙動を期待する構造体には明示的に false を指定してください。

//emlist[例][ruby]{
Point = Struct.new(:x, :...
...: z)
//}

//emlist[警告が出る例][ruby]{
Point = Struct.new(:x, :y)
Point.new(x: 1, y: 2) # => #<struct Point x={:x=>1, :y=>2}, y=nil>
# warning: Passing only keyword arguments to Struct#initialize will behave differently from Ruby 3.2. Please use a Hash literal like ....

絞り込み条件を変える

Data.define(*args) -> Class (181.0)

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

...ッドが定義されています。

//emlist[例][ruby]{
Dog = Data.define(:name, :age)
fred = Dog.new("Fred", 5)
p fred.name # => "Fred"
p fred.age # => 5
//}

メンバの値を書き換えることはできません。

//emlist[例][ruby]{
Dog = Data.define(:name, :age)
fred = Dog.new("Fred",...
...ンバを持たないサブクラスも定義可能です。
以下のように、パターンマッチに利用できます。

//emlist[例][ruby]{
class
HTTPFetcher
Response = Data.define(:body)
NotFound = Data.define

def get(url)
# ダミーの実装
if url == "http://example.com/...
...[ruby]{
Customer = Data.define(:name, :address) do
def greeting
"Hello #{name}!"
end
end
p Customer.new("Dave", "123 Main").greeting # => "Hello Dave!"
//}

なお、Dataのサブクラスのインスタンスを生成する際にオプション引数を使用したいときは、
initialize
...

Object.yaml_tag(tag) -> () (61.0)

クラスと tag の間を関連付けます。

... Ruby のオブジェクトに
変換したりその逆をしたりすることができます。

@param tag 対象のクラスに関連付けるタグの文字列

=== Example
require 'psych'

class
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 class
Psych.dump(Foo.new(3), STDOUT)
# =>
# --- !<tag:example.com,2013:foo>
# x: 3

#...

ERB.new(str, safe_level=NOT_GIVEN, trim_mode=NOT_GIVEN, eoutvar=NOT_GIVEN, trim_mode: nil, eoutvar: &#39;_erbout&#39;) -> ERB (55.0)

eRubyスクリプト から ERB オブジェクトを生成して返します。

...eRubyスクリプト から ERB オブジェクトを生成して返します。

@param str eRubyスクリプトを表す文字列
@param safe_level eRubyスクリプトが実行されるときのセーフレベル
@param trim_mode 整形の挙動を変更するオプション

@param eoutvar eRuby...
...字列。eRuby スクリプトの中でさらに ERB を使うときに変更
します。通常は指定する必要はありません。

Ruby
2.6.0 から位置引数での safe_level, trim_mode, eoutvar の指定は非推奨です。
Ruby
3.2 で削除されまし...
...さい。

//emlist[例][ruby]{
require "erb"

# build data class
class
Listings
PRODUCT = { :name => "Chicken Fried Steak",
:desc => "A well messages pattie, breaded and fried.",
:cost => 9.95 }

attr_reader :product, :price

def initialize( product = "", price = "...

ERB.new(str, safe_level=nil, trim_mode=nil, eoutvar=&#39;_erbout&#39;) -> ERB (55.0)

eRubyスクリプト から ERB オブジェクトを生成して返します。

...eRubyスクリプト から ERB オブジェクトを生成して返します。

@param str eRubyスクリプトを表す文字列
@param safe_level eRubyスクリプトが実行されるときのセーフレベル
@param trim_mode 整形の挙動を変更するオプション

@param eoutvar eRuby...
...字列。eRuby スクリプトの中でさらに ERB を使うときに変更
します。通常は指定する必要はありません。

Ruby
2.6.0 から位置引数での safe_level, trim_mode, eoutvar の指定は非推奨です。
Ruby
3.2 で削除されまし...
...さい。

//emlist[例][ruby]{
require "erb"

# build data class
class
Listings
PRODUCT = { :name => "Chicken Fried Steak",
:desc => "A well messages pattie, breaded and fried.",
:cost => 9.95 }

attr_reader :product, :price

def initialize( product = "", price = "...

JSON.create_id -> String (43.0)

json_create メソッドで使用するクラスを決定するために使用する値を返します。

...は "json_class" です。

//emlist[例][ruby]{
require "json"

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,
"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">
//}...

絞り込み条件を変える

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

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

...なければ nil です。

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

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

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

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

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

...なければ nil です。

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

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

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

ERB.new(str, trim_mode: nil, eoutvar: &#39;_erbout&#39;) -> ERB (31.0)

eRubyスクリプト から ERB オブジェクトを生成して返します。

...eRubyスクリプト から ERB オブジェクトを生成して返します。

@param str eRubyスクリプトを表す文字列
@param trim_mode 整形の挙動を変更するオプション

@param eoutvar eRubyスクリプトの中で出力をためていく変数の名前を表す文...
...字列。eRuby スクリプトの中でさらに ERB を使うときに変更
します。通常は指定する必要はありません。


//emlist[例][ruby]{
require "erb"

# build data class
class
Listings
PRODUCT = { :name => "Chicken Fried Steak",
:desc =...
...> "A well messages pattie, breaded and fried.",
:cost => 9.95 }

attr_reader :product, :price

def initialize( product = "", price = "" )
@product = product
@price = price
end

def build
b = binding
# create and run templates, filling member data variables
E...

File.path(filename) -> String (25.0)

指定されたファイル名を文字列で返します。filename が文字列でない場合は、to_path メソッドを呼びます。

...ル名を表す文字列か to_path メソッドが定義されたオブジェクトを指定します。

//emlist[例][ruby]{
require 'pathname'

class
MyPath
def initialize(path)
@path = path
end
def to_path
File.absolute_path(@path)
end
end

File.path("/dev/null") # => "...