Created
November 17, 2010 10:52
-
-
Save simonbaird/703255 to your computer and use it in GitHub Desktop.
write ruby with python style whitespace (a haml hack)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'haml' | |
# Put a - char on each line in just the right place. | |
# (Need to remove blank lines too, they cause problems) | |
haml_source = File.read($0).gsub(/\n+/,"\n").gsub(/^\s*/,'\&-') | |
begin | |
# Run the file with haml | |
Haml::Engine.new(haml_source).render | |
rescue Exception => e | |
# Spit out the haml source to make it easier to find your error | |
puts "FAILED!\n#{haml_source}" | |
raise e | |
end | |
exit | |
# Here's a one line version :) | |
# Haml::Engine.new(File.read($0).gsub(/\n+/,"\n").gsub(/^\s*/,'\&-')).render; exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby -r hamrbl | |
# Just a test of hamlrb.rb | |
module Singing | |
def sing(count=1) | |
count.times do |i| | |
if %w[tweety bigbird].include? @name.downcase | |
puts "#{@name}: 'tweet tweet #{i+1}'" | |
else | |
puts "#{@name}: 'la la la #{i+1}'" | |
class Singer | |
include Singing | |
def initialize(name) | |
@name = name | |
# comments are okay :) | |
Singer.new('Thom').sing(2) | |
Singer.new('Tweety').sing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ls -l | |
total 24 | |
-rw-r--r-- 1 simon staff 232 17 Nov 21:13 hamrbl.rb | |
-rwxr-xr-x 1 simon staff 433 17 Nov 21:56 test.rb | |
$ ./test.rb | |
Thom: 'la la la 1' | |
Thom: 'la la la 2' | |
Tweety: 'tweet tweet 1' | |
$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See blog post at http://simonbaird.blogspot.com/2010/11/hamrbl-write-ruby-with-python-style.html