Hi, this blog is no longer maintained, my new blog is here

Ruby On Rails and a Conning Israeli entrepreneur

Showing posts with label class. Show all posts
Showing posts with label class. Show all posts

Dynamically adding methods

This is divine, ruby is too great to be true. no explanation needed.


  1. class Object  
  2.   def def(method_name, &block)  
  3.     (class << selfself end).send(:define_method, method_name, block)  
  4.   end  
  5. end  
  6.   
  7. x = Object.new  
  8.   
  9. string = "This is a test"  
  10. x.def(:elad_says) {puts "Elad says: " + string}  
  11.   
  12. x.elad_says  

get classes of a class

  1. def get_all_subclasses_of(string)
  2. raise ArgumentError.new("#{string} is not an AR Class") if eval(string).class.ancestors
  3. .collect {|e| e.to_s.downcase}.include? ("ActiveRecord::Base")
  4. return ObjectSpace.subclasses_of(eval(string))
  5. end


The Web Ask eizesus.com

Subscribe

    follow me on Twitter

    Twiters Around

    About Me

    My photo
    I am a web developer for more than 9 years, managed, cried, coded, designed and made money in this industry. now trying to do it again.

    Labels