23
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Ruby]to_iメソッドとto_sメソッドについて

Posted at

#to_i と to_s が覚えられない
と言うか覚える気が無いようなのでメモっとく

  • to_iメソッドが文字→数値
  • to_sメソッドが数値→文字

to_iの例

number = "10"
puts number + 90       #エラーになる
puts number.to_i + 90  #OK

to_sの例

number = 10
puts number + "個のりんご"      #エラーになる
puts number.to_s + "個のりんご" #OK

##参考文献
ruby 数値と文字列を連結させる方法「to_s」「to_i」メソッド

23
17
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
23
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?