Add a new method with define_method : define_method « Method « Ruby






Add a new method with define_method


class String
  define_method('last') do |n|
   self[-n, n]
  end
end
p "Here's a string.".last(7)               # => "string."

 








Related examples in the same category

1.define_method method allows you to create methods on the fly.
2.Metaprogramming with String Evaluations