0
0

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 3 years have passed since last update.

Rubyで繰り返し処理の中で任意の要素数で処理を差し込む方法

Last updated at Posted at 2020-07-07

この記事で書くこと :pen_ballpoint:

Rubyで数字を整数かどうか判定する文を書こうとしたら、
すぐに自分の中から出てこなかったので
その内容を学びとしてアウトプットする。

繰り返し処理の中で任意の要素数で処理を差し込む方法について
学びとしてアウトプットする。

(記述する処理と内容が適さないため、修正しました。 :bow:  )

特定の数字の倍数かどうかで処理を分ける :deciduous_tree:


pry(main)> test_num = 1
=> 1
pry(main)> test_num.to_f
=> 1.0
pry(main)> test_num.to_f.to_s.split('.')[1] == '0'
=> true

pry(main)> test_num2 = 1.05
=> 1.05
pry(main)> test_num2.to_f
=> 1.05
pry(main)> test_num2.to_f.to_s.split('.')[1] == '0'
=> false

(0..1000).to_a.each do |num|
  if num.fdiv(100).to_s.split('.')[1] == '0' && num != 0
    p '100の倍数です'
  end
end
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?