diff --git a/Ruby/cli.rb b/Ruby/cli.rb new file mode 100644 index 0000000..9767938 --- /dev/null +++ b/Ruby/cli.rb @@ -0,0 +1,18 @@ +# cli.rb +require 'optparse' + +options = { times: 1 } +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 } \ No newline at end of file diff --git a/Ruby/fibonacci.rb b/Ruby/fibonacci.rb new file mode 100644 index 0000000..02f374c --- /dev/null +++ b/Ruby/fibonacci.rb @@ -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)}" } \ No newline at end of file diff --git a/Ruby/shapes.rb b/Ruby/shapes.rb new file mode 100644 index 0000000..3f4714d --- /dev/null +++ b/Ruby/shapes.rb @@ -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}" \ No newline at end of file