Skip to content

fizzbuzzプログラムを作成#1

Open
hiro23900 wants to merge 1 commit into
mainfrom
fizzbuzz
Open

fizzbuzzプログラムを作成#1
hiro23900 wants to merge 1 commit into
mainfrom
fizzbuzz

Conversation

@hiro23900

Copy link
Copy Markdown
Owner

No description provided.

Comment thread 01.fizzbuzz/fizzbuzz.rb
@@ -0,0 +1,7 @@
(1..20).each { |n|

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rubyのeachにブロックを渡すとき、複数行の場合は{}ではなくdo...endを使うのが慣例です。

(1..20).each do |n|
  # ...
end

1行で書く場合は{}、複数行の場合はdo...endという使い分けを覚えておくと良いです。

Comment thread 01.fizzbuzz/fizzbuzz.rb
@@ -0,0 +1,7 @@
(1..20).each { |n|
(puts "FizzBuzz"; next) if n.modulo(3 * 5) == 0

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

行末に余分なスペースが入っています。エディタの設定で「保存時に行末スペースを削除する」をオンにしておくと、こういったものを自動で防げます。

また、n.modulo(3 * 5)n.modulo(15) と書けますし、% 演算子を使って n % 15 == 0 と書くこともできます。どちらがより読みやすいか意識してみてください。

Comment thread 01.fizzbuzz/fizzbuzz.rb
@@ -0,0 +1,7 @@
(1..20).each { |n|
(puts "FizzBuzz"; next) if n.modulo(3 * 5) == 0
(puts "Buzz"; next) if n.modulo(5) == 0

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2行目と同様、行末に余分なスペースが入っています。

Comment thread 01.fizzbuzz/fizzbuzz.rb
(puts "FizzBuzz"; next) if n.modulo(3 * 5) == 0
(puts "Buzz"; next) if n.modulo(5) == 0
(puts "Fizz"; next) if n.modulo(3) == 0
puts n

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

行末に余分なスペースが入っています。こちらも合わせて取り除いてください。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants