# Thread: My first Ruby script!

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()

# Calculate area and print it
print &quot;The area is &quot;, area, &quot;\n&quot;
exit
end

def rectangle()
# Get base and height
print &quot;Base: &quot;
base = STDIN.gets
base = Float(base.chop!)
print &quot;Height: &quot;
height = STDIN.gets
height = Float(height.chop!)

# Calculate area and print it
area = base * height
print &quot;The area is &quot;, area, &quot;\n&quot;
exit
end

def triangle()
# Get base and height
print &quot;Base: &quot;
base = STDIN.gets
base = Float(base.chop!)
print &quot;Height: &quot;
height = STDIN.gets
height = Float(height.chop!)

# Calculate the area and print it
area = (base * height) / 2
print &quot;The area is &quot;, area, &quot;\n&quot;
exit
end

# Start the script

print &quot;Area Calculator!\n\n&quot;
print &quot;Select a shape:\n&quot;
print &quot;1 Circle\n&quot;
print &quot;2 Rectangle\n&quot;
print &quot;3 Triangle\n\n&quot;
print &quot;? &quot;

# Get input - Which shape?
choice = STDIN.gets
# Catch exceptions when input is String or Float - exit
begin
choice = Integer(choice.chop!)
rescue
print &quot;Invalid selection\n&quot;
exit
end

print &quot;\n&quot;

# 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 &quot;Invalid Selection\n&quot;```
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. ## Re: My first Ruby script!

Some general remarks:
- Input: for a string, just do:
Code:
```print &quot;Enter a string: &quot;
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 &quot;Invalid number. Exiting&quot;
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 &quot;blah\n&quot;. It will change line automatically. Very useful

- You can write a variable like this too:
Code:
`puts &quot;The value of variable foo is #{foo} and the value of bar is #{bar}&quot;`

- 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. ## 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 &lt; 2
puts &quot;Usage: #{\$0} &lt;hostname&gt; &lt;highest port to scan&gt;&quot;
exit 1
end

begin
limit = Integer(ARGV[1])
rescue
puts &quot;Error: port number must be an integer&quot;
exit 1
end

for x in 1 .. limit
begin
t = TCPSocket.new(ARGV[0], x)
rescue
next
end
puts &quot;#{x} is open&quot;
t.close
end```

4. ## Re: My first Ruby script!

Those are some sweet hints, man! *Now I don't have to do print &quot;Hello, World!\n&quot; 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. ## Re: My first Ruby script!

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

6. ## Re: My first Ruby script!

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

7. ## 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. ## Re: My first Ruby script!

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

9. ## Re: My first Ruby script!

Yeah, yeah right ;D

10. ## 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.

#### Posting Permissions

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