On Thursday, January 27, 2011 08:52:03 pm Ted Flethuseo wrote:
> David Masover wrote in post #978005:
> > On Thursday, January 27, 2011 02:55:24 pm Ted Flethuseo wrote:
> >> Ah that's too bad... I knew I could put some default values for x and y
> >> and z.. getting the same effect sort of...
> >> 
> >> but this isn't a solution if the methods that I want to "overload" (I
> >> call it
> >> overload, not sure what the proper term should be) are
> >> completely different, and are not differentiated simply by different
> >> values.
> > 
> > If they're not differentiated by different values, how were you
> > expecting
> > overloading to work? If the problem is that you need the behavior to be
> > different, I think default values still work -- for example:
> > 
> > def sum_or_inverse a, b=nil
> > 
> >   if b.nil?
> >   
> >     -a
> >   
> >   else
> >   
> >     a+b
> >   
> >   end
> > 
> > end
> > 
> > But I don't really know what you were asking.
> > I agree with Jess. We need more details.
> 
> Ok.. I didn't want to post so much code but you asked for it. I am
> trying to read a file, with a number of different methods. The methods
> allow you to interpret the file as an input and output matrices, input
> only or both but with only a number of rows.
> 
> class CSVFile
>   attr_accessor :inFile
>   attr_accessor :file_data
>   attr_accessor :in_data
>   attr_accessor :out_data
>   attr_reader :count
> 
>   def initialize(file)
>     @inFile = file
>     @file_data = []
>     @count = 0
>     @in_data = []
>     @out_data = []
>   end
> 
>   def read_data
>     if inFile.nil? == false
>       @count = 0
>       File.foreach(inFile) do |line|
>         arr = line.chomp.split(',')
>         float_array = arr.map { |x| x.to_f }
>         file_data.push(float_array)
>         @count = @count + 1
>       end
>     end
>     return file_data
>   end
> 
>   def read_data(num_in, num_out)
>     if inFile.nil? == false
>       @count = 0
>       File.foreach(inFile) do |line|
>         arr = line.chomp.split(',')
>         float_array = arr.map { |x| x.to_f }
>         arr_in = []
>         arr_out = []
>         for i in 0...num_in
>           arr_in << float_array[i]
>         end
> 
>         in_data.push(arr_in)
>         for j in num_in...(num_in+num_out)
>           arr_out << float_array[j]
>         end
>         out_data.push(arr_out)
>         @count = @count + 1
>       end
>     end
>     return file_data
>   end
> 
>   def read_data(num_in, num_out, num_lines)
>     if inFile.nil? == false
>       @count = 0
>       File.foreach(inFile) do |line|
>         if @count >= num_lines
>           break;
>         end
> 
>         arr = line.chomp.split(',')
>         float_array = arr.map { |x| x.to_f }
>         arr_in = []
>         arr_out = []
>         for i in 0...num_in
>           arr_in << float_array[i]
>         end
> 
>         in_data.push(arr_in)
>         for j in num_in...(num_in+num_out)
>           arr_out << float_array[j]
>         end
>         out_data.push(arr_out)
>         @count = @count + 1
>       end
>     end
>     return file_data
>   end
> end

Yeah, there's a lot of duplicate stuff there. Also, why are you doing this 
when there's a CSV class in the standard library?

http://ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html

Anyway, it looks like you're copying and pasting a _lot_ between those 
methods, so it also looks like if you DRYed it up a bit, it'd work just fine 
with default arguments. Also, there's a bunch of ways this could be more 
idiomatic... Here's a rough guess at what you're trying to do, at least for 
this one method:

def read_data num_in=nil, num_out=nil, num_lines=nil
  # This does what you said above. Are you sure that's right?
  # Shouldn't it raise an error or something?
  return file_data if inFile.nil?

  # If I were you, I'd have the 'count' method pass through to
  # file_data.length or something, so this could be a local...
  @count = 0

  # Object-oriented programming means letting the object
  # figure out what to do, instead of calling functions
  # with that object. The File.foreach was a code smell to me.
  inFile.each_line do |line|
    break if num_lines && @count >= num_lines

    # Note that, unlike a proper CSV parser, this won't handle escaped commas.
    arr = line.chomp.split(',').map(&:to_f)

    if num_in
      # An each would be better than a foreach IMO, but if you want
      # the best speed and readability, why not rely on stuff arrays
      # already know how to do instead of writing the loop yourself?
      arr_in.push arr[0...num_in]

      if num_out
        # num_in and num_out are both counts, right?
        arr_out.push arr[num_in,num_out]
      end
    end

    @count += 1
  end

  file_data
end


Warning: I haven't tested the above code at all, but it _looks_ like it can 
replace the read_data methods you wrote. Let me know. Still, I mostly did it 
as an exercise, because if you were planning to actually work with CSVs, I'd 
think your code should start with:

require 'csv'