Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19
My first Ruby script!
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: My first Ruby script!

  1. #1

    My first Ruby script!

    It gives you the choice of calculating the area of a circle, rectangle, or triangle:

    Code:
    #!/usr/bin/env ruby
    
    # The Area Calculator
    # Calculate the area of a variety of shapes
    
    def circle()
     # Get radius
     print "Radius: "
     radius = STDIN.gets
     radius = Float(radius.chop!)
    
     # Calculate area and print it
     area = 3.14 * radius * radius
     print "The area is ", area, "\n"
     exit
    end
    
    def rectangle()
     # Get base and height
     print "Base: "
     base = STDIN.gets
     base = Float(base.chop!)
     print "Height: "
     height = STDIN.gets
     height = Float(height.chop!)
    
     # Calculate area and print it
     area = base * height
     print "The area is ", area, "\n"
     exit
    end
    
    def triangle()
     # Get base and height
     print "Base: "
     base = STDIN.gets
     base = Float(base.chop!)
     print "Height: "
     height = STDIN.gets
     height = Float(height.chop!)
    
     # Calculate the area and print it
     area = (base * height) / 2
     print "The area is ", area, "\n"
     exit
    end
    
    # Start the script
    
    print "Area Calculator!\n\n"
    print "Select a shape:\n"
    print "1 Circle\n"
    print "2 Rectangle\n"
    print "3 Triangle\n\n"
    print "? "
    
    # Get input - Which shape?
    choice = STDIN.gets
    # Catch exceptions when input is String or Float - exit
    begin
     choice = Integer(choice.chop!)
    rescue
     print "Invalid selection\n"
     exit
    end
    
    print "\n"
    
    # Calculate the area of the selection
    # Catch exceptions when input in area calculations
    # is String
    begin
     case choice
      when 1
       circle()
      when 2
       rectangle()
      when 3
       triangle()
     end
    rescue
    end
    
    # This is reached when an exception occurs above
    # or when choice does not equal 1, 2, or 3
    # Otherwise, the shape functions exit after area calculation
    print "Invalid Selection\n"
    What do you think? A little sloppy? Should I add more shapes?

    By the way, is there a default statement in Ruby (for the case statement)? And does anyone know of any good Ruby tutorials? I found like two of them, and they aren't very... super.

  2. #2
    Guest

    Re: My first Ruby script!

    Some general remarks:
    - Input: for a string, just do:
    Code:
    print "Enter a string: "
    str = gets.chomp
    You don't really need the STDIN. Also, prefer chomp to chop: it's safer.

    - When you convert from string to float of integer, do something like this:
    Code:
    begin
     num = Float(gets.chomp)
    rescue
     puts "Invalid number. Exiting"
     exit 1
    end
    - You don't need the empty parenthesis if there are no parameters and/or arguments.

    - the word puts is the equivalent of print "blah\n". It will change line automatically. Very useful

    - You can write a variable like this too:
    Code:
    puts "The value of variable foo is #{foo} and the value of bar is #{bar}"
    This is quite helpful IMO.

    - The default statement in a case is else

    -Tutorial: you are right, there are not so many that are good. But here's one that's pretty good: http://www.ruby-lang.org/~slagell/ruby/index.html

    Also, you can find the modules methods here: http://dev.rubycentral.com/ref/


    Good luck man, and ask me if you need help. And thanks for not being a wuss and trying Ruby out

  3. #3
    Guest

    Re: My first Ruby script!

    Also, here's a small port scanner I made in Ruby (many thanks to jemfinch, I used his algorithm, but it's slower (I can probably fix that)):
    Code:
    #!/usr/bin/env ruby
    
    require 'socket'
    
    if ARGV.length < 2
     puts "Usage: #{$0} <hostname> <highest port to scan>"
     exit 1
    end
    
    begin 
     limit = Integer(ARGV[1])
    rescue
     puts "Error: port number must be an integer"
     exit 1
    end
    
    for x in 1 .. limit
     begin
      t = TCPSocket.new(ARGV[0], x)
     rescue
      next
     end
     puts "#{x} is open"
     t.close
    end

  4. #4

    Re: My first Ruby script!

    Those are some sweet hints, man! *Now I don't have to do print "Hello, World!\n" anymore. *I was wondering why you were using puts instead of print all the time.

    And thanks for the tip on gets. *I was looking for a shortcut like str = STDIN.gets.chomp, but that obviously wouldn't work.

    Should I use chomp or chomp! ? *And why is it safer than chop! ?

    I'm looking forward to reading that tutorial. Thanks.

  5. #5
    Guest

    Re: My first Ruby script!

    Methods that end with an exclamation (!) are applied to the object. So:
    Code:
    irb(main):001:0> a = "Hello\n"
    "Hello\n"
    irb(main):003:0> a.chomp
    "Hello"
    irb(main):004:0> a
    "Hello\n"
    irb(main):005:0> a.chomp!
    "Hello"
    irb(main):006:0> a
    "Hello"
    irb(main):007:0>
    I don't know if you understand? And chomp removes the "\n" (with no arguments), while chop removes the last character:
    Code:
    irb(main):007:0> b = "Hello"
    "Hello"
    irb(main):008:0> b.chop!
    "Hell"
    irb(main):009:0>

  6. #6

    Re: My first Ruby script!

    I understand now. Thanks, you're a big help.

  7. #7
    Guest

    Re: My first Ruby script!

    No problem. I've been advocating Ruby for the 6 past months and you're the first one who's interested.

  8. #8

    Re: My first Ruby script!

    Hey, I was interested, I'm just busy.

  9. #9
    Guest

    Re: My first Ruby script!

    Yeah, yeah right ;D

  10. #10
    Guest

    Re: My first Ruby script!


    No problem. I've been advocating Ruby for the 6 past months and you're the first one who's interested.
    K.... I cave... I'll try it out - any good links to learn this stuff ?

    btw. It looks so damn c00l, please do a PET series on ruby scripting.

Similar Threads

  1. Ruby / Ruby on Rails
    By coltrane in forum Linux - Software, Applications & Programming
    Replies: 3
    Last Post: 06-16-2006, 03:38 PM
  2. for comtux and all you other ruby nutjobs
    By maccorin in forum General Chat
    Replies: 1
    Last Post: 08-22-2005, 01:48 PM
  3. Makeing games with sdl and ruby
    By comtux in forum Linux - Software, Applications & Programming
    Replies: 1
    Last Post: 01-23-2005, 01:22 PM
  4. Ruby
    By Ashcrow in forum Linux - Software, Applications & Programming
    Replies: 4
    Last Post: 12-01-2001, 11:35 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •