Method question

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

Stuart C. [email protected] 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.

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

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 [email protected]

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.