Solution: rectangle in Ruby (CLI and STDIN = Standard IO)
Exercise: rectangle
examples/ruby/rectangle.rb
width = 23 length = 19 area = width * length circumference = 2 * (width + length) puts "area: #{area}" puts "circumference: #{circumference}"
examples/ruby/rectangle_cli.rb
if ARGV.size != 2 puts "#{$PROGRAM_NAME} WIDTH LENGTH" exit 1 end width = ARGV[0].to_f length = ARGV[1].to_f area = width * length circumference = 2 * (width + length) puts "area: #{area}" puts "circumference: #{circumference}"
examples/ruby/rectangle_stdin.rb
print "Width:" width = gets.strip.to_f print "Length:" length = gets.strip.to_f area = width * length circumference = 2 * (width + length) puts "area: #{area}" puts "circumference: #{circumference}"
Published on 2021-06-21
If you have any comments or questions, feel free to post them on the source of this page in GitHub. Source on GitHub.
Comment on this post