I want to expand a method to the String class in Sinatra, in the erb file, do something like
<%= 'some string'.my_method %>
but I don’t know how to put the definition code:
String.class_eval do
def my_mythod
some_code
end
end
By the way I’m using the sinatra modular coding style
I tend to stick code like this in its own file, under the lib/ext folder. Then, you can require this file from your Sinatra app.
Under lib/ext/string.rb:
Then add the following to your Sinatra app, assuming your base class file is inside the lib folder:
I’d be interested to see what over people think on this as well.