Method question

S

Stuart Clarke

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
 
P

Pascal J. Bourguignon

Stuart Clarke said:
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.
 
D

David A. Black

Hi --

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
 
S

steve

Stuart said:
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.
 
M

Mirko Viviani

...
if field3.length >=3D 100
changer(field3)
end
def changer(fieldData)
splitData =3D 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 said:
Stuart said:
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 =3D data[0]
field2 =3D data[1]
field3 =3D data[2]
if field3.length < 100
field3 =3D field3
else
changer(field3)
end
field4 =3D data[3]
field5 =3D data[4]
if field5.length < 100
field5 =3D field5
else
changer(field5)
end
end


def changer(fieldData)
splitData =3D 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 =3D 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=3Dfield3 and field5=3Dfield5 is not terribly
useful. A better way of doing this would be

def processer
field1 =3D data[0]
field2 =3D data[1]
field3 =3D if data[2].length < 100
data[2]
else
changer(data[2])
end
field4 =3D data[3]
field5 =3D 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=3Ddata

Then go back and change field3 and 5 if we need to - note the comparison
needs to change as well.

field3=3Dchanger(data[2]) if data[2].length !< 100
field5=3Dchanger(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 err= or
as you need an end for the do and an end for the def. But, the each bloc= k
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=3Ddata
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 leve= l
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 s= o
what other checks are necessary.

Hope this helps

Steve.


--=20
-- Mirko Viviani --
GPG-PGP Public Key: 0xE4E8FAB1
Fingerprint: 14D3 A373 E926 7737 DF32 502B A4C4 1CE2 E4E8 FAB1
***********************************************
"=93Machines take me by surprise with great frequency.=94 A. Turing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top