Add a new method with eval : eval « Development « Ruby






Add a new method with eval


class String
  eval %{def last(n)
           self[-n, n]
         end}
end
"Here's a string.".last(7)               # => "string."

 








Related examples in the same category

1.Use eval to run a statement dynamically
2.Execute an eval in a thread
3.Evaluation with variable
4.eval with variable name