Forum: Ruby Run some method based on some condition

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5d042621c7c90a43d2776db19eca2023?d=identicon&s=25 Ken Mitchell (kennyd)
on 2007-04-06 00:18
(Received via mailing list)
Say I wanted to run a method based on a condition like so:

class Klass
  def initialize
    a = '1st'
    b = '2nd'
    c = '3rd'
  end

  def a
      a
  end

  def b
    b
  end

  def c
    c
  end
end

ob = Klass.new
['a','b','c'].do {|x|  ob. **here I would want to get at #a and #b and
#c**

or maybe better explained like this

var = 'a'

puts ob.** ???? **

Or if you know perl
$foo = {bar => "baz"};
$baz = "bar";

puts $foo->{$baz};

---> baz

Sorry to be so confusing.




Thanks,

Ken Mitchell
--

Farmers Wireless
Office: (256)638-4040 x 224
Mobile: (256)899-3391
Fax:    (256)638-2110
kmitchell@farmerswireless.com



CONFIDENTIALITY NOTICE: This e-mail and any attachment to it may contain
confidential and legally privileged information. This information is
intended only for the recipient named above. If you are not the intended
recipient, take notice that any disclosure, copying, distribution or
taking of any action based upon this information is prohibited by law.
If you have received this e-mail in error, please return it to the
sender and delete this copy from your system. Thank you.
6d3c187a8b3ef53b08e3e7e8572c4fea?d=identicon&s=25 Jeremy McAnally (Guest)
on 2007-04-06 00:31
(Received via mailing list)
Something like this would work:

irb(main):008:0> x = ['chop!', 'chop!', 'chop!']
=> ["chop!", "chop!", "chop!"]
irb(main):009:0> y = "This is a string!!!"
=> "This is a string!!!"
irb(main):010:0> x.each {|method| y.send(method)}
=> ["chop!", "chop!", "chop!"]
irb(main):011:0> y
=> "This is a string"

The send method allows you to do what you're asking. :)

--Jeremy

On 4/5/07, Ken Mitchell <kmitchell@farmerswireless.com> wrote:
>    def a
>  end
>  Or if you know perl
>
>  Fax:    (256)638-2110
> this copy from your system. Thank you.
>
>
>
>


--
http://www.jeremymcanally.com/

My free Ruby e-book:
http://www.humblelittlerubybook.com/book/

My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/
5d042621c7c90a43d2776db19eca2023?d=identicon&s=25 Ken Mitchell (kennyd)
on 2007-04-06 00:36
(Received via mailing list)
Yes that does exactly what I wanted.

['a','b','c'].do {|x|  ob.send(x)}
5d042621c7c90a43d2776db19eca2023?d=identicon&s=25 Ken Mitchell (kennyd)
on 2007-04-06 01:49
(Received via mailing list)
Now I need to do something similar, but probably more difficult.

class Klass

  class SubKlass1
     def initialize
     end
  end

  class SubKlass2
    def initialize
    end
  end

end

foo = "SubKlass1"

bar = Klass::send(foo).new **That is wrong, because method SubKlass1
doesn't exist in Klass::Class.  But I think you get my point.

On Fri, 2007-04-06 at 07:30 +0900, Jeremy McAnally wrote:

>
> >      a = '1st'
> >    end
> >
> >  ---> baz
> >  --
> > confidential and legally privileged information. This information is
>
Thanks,

Ken Mitchell
--

Farmers Wireless
Office: (256)638-4040 x 224
Mobile: (256)899-3391
Fax:    (256)638-2110
kmitchell@farmerswireless.com



CONFIDENTIALITY NOTICE: This e-mail and any attachment to it may contain
confidential and legally privileged information. This information is
intended only for the recipient named above. If you are not the intended
recipient, take notice that any disclosure, copying, distribution or
taking of any action based upon this information is prohibited by law.
If you have received this e-mail in error, please return it to the
sender and delete this copy from your system. Thank you.
Caf38c89d40443a858741b61ac6d82de?d=identicon&s=25 Dan Zwell (Guest)
on 2007-04-06 03:55
(Received via mailing list)
Have a look at Module#const_get. I think this is what you wanted:
>> bar = Klass.const_get(foo).new
=> #<Klass::SubKlass1:0x2b5a3216e470>

Dan
This topic is locked and can not be replied to.