Hi --

On Wed, 29 Jul 2009, Stuart Clarke wrote:

> Hi all,
>
> Struggling a little with the concept of an exercise I am doing.
>
> I have two Ruby methods, the first does a simple string replace the
> second converts data to something else if the criteria is satisfied. See
> example below
>
> def processer
> field1 = data[0]
> field2 = data[1]
> field3 = data[2]
> if field3.length < 100
>  field3 = field3
> else
>  changer(field3)
> end
> field4 = data[3]
> field5 = data[4]
> if field5.length < 100
>  field5 = field5
> else
>  changer(field5)
> end
> end
>
>
> def changer(fieldData)
> splitData = fieldData.split(",")
> splitData.each do |output|
>  p output
> end
>
> What I would like to happen is when field3 for example is longer than
> 100 and its data is passed to def charger, I want that method (def
> charger) to split the field on all commas and then return data to
> field3.
>
> I could just put something like field3 = output, however when I get
> other fields for example field5 this will not work and therefore I need
> something generic that will give the data to the correct field.
>
> I hope this makes sense. Any help is much appreciated

You could do something like:

   field3 = changer(field3) if field3.size >= 100

and then:

   def changer(string)
     string.split(',')
   end

Or just eliminate changer and do the split inline.

I suspect there's more to your program than you're showing, since
you're just assigning to local variables and discarding them, but
it seems like doing the change conditionally should work. Don't do
that field3 = field3 thing; that makes no sense.


David

-- 
David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com
Q: What's the best way to get a really solid knowledge of Ruby?
A: Come to our Ruby training in Edison, New Jersey, September 14-17!
    Instructors: David A. Black and Erik Kastner
    More info and registration: http://rubyurl.com/vmzN