Examples Verified (100%)
標準ライブラリ型
T-RubyはRubyの標準ライブラリに対する型定義を提供します。このリファレンスでは、一般的に使用されるstdlibモジュールとクラスの型付きインターフェースを文書化します。
ステータス
現在のサポート
T-Rubyの標準ライブラリ型カバレッジは活発に成長しています。ここにリストされている型は現在のリリースで利用可能です。追加のstdlib型は定期的に追加されています。
ファイルシステム
File
型安全性を持つファイルI/O操作。
# ファイル読み込み
def read_config(path: String): String | nil
return nil unless File.exist?(path)
File.read(path)
end
# ファイル書き込み
def save_data(path: String, content: String): void
File.write(path, content)
end
# ファイル操作
def process_file(path: String): Array<String>
File.readlines(path).map(&:strip)
end
型シグネチャ:
File.exist?(path: String): BooleanFile.read(path: String): StringFile.write(path: String, content: String): IntegerFile.readlines(path: String): Array<String>File.open(path: String, mode: String): FileFile.open(path: String, mode: String, &block: Proc<File, void>): voidFile.delete(*paths: String): IntegerFile.rename(old: String, new: String): IntegerFile.size(path: String): IntegerFile.directory?(path: String): BooleanFile.file?(path: String): Boolean
Dir
ディレクトリ操作。
# ディレクトリ内容をリスト
def list_files(dir: String): Array<String>
Dir.entries(dir)
end
# ファイル検索
def find_ruby_files(dir: String): Array<String>
Dir.glob("#{dir}/**/*.rb")
end
# ディレクトリ操作
def create_dirs(path: String): void
Dir.mkdir(path) unless Dir.exist?(path)
end
型シグネチャ:
Dir.entries(path: String): Array<String>Dir.glob(pattern: String): Array<String>Dir.exist?(path: String): BooleanDir.mkdir(path: String): voidDir.rmdir(path: String): voidDir.pwd: StringDir.chdir(path: String): voidDir.home: String
FileUtils
高レベルファイル操作。
require 'fileutils'
# ファイルコピー
def backup_file(source: String, dest: String): void
FileUtils.cp(source, dest)
end
# ディレクトリ削除
def clean_temp(dir: String): void
FileUtils.rm_rf(dir)
end
# ディレクトリツリー作成
def setup_structure(path: String): void
FileUtils.mkdir_p(path)
end
型シグネチャ:
FileUtils.cp(src: String, dest: String): voidFileUtils.mv(src: String, dest: String): voidFileUtils.rm(path: String | Array<String>): voidFileUtils.rm_rf(path: String | Array<String>): voidFileUtils.mkdir_p(path: String): voidFileUtils.touch(path: String | Array<String>): void
JSON
JSONパースと生成。
require 'json'
# JSONパース
def load_config(path: String): Hash<String, Any>
content = File.read(path)
JSON.parse(content)
end
# JSON生成
def save_data(path: String, data: Hash<String, Any>): void
json = JSON.generate(data)
File.write(path, json)
end
# プリティプリント
def pretty_json(data: Hash<String, Any>): String
JSON.pretty_generate(data)
end
型シグネチャ:
JSON.parse(source: String): AnyJSON.generate(obj: Any): StringJSON.pretty_generate(obj: Any): StringJSON.dump(obj: Any, io: IO): voidJSON.load(source: String | IO): Any
型付きJSON
型安全なJSON操作には、明示的な型を定義します:
type JSONPrimitive = String | Integer | Float | Boolean | nil
type JSONArray = Array<JSONValue>
type JSONObject = Hash<String, JSONValue>
type JSONValue = JSONPrimitive | JSONArray | JSONObject
def parse_json(source: String): JSONValue
JSON.parse(source) as JSONValue
end
def parse_object(source: String): JSONObject
result = JSON.parse(source)
result.is_a?(Hash) ? result : {}
end
YAML
YAMLパースと生成。
require 'yaml'
# YAMLロード
def load_yaml(path: String): Any
YAML.load_file(path)
end
# YAML生成
def save_yaml(path: String, data: Any): void
File.write(path, YAML.dump(data))
end
# 型付きYAMLロード
def load_config(path: String): Hash<String, Any>
YAML.load_file(path) as Hash<String, Any>
end
型シグネチャ:
YAML.load(source: String): AnyYAML.load_file(path: String): AnyYAML.dump(obj: Any): StringYAML.safe_load(source: String): Any
Net::HTTP
HTTPクライアント操作。
require 'net/http'
# GETリクエスト
def fetch_data(url: String): String
uri = URI(url)
Net::HTTP.get(uri)
end
# POSTリクエスト
def send_data(url: String, body: String): String
uri = URI(url)
Net::HTTP.post(uri, body, { 'Content-Type' => 'application/json' }).body
end
# 完全なリクエスト
def api_call(url: String): Hash<String, Any> | nil
uri = URI(url)
response = Net::HTTP.get_response(uri)
return nil unless response.is_a?(Net::HTTPSuccess)
JSON.parse(response.body)
end
型シグネチャ:
Net::HTTP.get(uri: URI): StringNet::HTTP.post(uri: URI, data: String, headers: Hash<String, String>?): Net::HTTPResponseNet::HTTP.get_response(uri: URI): Net::HTTPResponseNet::HTTPResponse#code: StringNet::HTTPResponse#body: StringNet::HTTPResponse#[](key: String): String?
URI
URIパースと操作。
require 'uri'
# URIパース
def parse_url(url: String): URI::HTTP | URI::HTTPS
URI.parse(url) as URI::HTTP
end
# URIビルド
def build_api_url(host: String, path: String, query: Hash<String, String>): String
uri = URI::HTTP.build(
host: host,
path: path,
query: URI.encode_www_form(query)
)
uri.to_s
end
型シグネチャ:
URI.parse(uri: String): URI::GenericURI.encode_www_form(params: Hash<String, String>): StringURI::HTTP.build(params: Hash<Symbol, String>): URI::HTTPURI#host: String?URI#path: String?URI#query: String?URI#to_s: String
CSV
CSVファイル処理。
require 'csv'
# CSV読み込み
def load_csv(path: String): Array<Array<String>>
CSV.read(path)
end
# ヘッダー付き読み込み
def load_users(path: String): Array<Hash<String, String>>
result: Array<Hash<String, String>> = []
CSV.foreach(path, headers: true) do |row|
result << row.to_h
end
result
end
# CSV書き込み
def save_csv(path: String, data: Array<Array<String>>): void
CSV.open(path, 'w') do |csv|
data.each { |row| csv << row }
end
end
型シグネチャ:
CSV.read(path: String): Array<Array<String>>CSV.foreach(path: String, options: Hash<Symbol, Any>?, &block: Proc<CSV::Row, void>): voidCSV.open(path: String, mode: String, &block: Proc<CSV, void>): voidCSV#<<(row: Array<String>): voidCSV::Row#to_h: Hash<String, String>
Logger
ロギング機能。
require 'logger'
# ロガー作成
def setup_logger(path: String): Logger
Logger.new(path)
end
# メッセージロギング
def log_event(logger: Logger, message: String): void
logger.info(message)
end
# 異なるログレベル
def log_error(logger: Logger, error: Exception): void
logger.error(error.message)
logger.debug(error.backtrace.join("\n"))
end
型シグネチャ:
Logger.new(logdev: String | IO): LoggerLogger#debug(message: String): voidLogger#info(message: String): voidLogger#warn(message: String): voidLogger#error(message: String): voidLogger#fatal(message: String): voidLogger#level=(severity: Integer): void
Pathname
オブジェクト指向のパス操作。
require 'pathname'
# パス操作
def process_directory(path: String): Array<String>
dir = Pathname.new(path)
dir.children.map { |child| child.to_s }
end
# パスクエリ
def find_config(dir: String): Pathname | nil
path = Pathname.new(dir)
config = path / "config.yml"
config.exist? ? config : nil
end
型シグネチャ:
Pathname.new(path: String): PathnamePathname#/(other: String): PathnamePathname#exist?: BooleanPathname#directory?: BooleanPathname#file?: BooleanPathname#children: Array<Pathname>Pathname#basename: PathnamePathname#dirname: PathnamePathname#extname: StringPathname#to_s: String
StringIO
メモリ内文字列ストリーム。
require 'stringio'
# 文字列バッファ作成
def build_output: String
buffer = StringIO.new
buffer.puts "Header"
buffer.puts "Content"
buffer.string
end
# 文字列から読み込み
def parse_data(content: String): Array<String>
io = StringIO.new(content)
io.readlines
end
型シグネチャ:
StringIO.new(string: String?): StringIOStringIO#puts(str: String): voidStringIO#write(str: String): IntegerStringIO#read: StringStringIO#readlines: Array<String>StringIO#string: String
Set
一意な要素のコレクション。
require 'set'
# セットの作成と使用
def unique_tags(posts: Array<Hash<Symbol, Array<String>>>): Set<String>
tags = Set<String>.new
posts.each do |post|
post[:tags].each { |tag| tags.add(tag) }
end
tags
end
# セット演算
def common_interests(users: Array<Hash<Symbol, Set<String>>>): Set<String>
return Set.new if users.empty?
users.map { |u| u[:interests] }.reduce { |a, b| a & b }
end
型シグネチャ:
Set.new(enum: Array<T>?): Set<T>Set#add(item: T): Set<T>Set#delete(item: T): Set<T>Set#include?(item: T): BooleanSet#size: IntegerSet#empty?: BooleanSet#to_a: Array<T>Set#&(other: Set<T>): Set<T>- 積集合Set#|(other: Set<T>): Set<T>- 和集合Set#-(other: Set<T>): Set<T>- 差集合
OpenStruct
動的属性オブジェクト。
require 'ostruct'
# 構造体作成
def create_config: OpenStruct
OpenStruct.new(
host: "localhost",
port: 3000,
debug: true
)
end
# プロパティアクセス
def get_host(config: OpenStruct): String
config.host as String
end
型シグネチャ:
OpenStruct.new(hash: Hash<Symbol, Any>?): OpenStructOpenStruct#[](key: Symbol): AnyOpenStruct#[]=(key: Symbol, value: Any): voidOpenStruct#to_h: Hash<Symbol, Any>
Benchmark
パフォーマンス測定。
require 'benchmark'
# 実行時間を測定
def measure_operation: Float
result = Benchmark.measure do
1_000_000.times { |i| i * 2 }
end
result.real
end
# 実装を比較
def compare_methods: void
Benchmark.bm do |x|
x.report("map") { (1..1000).map { |i| i * 2 } }
x.report("each") { (1..1000).each { |i| i * 2 } }
end
end
型シグネチャ:
Benchmark.measure(&block: Proc<void>): Benchmark::TmsBenchmark.bm(&block: Proc<Benchmark::Job, void>): voidBenchmark::Tms#real: FloatBenchmark::Tms#total: Float
ERB
埋め込みRubyテンプレート。
require 'erb'
# テンプレートレンダリング
def render_template(template: String, name: String): String
erb = ERB.new(template)
erb.result(binding)
end
# ファイルから
def render_email(user_name: String): String
template = File.read("templates/email.erb")
ERB.new(template).result(binding)
end
型シグネチャ:
ERB.new(str: String): ERBERB#result(binding: Binding?): String
Base64
Base64エンコードとデコード。
require 'base64'
# エンコード
def encode_data(data: String): String
Base64.strict_encode64(data)
end
# デコード
def decode_data(encoded: String): String
Base64.strict_decode64(encoded)
end
# URL安全なエンコード
def url_safe_token(data: String): String
Base64.urlsafe_encode64(data)
end
型シグネチャ:
Base64.encode64(str: String): StringBase64.decode64(str: String): StringBase64.strict_encode64(str: String): StringBase64.strict_decode64(str: String): StringBase64.urlsafe_encode64(str: String): StringBase64.urlsafe_decode64(str: String): String
Digest
ハッシュ関数(MD5、SHA等)。
require 'digest'
# MD5ハッシュ
def md5_hash(data: String): String
Digest::MD5.hexdigest(data)
end
# SHA256ハッシュ
def sha256_hash(data: String): String
Digest::SHA256.hexdigest(data)
end
# ファイルチェックサム
def file_checksum(path: String): String
Digest::SHA256.file(path).hexdigest
end
型シグネチャ:
Digest::MD5.hexdigest(str: String): StringDigest::SHA1.hexdigest(str: String): StringDigest::SHA256.hexdigest(str: String): StringDigest::SHA256.file(path: String): Digest::SHA256Digest::Base#hexdigest: String
SecureRandom
暗号学的に安全なランダム値。
require 'securerandom'
# ランダムhex
def generate_token: String
SecureRandom.hex(32)
end
# UUID
def generate_uuid: String
SecureRandom.uuid
end
# ランダムバイト
def random_bytes(size: Integer): String
SecureRandom.bytes(size)
end
型シグネチャ:
SecureRandom.hex(n: Integer?): StringSecureRandom.uuid: StringSecureRandom.bytes(n: Integer): StringSecureRandom.random_number(n: Integer | Float?): Integer | Float
Timeout
タイムアウト付きコード実行。
require 'timeout'
# タイムアウト付き
def fetch_with_timeout(url: String): String | nil
begin
Timeout.timeout(5) do
Net::HTTP.get(URI(url))
end
rescue Timeout::Error
nil
end
end
型シグネチャ:
Timeout.timeout(sec: Integer | Float, &block: Proc<T>): T
カバレッジマップ
stdlibモジュールサポートのクイックリファレンステーブル:
| モジュール | ステータス | 備考 |
|---|---|---|
File | ✅ サポート | 完全な型カバレッジ |
Dir | ✅ サポート | 完全な型カバレッジ |
FileUtils | ✅ サポート | 一般的なメソッドに型付き |
JSON | ✅ サポート | 安全のため型キャストを使用 |
YAML | ✅ サポート | 安全のため型キャストを使用 |
Net::HTTP | ✅ サポート | 基本操作 |
URI | ✅ サポート | パースとビルド |
CSV | ✅ サポート | 読み込みと書き込み |
Logger | ✅ サポート | すべてのログレベル |
Pathname | ✅ サポート | パス操作 |
StringIO | ✅ サポート | ストリーム操作 |
Set | ✅ サポート | ジェネリックSet<T> |
OpenStruct | ⚠️ 部分的 | 動的属性はAnyを使用 |
Benchmark | ✅ サポート | パフォーマンス測定 |
ERB | ✅ サポート | テンプレートレンダリング |
Base64 | ✅ サポート | エンコード/デコード |
Digest | ✅ サポート | ハッシュ関数 |
SecureRandom | ✅ サポート | 安全なランダム生成 |
Timeout | ✅ サポート | タイムアウト実行 |
Socket | 🚧 計画中 | 近日追加予定 |
Thread | 🚧 計画中 | 近日追加予定 |
Queue | 🚧 計画中 | 近日追加予定 |
Stdlib型の使用
インポートと使用
# stdlibモジュールをインポート
require 'json'
require 'fileutils'
# 型安全に使用
def process_config(path: String): Hash<String, Any> | nil
return nil unless File.exist?(path)
content = File.read(path)
JSON.parse(content) as Hash<String, Any>
end
型キャスト
動的stdlibモジュールには型キャストを使用します:
# 安全なキャスト
def load_users(path: String): Array<Hash<String, String>>
raw_data = JSON.parse(File.read(path))
if raw_data.is_a?(Array)
raw_data as Array<Hash<String, String>>
else
[]
end
end
カスタムラッパー
より良い安全性のために型付きラッパーを作成します:
class Config
@data: Hash<String, Any>
def initialize(path: String): void
@data = YAML.load_file(path) as Hash<String, Any>
end
def get_string(key: String): String?
value = @data[key]
value.is_a?(String) ? value : nil
end
def get_int(key: String): Integer?
value = @data[key]
value.is_a?(Integer) ? value : nil
end
end
ベストプラクティス
- 動的結果を型キャスト - JSON/YAMLパースには
asを使用 - 型安全なラッパーを作成 - 型付きインターフェースで動的ライブラリをラップ
- nilケースを処理 - stdlibメソッドは頻繁にnilを返す
- ユニオン型を使用 - 多くのstdlibメソッドは複数の戻り型を持つ
- 外部データを検証 - パースされたJSON/YAML型を信頼しない
Stdlib型への貢献
stdlibカバレッジの拡大に協力したいですか?新しい標準ライブラリ型定義の追加について詳しくは、コントリビューションガイドを参照してください。