Skip to content

Commit f6cb264

Browse files
author
Leon Rische
committed
code for chapter 3 in ruby
1 parent 1e2d586 commit f6cb264

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

03_recursion/ruby/01_countdown.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def countdown(i)
2+
puts i
3+
# base case
4+
if i <= 0
5+
return
6+
# recursive case
7+
else
8+
countdown(i - 1)
9+
end
10+
end
11+
12+
countdown(5)

03_recursion/ruby/02_greet.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def greet2(name)
2+
puts "how are you, #{name}?"
3+
end
4+
5+
def bye
6+
print "ok bye!"
7+
end
8+
9+
def greet(name)
10+
puts "hello, #{name}!"
11+
greet2(name)
12+
puts "getting ready to say bye..."
13+
bye
14+
end
15+
16+
greet("adit")

03_recursion/ruby/03_factorial.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def fact(x)
2+
if x == 1
3+
1
4+
else
5+
x * fact(x - 1)
6+
end
7+
end
8+
9+
puts fact(5)

0 commit comments

Comments
 (0)