TextMate bundles for Merb
If you are using TextMate (OS X) or E Text Editor (Windows) then you’ll be dead keen to know there is a TextMate bundle for Merb, and another one for Datamapper.
[This is cross-posted on the new Merbunity community site for Merb]
Currently, you can access the bundles via git:
cd "~/Library/Application Support/TextMate/Bundles" git clone git://github.com/drnic/datamapper-tmbundle.git Datamapper.tmbundle git clone git://github.com/drnic/merb-tmbundle.git Merb.tmbundle
Now “Reload Bundles” or restart TextMate.
You can now select from three Merb syntax/scopes. Press Shift+Alt+Cmd+M to see the options (below)
- If you select Merb (Datamapper) you will have access to the Merb bundle AND the Datamapper bundle.
- If you select Merb (ActiveRecord) you have access to the Merb bundle AND the Ruby on Rails bundle.
- If you select Merb (Sequel) then nothing special happens as there isn’t a Sequel bundle yet. Want to create one?
Currently there is no separation in the Rails bundle for ActiveRecord and non-ActiveRecord snippets and commands, so if you use Merb (ActiveRecord) there will be some snippets + commands that you don’t want from Rails. In future, there may be more separation to help the Merb bundle. Also, TextMate 2 may help this cause.
As always, to learn what snippets are available at any given cursor position, use Ctrl+Cmd+T.
If you have new snippets for any of the bundles, I suggest forking the git repositories, pushing up the changes to github and sending Pull Requests to me (drnic).
Sponsored by Engine Yard
The initial development of this bundle was sponsored by Engine Yard because they care.
Rails bundle
There is also the hughly popular Ruby on Rails bundle for TextMate that you can use. Download from http://railsbundle.com/ or via github.
If you want to learn everything about the new Ruby on Rails bundle for TextMate and daily life with TextMate (short cuts, editing bundles) then I highly recommend the new TextMate for Rails 2 Peepcode:
This recommendation comes from inside information on the quality of the Peepcode… it was written by me, and incorporates the vocal skills of the operatic Geoffrey Grosenbach.
GitHub Badge for your Blog with 100% guarantee of more coolness
The killer app for JavaScript in the 90s was to take a perfectly readable sequence of words - aka “a sentence” - and turn every single character a different colour. You did that, then you went back to doing normal work.
The killer app for JavaScript in the 00s is widgets/badges. Taking perfectly useful stuff from one website - aka “data” - and re-posting it on your blog sidebar.
In both circumstances, across two decades, you did this to look cool. Sure, it never works but you do it anyway.
Thusly, riding high on the world-dominating success of GitHub there seems to be a vacancy in the department of “JavaScript badge for my blog to make me look cool” projects, in the sub-category of “GitHub”.
The first entrant and thus market leader of the “Blog Badges for GitHub” micro-industry is the “GitHub Badge” It is beautifully demonstrated in static image form above, or if you click the image through to the website (or here to my blog and its aesthetically appealing enhancement with said GitHub Badge) you’ll see it live and interactive.
Touch it. Feel it. Press the “Show more” link for hours of entertainment.
Installation
Then get it for yourself. Read the GitHub Badge website or just…
Slap the following into your blog sidebar:
<div id="github-badge"></div> <script type="text/javascript" charset="utf-8"> GITHUB_USERNAME="drnic"; GITHUB_LIST_LENGTH=10; GITHUB_HEAD="div"; // e.g. change to "h2" for wordpress sidebars </script> <script src="http://drnicjavascript.rubyforge.org/github_badge/dist/github-badge-launcher.js" type="text/javascript"></script>
Only GITHUB_USERNAME is a required pre-set variable. The others above show the defaults, and can be changed by being specified.
For my WordPress sidebar, which uses <li> and <h2> for sections and headers, I use the following:
<li id="github-badge"></li> <script type="text/javascript" charset="utf-8"> GITHUB_USERNAME="drnic"; GITHUB_HEAD="h2"; </script> <script src="http://drnicjavascript.rubyforge.org/github_badge/dist/github-badge-launcher.js" type="text/javascript"></script>
Thus the badge is inserted in the <li> element, and the “My projects (drnic)” header is an <h2> instead of a <div>.
Source and tests on github
You can fetch the source from github - http://github.com/drnic/github_badges/tree/master - using:
git clone git://github.com/drnic/github_badges.git
You might want to add an option to turn off the auto-CSS generator so you can theme it yourself.
The JavaScript tests are in test/ folder. Either run them with rake test, or individually open the files in your browser.
Future cool things the badge could do…
Ultimately, when Chris Wanstrath (harass him on twitter) adds more fields and APIs to the GitHub API then the badge can do more. Currently it orders the projects in reverse order - that is, newest projects first. If I can get the network/watching counters then I can order the list using those values, etc.
Any other cool ideas for what the badge could do?
I’d like to be able to show project version numbers (e.g. gem version numbers) and other project meta-information. This would require GitHub to offer a CRUD UI for generic key/values and for the fields to be returned via their API.
If you know Chris and can throw heavy objects at him with moderate certainty of hitting him and not damaging any expensive equipment, please do so til he gives me this stuff.
The explicit Ruby metaclass you know you always wanted
When you define a “static” or “class” method on a Ruby class, it actually stores the method on that class’s metaclass/singleton class/eigenclass.
_why’s metaid gem gives you a metaclass method to explicit access this object:
require 'metaid'
class Person
def self.oldest
# find oldest person
end
end
Person.methods.grep(/oldest/) # => ['oldest']
Person.metaclass.instance_methods.grep(/oldest/) # => ['oldest']
So now here’s a new, fun way to access the metaclass of a class, look for a constant suffixed with ‘Metaclass’. For the Person class, look for PersonMetaclass. Yep, we can have explicit metaclass constants. Or try PersonClass or PersonEigen or PersonEigenclass. No one can agree on what they are called, so I made them all work.
$ gem install magic_metaclass $ irb
In irb try:
require 'rubygems' require 'magic_metaclass' class Person; end Person # => Person PersonMetaclass # => #<Class:Person> PersonClass # => #<Class:Person> PersonEigenclass # => #<Class:Person> PersonEigen # => #<Class:Person>
Neat.
Finally, the example from above:
class Person
def self.oldest
# find oldest person
end
end
PersonMetaclass.instance_methods.grep(/oldest/) # => ['oldest']
I wrote this gem with no known use cases. If you find any, let me know.
Writing C extensions in RubyGems using newgem generators (plus a free TextMate bundle)
Already know C extensions in RubyGems? Cool - then just run the following cmds and see what can be generated for you; plus check out the TextMate bundle at the bottom.
sudo gem install newgem newgem pickaxe cd pickaxe script/generate extconf my_test rake test
For everyone else…
Its 15000km from Brisbane AU to Prague CZ where Euruko2008 - the European Ruby Conf - was held. I came ready to talk, to met lots of cool multi-lingual Rubyists, and to learn. Ooh, I learnt something alright.
Tim Becker was introducing Native C Extensions for Ruby, and fortunately he said “now, everyone, follow along with this example”. I’d never done native C extensions, but I’d received lots of requests from RubyGem developers on how to do it. I had no idea.
So I was typing in everything Tim told me to type in. When Tim changed slides too quickly, I may have yelled at him to slow down. Perhaps I was the only one doing his tutorial out of 300 people, but I didn’t care. This was gold.
After he finished his session, I dragged him off into the corridor with Jonas Pfenniger (zimbatm), and the three of us mapped out a generic layout for how native C extensions work within RubyGems. I didn’t know any of this, but Tim and Jonas did, and we probably looked silly sitting at a small table in the middle of the narrow corridor.
But at the end, we had a working RubyGem with native C extensions that were built: when the tests were executed via rake, and when the gem was installed. The next day I figured out how to get the C extension built via autotest.
Thanks to Tim and Jonas I was able to then write a extconf generator for RubyGems so that its super-super easy to get started writing native C extensions within RubyGems.
Tutorial
This tutorial is for *nix, as I’m still investigating win32 extensions, and jruby + .net/ironruby extensions. So when I figure that out - hopefully with the help of other people currently at RubyFools conferences, I’ll get back to you.
The code comes from the Pickaxe book - p264, and we’ll insert it into a new RubyGem using newgem (version 0.20.1+):
sudo gem install newgem newgem pickaxe cd pickaxe script/generate extconf my_test
Create a test for a class MyTest that doesn’t exist yet:
# test/test_my_test_extn.rb
require "test/unit"
require 'pickaxe'
class TestMyTestExtn < Test::Unit::TestCase
def test_working
t = MyTest.new
assert_equal(Object, MyTest.superclass)
assert_equal(MyTest, t.class)
t.add(1)
t.add(2)
assert_equal([1,2], t.instance_eval(”@arr”))
end
end
Run rake to build the C extension and run the tests. You can also run autottest and it will automatically build the C extension before running the tests.
To create the MyTest class, using the code from p262 of the Pickaxe book:
#include "ruby.h"
static int id_push;
static VALUE t_init(VALUE self)
{
VALUE arr;
arr = rb_ary_new();
rb_iv_set(self, "@arr", arr);
return self;
}
static VALUE t_add(VALUE self, VALUE obj)
{
VALUE arr;
arr = rb_iv_get(self, "@arr");
rb_funcall(arr, id_push, 1, obj);
return arr;
}
VALUE cTest;
void Init_my_test() {
cTest = rb_define_class("MyTest", rb_cObject);
rb_define_method(cTest, "initialize", t_init, 0);
rb_define_method(cTest, "add", t_add, 1);
id_push = rb_intern("push");
}
To lib/pickaxe.rb:
require "my_test.so" # or require 'my_test' if its unique
The last line will import the generated shared library. If the RubyGem is tested or installed on Windows, then the .dll file will be automatically loaded instead. The “.so” notation is merely a placeholder to explicitly specify the shared C-extension, rather than any Ruby library of the same name.
Now run tests (rake), the C extension will be rebuilt and the tests will pass.
Build and install RubyGem
rake manifest:refresh rake install_gem irb -rubygems -rpickaxe > a = MyTest.new > a.add 3
You have successfully created a C-extension within RubyGems, using TDD.
TextMate bundle for Ruby C extensions
I’ve started a TextMate bundle to give syntax highlighting + some simple snippets for developing the C files for Ruby extensions.
To install:
cd ~/Library/Application Support/TextMate/Bundles git clone git://github.com/drnic/ruby-c-extensions-tmbundle.git "Ruby C Extensions.tmbundle" or wget http://github.com/drnic/ruby-c-extensions-tmbundle/tarball/master tar xfv drnic-ruby-c-extensions-tmbundle-master.tar.gz mv drnic-ruby-c-extensions-tmbundle-master "Ruby C Extensions.tmbundle"
Then restart TextMate or “Reload Bundles”.
You can clone/fork the source via http://github.com/drnic/ruby-c-extensions-tmbundle/tree/master
RejectConf tonight - RailsConfEurope 2007
Tonight only. Live in Berlin. One show only. RejectConf. Woohoo!
Who? What?
Well, it could be you. If you are in Berlin tonight, come to RejectConf.
Time: 9pm (get there early to ensure a seat or at least a space indoors)
Location: NOT NEAR THE VENUE
The remaining details are documented so you can figure out which U-Bahn lines to take and which stops to get off.



