# Method question

**URL:** <https://www.ruby-forum.com/t/method-question/172892>\
**Category:** Ruby\
**Created:** [July 29, 2009, 4:19pm UTC](https://www.ruby-forum.com/t/method-question/172892 "2009-07-29T16:19:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [July 29, 2009, 4:19pm UTC](https://www.ruby-forum.com/t/method-question/172892/1 "2009-07-29T16:19:55Z")

</div>

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

Regards

Stuart

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [July 29, 2009, 4:55pm UTC](https://www.ruby-forum.com/t/method-question/172892/2 "2009-07-29T16:55:46Z")

</div>

Stuart C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [July 29, 2009, 5:00pm UTC](https://www.ruby-forum.com/t/method-question/172892/3 "2009-07-29T17:00:27Z")

</div>

Hi –

On Wed, 29 Jul 2009, Stuart C. wrote:

> field2 = data[1]  
> else
> 
> 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

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [July 30, 2009, 8:15am UTC](https://www.ruby-forum.com/t/method-question/172892/4 "2009-07-30T08:15:26Z")

</div>

…

> 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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [July 29, 2009, 5:15pm UTC](https://www.ruby-forum.com/t/method-question/172892/5 "2009-07-29T17:15:40Z")

</div>

Stuart C. wrote:

> Hi all,  
> Hi Stuart  
> field3 = data[2]  
> changer(field5)  
> What I would like to happen is when field3 for example is longer than  
> 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.
