Skip to content

Commit 85d0063

Browse files
authored
Add CLI and Fibonacci scripts, and implement Rectangle area calculation (#129)
Introduce a command-line interface for message printing, a Fibonacci sequence generator, and a Rectangle class with area calculation functionality. ## Summary by Sourcery Introduce three standalone Ruby scripts: a CLI utility for message printing, a Fibonacci sequence generator, and a Rectangle class under an Area module for computing area. New Features: - Add a command-line interface script for printing a customizable message multiple times - Implement a Fibonacci sequence generator script that prompts for input and outputs the sequence - Introduce an Area module and Rectangle class with an area calculation method and demonstration
2 parents 39e1e6d + 91828b2 commit 85d0063

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

Ruby/cli.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# cli.rb
2+
require 'optparse'
3+
4+
options = { times: 1 }
5+
OptionParser.new do |opts|
6+
opts.banner = "Usage: cli.rb [options]"
7+
8+
opts.on("-nN", "--number=N", Integer, "Number of times") do |n|
9+
options[:times] = n
10+
end
11+
12+
opts.on("-mMSG", "--message=MSG", "Message to print") do |m|
13+
options[:message] = m
14+
end
15+
end.parse!
16+
17+
msg = options[:message] || "Hello"
18+
options[:times].times { puts msg }

Ruby/fibonacci.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# fibonacci.rb
2+
def fib(n)
3+
a, b = 0, 1
4+
n.times { a, b = b, a + b }
5+
a
6+
end
7+
8+
print "How many Fibonacci numbers? "
9+
m = gets.to_i
10+
(0...m).each { |i| puts "#{i}: #{fib(i)}" }

Ruby/shapes.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# shapes.rb
2+
module Area
3+
def area
4+
raise NotImplementedError
5+
end
6+
end
7+
8+
class Rectangle
9+
include Area
10+
attr_reader :w, :h
11+
def initialize(w, h); @w = w; @h = h; end
12+
def area; w * h; end
13+
end
14+
15+
r = Rectangle.new(3, 4)
16+
puts "Rectangle area = #{r.area}"

0 commit comments

Comments
 (0)