Patching with git
March 27th, 2008
I patched the Ruby on Rails bundle for TextMate to allow footnotes to catch Haml views, which might be in files named something.haml or something.html.haml.
The change itself is trivial; making a patch using git was something new and, as it turns out, five steps:
1. Grab the source with git
Wonderland$ git clone git://github.com/drnic/ruby-on-rails-tmbundle.git
2. Make your changes
3. Commit locally, from the root of the source treee
Wonderland$ git commit -a -m 'add support for html.haml templates' Wonderland$ git show
4. Generate a patch file
Wonderland$ git format-patch origin
5. Email the patch file
Syntax Highlighting with the Code Highlighter Macro for Mephisto
March 24th, 2008
Dan Webb has an alternative to the built-in syntax highlighting provided by Mephisto that uses JavaScript to markup your code by using regular expressions to tokenize the code. Nifty, and based on this DHTML behavior.
Here is a summary of the install:
- Use svn to pull from http://svn.danwebb.net/external/rails/plugins/filtered_column_code_highlighter/trunk/ into the vendor/plugins directory
- Do either of:
- Copy the assets/*.js to your public/javascripts directory
- rake install (copies to themes/site-1/javascripts)
- Create a new CSS stylesheet (codehighlight.css) with the syntax coloring you want; I used this as a start.
- Add the following to your layout.liquid template
{{ 'codehighlight' | stylesheet }}
{{ 'code_highlighter' | javascript }}
{{ 'html' | javascript }}
{{ 'ruby' | javascript }}
{{ 'css' | javascript }}
{{ 'javascript' | javascript }}
Now the following markup:
<filter:jscode lang=”javascript”>document.someScriptThing = “BOO”</filter:jscode>
Generates:
document.someScriptThing = "BOO"
define_method
March 23rd, 2008
While we were waiting for March 20th RubyJax meeting to get started, Steve Bristol entertained us with a Ruby quiz. One of the questions was particularly interesting as it had many possible answers. I will paraphrase the question as:
“How many ways are there to add a method to an existing class in Ruby?”
I have cataloged the answers that were tossed out as well as a few I just added.
1 . Open the class and add a new method
class String
def method_one
puts 'method 1'
end
end
"abcde".method_one
2 . Create a singleton method on an instance
s = "12345"
def s.method_two
puts 'method 2'
end
s.method_two
3 . Use Kernel#method_missing
class String
def method_missing(name, *args)
super unless name == :method_three
puts 'method 3'
end
end
"abcde".method_three
4 . Use Module#define_method
class String
def create_method(name, &block)
self.class.send(:define_method, name, block)
end
end
"abcde".create_method(:method_four) { puts 'method 4' }
"12345".method_four
5 . A variation of #1 : include a module in the class
module ExtraMethods
def method_five
puts 'method 5'
end
end
class String
include ExtraMethods
end
"abcde".method_five
6 . A variation of #4 : use Object#instance_eval
String.instance_eval("define_method(:method_six) { puts 'method 6' }")
"abcde".method_six
7 . We could use Kernel#eval to execute any of the above as well; here we will emulate instance_eval with a binding
class String
def self.get_binding
binding
end
end
eval("define_method(:method_seven) { puts 'method 7' }", String.get_binding)
"abcde".method_seven
Any others?