Skip to content

Instantly share code, notes, and snippets.

@krisleech
Created February 23, 2012 15:50
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save krisleech/1893380 to your computer and use it in GitHub Desktop.
Save krisleech/1893380 to your computer and use it in GitHub Desktop.
A Micro Gem for commenting out lines of Ruby/Erb
hide do          
          _     _     _            
    /\  /(_) __| | __| | ___ _ __  
   / /_/ / |/ _` |/ _` |/ _ \ '_ \ 
  / __  /| | (_| | (_| |  __/ | | |
  \/ /_/ |_|\__,_|\__,_|\___|_| |_|
end

HIDE Micro Gem

It provides a way to comment out code in Ruby

It adds a method to Kernel which might be considered bad practice.

It wants to considered for the award for smallest DSL ever (however, claiming to be a DSL might be pushing it!)

hide do
  raise 'I do not want this to happen'
end
<% hide do %>
  <p>Not working yet</p>
<% end %>

Its just a bit of fun and is a way for me to try out Mirco gems as suggested by Jeff Kreeftmeijer (http://jeffkreeftmeijer.com/2011/microgems-five-minute-rubygems)

Gem::Specification.new do |s|
s.name = 'hide'
s.summary = ''
s.description = ''
s.version = '0.0.1'
s.platform = Gem::Platform::RUBY
s.files = ['hide.rb']
s.require_path = '.'
s.author = 'Kris Leech'
s.email = ''
s.homepage = 'http://teamcoding.com'
s.test_file = 'hide_spec.rb'
s.add_development_dependency('rspec', ["~> 2.8"])
end
module Kernel
def noop(&block)
# do nothing
end
alias :hide :noop
end
require File.expand_path('hide')
describe Kernel do
it 'provides a noop for a block' do
lambda { hide { raise 'Bad code' } }.should_not raise_error
end
end
@krainboltgreene
Copy link

You could simplify this to:

Gem::Specification.new do |gem|
  gem.name = gem.summary = "hide"
  gem.version = "1.0.0"
  gem.files = ["hide.rb"]
  gem.require_path = "."
  gem.authors = ["Kris Leech"]
  gem.add_development_dependency("rspec", "~> 3.1")
end

module Kernel
  def hide(&block)
    # Don't bother!
  end
end

if defined?(RSpec)
  describe Kernel do
    describe "#hide" do
      it "doesn't evaluate" do
        expect { hide { raise "Bad code" } }.not_to raise_error
      end
    end
  end
end

And now it's a single file.

@tylercollier
Copy link

Is there much advantage in a single file? As it is, I like the README, which helps to clarify what the gem does and does not do (and is easy to read on github). I'm truly asking here, as I was thinking of doing my own microgem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment