0
0

More than 3 years have passed since last update.

【Ruby】to_iメソッドで値を整数に変換する。

Last updated at Posted at 2021-03-08

個人メモです。

to_iを使うと整数に変換できる。

to_i
test = { x: "123", y: "456", z: "789" }
puts test[:x].class  #String
puts test[:x].to_i.class #Integer

classは型を調べるメソッド。


to_i
test = { x: "123", y: "456", z: "789" }
puts test[:x].is_a?(Integer)  #false
puts test[:x].to_i.is_a?(Integer)  #true

.is_a?(型)は指定した型かどうかを調べるメソッド。


次の処理は何を意味しているか?

    per_page = params[:per_page].to_i

シンボルparamsの中のper_pageの値を取得し整数に変換したものを、per_pageに格納している。

つまり、per_pageにはただの整数が入っている。

0
0
2

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