Defining the instance and class methods using define_method


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. class Example
  2. class << self
  3. define_method(:one) { puts 'one' }
  4. define_method(:two) { puts 'two' }
  5. end
  6.  
  7. define_method(:three) { puts 'three' }
  8. define_method(:four) { puts 'four' }
  9. end
  10.  
  11. Example.one #=> one
  12. Example.two #=> two
  13.  
  14. Example.new.three #=> three
  15. Example.new.four #=> four

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.