Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Ruby/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# cli.rb
require 'optparse'

options = { times: 1 }
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Consider validating input for negative or zero values for 'times'.

Without validation, negative or zero values for '-n'/'--number' may cause the script to fail silently or act unpredictably. Please add a check to ensure 'times' is a positive integer.

OptionParser.new do |opts|
opts.banner = "Usage: cli.rb [options]"

opts.on("-nN", "--number=N", Integer, "Number of times") do |n|
options[:times] = n
end

opts.on("-mMSG", "--message=MSG", "Message to print") do |m|
options[:message] = m
end
end.parse!

msg = options[:message] || "Hello"
options[:times].times { puts msg }
10 changes: 10 additions & 0 deletions Ruby/fibonacci.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# fibonacci.rb
def fib(n)
a, b = 0, 1
n.times { a, b = b, a + b }
a
end

print "How many Fibonacci numbers? "
m = gets.to_i
(0...m).each { |i| puts "#{i}: #{fib(i)}" }
16 changes: 16 additions & 0 deletions Ruby/shapes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# shapes.rb
module Area
def area
raise NotImplementedError
end
end

class Rectangle
include Area
attr_reader :w, :h
def initialize(w, h); @w = w; @h = h; end
def area; w * h; end
end

r = Rectangle.new(3, 4)
puts "Rectangle area = #{r.area}"