--0016368e2f4e594fee046fe63858 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable ... >if field3.length >= 100 > changer(field3) >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. But changer returns nil (just output something)... 2009/7/29 steve <zyzygy / telstra.com> > Stuart Clarke wrote: > >> Hi all, >> > Hi Stuart > > >> 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 >> >> Regards >> >> Stuart >> > > First off, assigning field3=field3 and field5=field5 is not terribly > useful. A better way of doing this would be > > def processer > field1 = data[0] > field2 = data[1] > field3 = if data[2].length < 100 > data[2] > else > changer(data[2]) > end > field4 = data[3] > field5 = if data[4].length < 100 > data[4] > else > changer(data[4]) > end > end > > Ruby can do parallel assignment so we can change the assignment to > > field1,field2,field3,field4,field5=data > > Then go back and change field3 and 5 if we need to - note the comparison > needs to change as well. > > field3=changer(data[2]) if data[2].length !< 100 > field5=changer(data[4)) if data[4].length !< 100 > > now for two repeats its probably not worth making a new method, but for 3 > I'd be tempted and for 4 I'd certainly write another method. Lets do it > anyway for practice. > > def check_n_change(x) > if x.length<100 > x > else > changer(x) > end > end > > The method returns the value of the last expression evaluated. > > Your original changer method was almost right. First you had a syntax error > as you need an end for the do and an end for the def. But, the each block > is not needed anyway. so changer becomes > > def changer(fieldData) > fieldData.split(',') > end > > and the whole program is thus > > def processer > field1, field2, field3, field4, field5=data > check_n_change(field3) > check_n_change(field5) > end > > def check_n_change(x) > if x.length<100 > x > else > changer(x) > end > end > > def changer(fieldData) > fieldData.split(',') > end > > Of course you could roll changer back up into check_n_change at this level > of complexity. > > Now, what would happen if data had more than 5 fields or fewer? what if > field3 was a number rather than a string? You know your input data and so > what other checks are necessary. > > Hope this helps > > Steve. > > > > > > > > > -- -- Mirko Viviani -- GPG-PGP Public Key: 0xE4E8FAB1 Fingerprint: 14D3 A373 E926 7737 DF32 502B A4C4 1CE2 E4E8 FAB1 *********************************************** "Ůachines take me by surprise with great frequency.A. Turing --0016368e2f4e594fee046fe63858--