LoginSignup
0
0

More than 3 years have passed since last update.

ruby: to_iメソッドについて

Last updated at Posted at 2019-07-21

rubyを勉強していてto_iメッドについて学んたことをまとめる。

to_iメソッドの使い方

to_iメソッドは、文字列を10進数の表現と見なして整数に変換します。
文字列の先頭から10進数と見なせる部分を切り取って変換します。
*10進数とは
数値の表現形式のうち、「0」から「9」までの10種類の数字を使って数値を表現する形式

sample.rb
string = '1993'
number = string.to_i
puts number

・結果

1993

10進数と見なせない部分は0を返す
ex)'はじめまして', nullなど

sample.rb
string = 'はじめまして'
number = string.to_i
put number

・結果

0

10進数と10進数と見なせない部分が混合している時

sample.rb
string = '12 apple'
number = string.to_i
put number

・結果

12
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