LoginSignup
0
0

More than 1 year has passed since last update.

Ruby逆引きメモ

Last updated at Posted at 2022-05-25

Rubyにおける逆引きメモです。「やりたいこと→メソッド」という形にしています。最後に公式ドキュメントへのリンクを付けてますので合わせて参考にしてください。

オブジェクトを調べる

サンプル用コード

class User
  def initialize()
    @name = "bob"
  end
end

user = User.new

オブジェクトが持つインスタンス変数名を調べる
p user.instance_variables
#=> [:@name]
  • 変数名をシンボルの配列として返します。
  • 初期化していない変数は返されません。

オブジェクトのインスタンス変数の値を調べる
p user.instance_variable_get("@name")
#=> "bob"
  • 引数にはシンボルも使えます。(:@name)

オブジェクトのクラスを調べる
"Hello".class
#=> String

文字列を操作する

文字列を文字ごとに切り離して配列にする
"Hello".chars
#=> ["h", "e", "l", "l", "o"]

配列

配列の要素数を取得する
array = [1, 2, 3]
array.size
#=> 3

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0