On Fri, Jan 20, 2012 at 1:15 AM, Doug Jolley <ddjolley / gmail.com> wrote:
> I need to dynamically call a class method (i.e., unless there is some
> other way, the name of the class method should be contained in a
> variable). =A0I have come up with the way illustrated in the example show=
n
> below. =A0The program is in the file, 'test1'. =A0The relevant method is =
in
> the file, 'Test1a.rb'. I use 'eval' which (I believe) is frowned upon.
> Does anyone have a better way of accomplishing this objective? =A0Thanks
> for any input.
>
> =A0 =A0 ... doug
>
> $ ls
> test1 =A0Test1a.rb
> $ cat Test1a.rb
> class Test1a
> =A0def Test1a.howdy()
> =A0 =A0return 'Hello, world!'
> =A0end
> end
> $ cat test1
> #!/usr/bin/ruby
>
> require './Test1a.rb'
> myvar=3D'howdy'
> puts(eval("Test1a.#{myvar}()"))
> $ ./test1
> Hello, world!
> $
>
> --

Use #send, e.g.

myvar =3D "howdy"
Test1a.send(myvar)