Very Basic Example: (notice the <! part !>)
/------------------------------------------------->
class MyName
def first_name(era)
part = "Cassius" if era == "before"
part = "Muhammad" if era == "after"
return part
end
def middle_name(era)
part = "Marcellus" if era == "before"
part = "" if era == "after"
return part
end
def last_name(era)
part = "Clay" if era == "before"
part = "Ali" if era == "after"
return part
end
end
parts = %w"first middle last"
parts.each do |part|
puts part + " name details: " +
MyName.new.<! part !>_name("before") +
" is now " +
MyName.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
MyName.new.send( "#{part}_name", "before" )
···
On Sep 14, 2005, at 7:12 PM, x1 wrote:
parts = %w"first middle last"
parts.each do |part|
puts part + " name details: " +
MyName.new.<! part !>_name("before") +
" is now " +
MyName.new.<! part !>_name("after")
end
A general suggestion on code, unrelated to your question. (I know you said "excuse the logic", but just in case)
def first_name( era )
case era
when "before" then "Cassius"
when "after" then "Muhammad"
end
end
But of course, if you have three methods with almost the same logic, they should probably be a single method for DRY purposes.
···
On Sep 14, 2005, at 7:12 PM, x1 wrote:
def first_name(era)
part = "Cassius" if era == "before"
part = "Muhammad" if era == "after"
return part
end
Absolutely perfect.
I was nearly there.. Thank you!!
···
On 9/14/05, Gavin Kistner <gavin@refinery.com> wrote:
On Sep 14, 2005, at 7:12 PM, x1 wrote:
> parts = %w"first middle last"
> parts.each do |part|
> puts part + " name details: " +
> MyName.new.<! part !>_name("before") +
> " is now " +
> MyName.new.<! part !>_name("after")
> end
MyName.new.send( "#{part}_name", "before" )
I'm completely aware of that and appreciate your help.
Thank you.
···
On 9/14/05, x1 <caldridge@gmail.com> wrote:
Absolutely perfect.
I was nearly there.. Thank you!!
On 9/14/05, Gavin Kistner <gavin@refinery.com> wrote:
> On Sep 14, 2005, at 7:12 PM, x1 wrote:
> > parts = %w"first middle last"
> > parts.each do |part|
> > puts part + " name details: " +
> > MyName.new.<! part !>_name("before") +
> > " is now " +
> > MyName.new.<! part !>_name("after")
> > end
>
> MyName.new.send( "#{part}_name", "before" )
>
>