Ruby Problem

Hello,

i don´t know how to build a loop for the following problem:

class TestClass
def input(num1,num2,num3,num4,num5,num6) #<-- does that make sense? Is it right?
@num1=num1
@num2=num2
@num3=num3
@num4=num4
@num5=num5
@num6=num6
end

attr_accessor :num1
attr_accessor :num2
attr_accessor :num3
attr_accessor :num4
attr_accessor :num5
attr_accessor :num6
end

ob = TestClass::new

6.times do |i|
print "Enter number #(i+1): "
ob.num[i]=gets.to_f #<—this is my problem
end

Maybe you´ve found some other mistakes or things i should do better. I´d be very happy if you´ld help me ´cause i´m very new to ruby. Thank you very much.

Kind regards,
Stefano

···


freenet Email-Office:
E-Mail, Kalender und 30 MB virtuelle Festplatte.
Jetzt testen unter www.freenet.de/tipp/emo

stefanocheri@freenet.de wrote:

Hello,

i don?t know how to build a loop for the following problem:

class TestClass
def input(num1,num2,num3,num4,num5,num6) #<-- does that make
sense? Is it right?

This is definitely a code smell. As far as possible, semantic
information (in this case, the numbers) should not be part of variable
names. In other words, if you have a numbered sequence of variables, you
need to be using an array.

class TestClass

attr_accessor :num

def input(*args) # input a variable number of arguments, put them in an
# array called args

if args.length != 6
raise “six arguments expected”
end

now we can capture the arguments in an instance variable

@num = args # note that @num is now a six-element array

end

end #class TestClass

ob = TestClass.new

0.upto(5) do |i| # ‘times’ doesn’t feel like I’m iterating over a
# sequence
print "Enter number #{i+1}: "
ob.num[i] = gets.chomp.to_f # now ob.num will respond to [i] since
# it is an array
end

The thing to note is that variables like num1, num2, num3… don’t mean
anything more to Ruby than foo, bar, baz… . num[3] is the third (well,
fourth since counting starts from 0) element of an array of numbers, but
num3 could just as easily be the 10th numeric variable you have.

martin

p.s. I find it useful coding style to have my attr_accessor statements
right at the beginning of the class definition - it documents the
pubilc instance variables more clearly.

Sorry - I was careless and didn’t test the code…

class TestClass

attr_accessor :num

def input(*args) # input a variable number of arguments, put them in an
# array called args

that should be

def initialize(*args)

when you call new on your class, that calls the initialize method you
provide.

if args.length != 6
raise “six arguments expected”
end

now we can capture the arguments in an instance variable

@num = args # note that @num is now a six-element array

end

end #class TestClass

ob = TestClass.new

Also, you seem to be doing something different here - if you’re setting
the values for num in the loop, you don’t need to pass anything to
initialize. So your code should look like

class TestCase
attr_accessor :num

def initialize
@num =
end
end

ob = TestClass.new
print "How many numbers to input? "
n = gets.chomp.to_i
0.upto(n-1) do |i|
print "Please enter number #{i+1}: "
ob.num[i] = gets.chomp.to_f
end

You could make the whole thing a method of TestCase:

class TestCase
attr_accessor :num

def initialize
@num =
end

def input_nums
print "How many numbers to input? "
n = gets.chomp.to_i
0.upto(n-1) do |i|
print "Please enter number #{i+1}: "
num[i] = gets.chomp.to_f
end
end
end

ob = TestClass.new
ob.input_nums

martin

···

Martin DeMello martindemello@yahoo.com wrote: