[Quick Easy Question] Referencing Methods Dynamically?

X

x1

Very Basic Example: (notice the <! part !>)

/------------------------------------------------->
class MyName
=09def first_name(era)
=09=09part =3D "Cassius" if era =3D=3D "before"
=09=09part =3D "Muhammad" if era =3D=3D "after"
=09=09return part
=09end
=09def middle_name(era)
=09=09part =3D "Marcellus" if era =3D=3D "before"
=09=09part =3D "" if era =3D=3D "after"
=09=09return part
=09end
=09def last_name(era)
=09=09part =3D "Clay" if era =3D=3D "before"
=09=09part =3D "Ali" if era =3D=3D "after"
=09=09return part
=09end
end

parts =3D %w"first middle last"
parts.each do |part|
=09puts part + " name details: " +
=09MyName.new.<! part !>_name("before") +
=09" is now " +
=09MyName.new.<! part !>_name("after")
end
/-------------------------------------------------<

Excuse the logic.. The purpose is to display the need for referencing
methods dynamically, with variables being passed.

Any help will be appreciated.

Thx
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top