From: pjb@... (Pascal J. Bourguignon) Date: 2009-07-29T23:55:07+09:00 Subject: Re: Method question Stuart Clarke writes: > 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 > if field3.length < 100 > field3 = field3 > else > changer(field3) > end field3=changeIfLongerThan(data[2],100) > if field5.length < 100 > field5 = field5 > else > changer(field5) > end > end field5=changeIfLongerThan(data[4],100) > 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. This is not clear. Do you wan to return an array containing the split fields, or just the first one, or do you want to some way process each fields in turn? In the later case, AFAIK, Ruby doesn't have continuations, so you will have to add control flow yourself. In the first case you can write: def changer(field) # everything is data inside the computer! field.split(",") end def changeIfLongerThan(field,maxLen) if field < maxLen field else changer(field) end end > 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 It wasn't too clear, but I hope my answer will help you make it clearer. -- __Pascal Bourguignon__