[Courses] [Ruby] Lesson 0: Installing, References,
and your first homework assignment
Laurel Fan
laurel.fan at gmail.com
Thu Nov 17 02:21:05 EST 2005
reply to chantal
On 11/9/05, Chantal Rosmuller <chantal at antenna.nl> wrote:
> Hi I'm Chantal
Hi!
> Well anyway, here's my
> homework, some code I wrote myself, I needed to configure an interface
> on an openbsd system for a whole ip range and it had to put something
> like this:
>
> inet alias x.y.z.1 255.255.255.0
> inet alias x.y.z.2 255.255.255.0
>
> etc
>
> I didn't want to type all of that so I wrote ipspitter.rb:
> It basically takes a number to start with and adds one a number of times
> and keeps printing that, together with a string for the first three
> parts of the ip address and some text if you want
This looks great, a few comments (for you and everyone else) in-line:
> #!/usr/bin/ruby
>
> puts "This is ipspitter"
> puts "Which number do you want to start the range with?"
>
> count = gets.to_i
puts prints a string (I think it stands for "PUT String"), and gets
(conincidentally enough) gets a string. It defaults to getting it
from standard input/output (which is the terminal if you're running
the program in the shell without redirecting input), but you can make
it put/get input from any IO object, like a file, network socket, etc
(see the IO documentation for more).
> sum = count
It doesn't look like you use sum.. maybe leftover from a previous
incarnation of the program?
> puts " How many ip addresses do you want?"
>
> number = gets.to_i
> number = number - 1
> puts "Enter the first three parts of the ip-address (for example
> 192.168.4. )"
> range = gets.chomp
Chomp is like chomp in perl; it takes the newline off the end of the
string if it happens to have one.
> puts "Do you want to put some text in front of the ip-address? Put it here"
> textbefore = gets.chomp
>
> puts "Do you want to put some text after the ip-address? Put it here"
> textafter = gets
>
> puts textbefore + range + count.to_s + textafter
>
> number.times do
> count +=1
> sum += count
> puts textbefore + range + count.to_s + textafter
> end
There are a few ways to rearrange the loop that might (or might not be) clearer.
First, if you take away the 'number = number-1' line, I think you
could also take away the first puts outside of the loop, and do:
number.times do
puts textbefore + range + count.to_s + textafter
count +=1
sum += count
end
Or, you could do something like this (which we'll learn about mostly
in Lesson 2 in the Expressions chapter of the book):
start = count
end = count + number - 1
start.upto(end) do |current_number|
puts textbefore + range + current_number.to_s + textafter
end
(I haven't tried it so there's probably an off by one error)
The upto method (in the Integer class) does the stuff in the do/end
for every integer from start up to end.
Also, a different way to create strings: This code would do the same thing:
puts "#{textbefore}#{range}#{count}#{textafter}"
(and it would call the count.to_s for you). We'll see this first in Lesson 1.
--
Laurel Fan
http://dreadnought.gorgorg.org
More information about the Courses
mailing list