ライブラリ
- ビルトイン (4)
検索結果
-
Data
. define(*args) -> Class (131.0) -
Data クラスに新しいサブクラスを作って、それを返します。
...
Data クラスに新しいサブクラスを作って、それを返します。
サブクラスでは値オブジェクトのメンバに対するアクセスメソッドが定義されています。
//emlist[例][ruby]{
Dog = Data.define(:name, :age)
fred = Dog.new("Fred", 5)
p fred.name # =>......Dog = Data.define(:name, :age)
fred = Dog.new("Fred", 5)
fred.age = 6 # => NoMethodError
//}
メンバを持たないサブクラスも定義可能です。
以下のように、パターンマッチに利用できます。
//emlist[例][ruby]{
class HTTPFetcher
Response = Data.define(:body)
N......url)
fetcher = HTTPFetcher.new
case fetcher.get(url)
in HTTPFetcher::Response(body)
body
in HTTPFetcher::NotFound
:NotFound
end
end
p fetch("http://example.com/") # => "Current time is 2023-01-10 10:00:53 +0900"
p fetch("http://example.com/404") # => :NotFound
//}
@param arg... -
Data
. define(*args) {|subclass| block } -> Class (131.0) -
Data クラスに新しいサブクラスを作って、それを返します。
...
Data クラスに新しいサブクラスを作って、それを返します。
サブクラスでは値オブジェクトのメンバに対するアクセスメソッドが定義されています。
//emlist[例][ruby]{
Dog = Data.define(:name, :age)
fred = Dog.new("Fred", 5)
p fred.name # =>......Dog = Data.define(:name, :age)
fred = Dog.new("Fred", 5)
fred.age = 6 # => NoMethodError
//}
メンバを持たないサブクラスも定義可能です。
以下のように、パターンマッチに利用できます。
//emlist[例][ruby]{
class HTTPFetcher
Response = Data.define(:body)
N......url)
fetcher = HTTPFetcher.new
case fetcher.get(url)
in HTTPFetcher::Response(body)
body
in HTTPFetcher::NotFound
:NotFound
end
end
p fetch("http://example.com/") # => "Current time is 2023-01-10 10:00:53 +0900"
p fetch("http://example.com/404") # => :NotFound
//}
@param arg...