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

【ruby】『ruby入門(メモ6)』メソッドで恐竜fizzbuzzを作ってみよう(windows10+ruby2.3.3)

Posted at

0.あると望ましい(どうでもいい)前提知識

超久しぶりの投稿!
気分転換のためにfizzbuzzを作成してみました。
インドミナスレックス(IndominusRex):ヴェロキラプトルとティラノザウルスを融合させた恐竜。
インドラプトル(Indoraptor):インドミナスレックスとヴェロキラプトルを融合させた恐竜。

この知識を前提に変形fizzbuzzを作ってみた。
最後にインドラプトルが表示されていれば成功!

1.コード

method_fizzbuzz.rb
def act_dino(dinosor)
	if dinosor == 'Tyrannosaurus'
		puts 'Bite!!'
	elsif dinosor == 'Stegosaurus'
		puts 'Waving the tail!'
	else
		puts '???'
	end				
end

def FizzBuzz_dinosor(n)
	#インドラプトル
	if n % 45 == 0
		'Indoraptor'
	#インドミナスレックス	
	elsif n % 15 == 0
		'IndominusRex'
	#ティラノサウルス
	elsif n % 5 == 0
		'Tyrannosaurus'
	#ヴェロキラプトル	
	elsif n % 3 == 0
		'Velociraptor'
	else
		n.to_s
	end
end

act_dino("tiger")
act_dino("Stegosaurus")

n=0
while n < 45 do
	n += 1
	puts FizzBuzz_dinosor(n)
end

2.実行結果

c:\ruby_pg>ruby method_fizzbuzz.rb
???
Waving the tail!
1
2
Velociraptor
4
Tyrannosaurus
Velociraptor
7
8
Velociraptor
Tyrannosaurus
11
Velociraptor
13
14
IndominusRex
16
17
Velociraptor
19
Tyrannosaurus
Velociraptor
22
23
Velociraptor
Tyrannosaurus
26
Velociraptor
28
29
IndominusRex
31
32
Velociraptor
34
Tyrannosaurus
Velociraptor
37
38
Velociraptor
Tyrannosaurus
41
Velociraptor
43
44
Indoraptor

c:\ruby_pg>
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?