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.