Take the 2-minute tour ×

In ruby I can say (I picked up Mongoid only for example)

class MyItem
  include Mongoid::Document
  include Mongoid::Timestamps


  def method1(some_type)
    raise "Not symbol" unless some_type.is_a?(Symbol)
    raise "Unsupported some_type (#{some_type})" unless [:some_type1, :some_type2, :some_type3].include?(some_type)
    min_time_to_update = 60*60
    full_method_name = "#{some_type}_another_method".to_sym()


and call it

result = MyItem.first.method1(:some_type2)

Here the method send is being used to call a type's method by its name. But what if I want to do the following

  def method1(type, arg1, arg2)
     #check if it's a correct type....
     # type might be either MyItem1 or MyItem2 or anything that has a method `method123`
     "#{type}".method123(arg1, arg2)

How can I do that? How can I get access to the type by its name to call its method?

share|improve this question
What is list_type? What is MyItem#first? Explain all methods or local variables that are not standard. Also, in Ruby, the word "class" is used. Using "type" makes it difficult to immediately understand, and puts extra burden to the reader. –  sawa Jan 19 '13 at 9:46

1 Answer 1

up vote 2 down vote accepted

type being a string eg. 'MyItem1' or 'MyItem2'?

Try Object.const_get(type).method123(arg1, arg2).

share|improve this answer

Your Answer


