TextMate Bundle for Pure

Pure is a fairly new interesting language based on term rewriting. Unfortunately I lost track of time last night and whipped up a TextMate bundle with some basic support for the language. It is pretty minimal at the moment though it does support basic syntax highlighting, some useful completions, and documentation look up.

You can find it on Github.

Pure Documentation Tooltip

Add comment September 3, 2009

SexpPath 0.4

SexpPath 0.4 is complete now, and a gem is available from GitHub:

sudo gem install adamsanderson-sexp_path

New Features

Two new matchers have been added:

Negation
Matches any time a SexpPath query would normally not match, for instance this Q?{ -s(:a) } would match s(:b), but not s(:a).

Sibling
Matches any pair of siblings in sequential order. For instance this Q?{ s(:a) >> s(:c) } would match s( s(:a), s(:b), s(:c) ), but not s( s(:c), s(:a), s(:b) ).

I am also pretty excited that Magnus Holm has started playing with SexpPath, take a look at sexp_template and sexp_builder, they’re both starting to take shape.

Add comment July 21, 2009

Using the Mac OS X Find Buffer for Text Navigation

Mac OS X has a nifty text feature where you can hit Command-E, and your selection will be placed into a find buffer. Hitting Command-G and Shift-Command-G will find the next or previous instance of your selection.

I find this really useful when poking around in code. For instance if I want to go look somewhere else in a file, I will select a unique chunk of code, and stash it in the find buffer. I go about my business and then as soon as I need to go back, I just hit Command-G. It’s one of those basic little things that really makes working on a Mac quite enjoyable.

Any other good keyboard shortcuts people tend to miss?

Add comment July 17, 2009

SexpPath

SexpPath is a ruby DSL for pattern matching S-Expressions. Think of it as XPath or Regular Expressions for Ruby code, and you’re most of the way there.

Here is an example of an S-Expression in Ruby:

pets = s(:pets,
  s(:cat, :fluffy, s(:color, :white)),
  s(:cat, :snuggles, s(:color, :grey)),
  s(:fish, :bubbles, s(:drinks, :water)
)

This query extracts all the cats from our example:

pattern = Q?{ s(:cat, atom, _ ) }
cats = pets.search pattern

The SexpPath query above looks for expressions that start with the symbol :cat. The atom part says that we will match any symbol, so in the example above it would match both :fluffy and :snuggles. The underscore at the end will match anything at all, in this case, the S-Expression for color.

You can also match nested expressions with SexpPath:

pattern = Q?{ s(:cat, atom, s(:color, grey) ) }
grey_cats = pets.search pattern

SexpPath also has a notion of named matches. This query will place each cat’s name in the query result:

pattern = Q?{ s(:cat, atom % 'name', _ ) }
cat_matches = pets.search pattern
cat_names = cat_matches.map{|match| match['name']}

The % operator tells SexpPath where to stash a matching value.

Here is an example of using SexpPath with Ryan Davis’ excellent ParseTree library to extract all the methods in a given file:

# read in a ruby file
code = File.read('pony_factory.rb')
# parse the file with ParseTree
sexp = Sexp.from_array(ParseTree.new.parse_tree_for_string(code))
# create a SexpPath to find all the methods
pattern = Q?{ s(:defn, atom % 'name', _ ) }
# print all the methods in the ruby file
sexp.search(pattern).each do |match|
  puts match['name']
end

Now you’re doing static analysis on Ruby code! To learn more take a look at the Readme on GitHub, or skim over some of the examples.

If you have any suggestions or critiques of the code, API, etc. I would love to hear from you so comment, fork, or open an issue on GitHub.

4 comments June 22, 2009

Excel ruby-prof Report Printer

I got tired of re-sorting ruby-prof’s profiles in TextMate ( sort -n -r -k 2 is neat, but cumbersome ). So I whipped up a little gem for printing out the reports as Excel documents with excel_printer.

If nothing else, it’s a nice example of using the Spreadsheet gem in ruby.

Go fork it on Github and do something neat with it.-

Add comment June 2, 2009

OpenGem 1.3.1

Quick note, I just updated OpenGem so that you can now do:

gem read activerecord

If you the rdoc has not been generated yet, OpenGem will generate it for you and then open it up for your browsing pleasure (or horror).

So go ahead and update.

sudo gem update open_gem

Add comment June 2, 2009

Testing for existence with “blank?”

I know, I know, all ruby posts should be about slightly new takes on testing paradigms or other amazing best practices, but sometimes I think we skip the basiscs. Today it’s blank?.

ActiveSupport, the Rails extensions to ruby’s standard library introduce the method blank?. It’s pretty handy, and I think people tend to take it for granted without realizing exactly what its semantics are. blank? is handy if you want to treat an empty string, array, or hash as false.

Here’s a slightly haphazard table showing nil?, empty?, and blank? all work.

Input nil? empty? blank?
” “ FALSE FALSE TRUE
nil TRUE ERROR TRUE
1 FALSE ERROR FALSE
0 FALSE ERROR FALSE
“hi” FALSE FALSE FALSE
[] FALSE TRUE TRUE
[ [] ] FALSE FALSE FALSE
[ nil ] FALSE FALSE FALSE
[ 1 ] FALSE FALSE FALSE
true FALSE ERROR FALSE
false FALSE ERROR TRUE
{} FALSE TRUE TRUE
{1=>2} FALSE FALSE FALSE

So if you want a flexible way of testing whether something exists, blank? will often give you what you want. I’m sure that was just thrilling!

Add comment May 15, 2009

OpenGem is on RubyForge

Hey quick note, OpenGem is now on RubyForge, so you can all just do:

gem install open_gem

Hoorah!

Add comment May 6, 2009

Creating Sample Data

Nothing earth shattering today, but I thought it was neat.  Every now and then I come across a document I would like to add to my test suites for a project, but there’s a problem.  The document might contain email addresses, phone numbers, who knows what, and I don’t really want that to be sitting around in my codebase, but the structure of the document matters.  The solution? Pipe it through this:

tr 'A-Za-z0-9' 'a-ff-aa-fa-ff-aa-fa-ff-aa-f001122334455'

Now all of your punctuation and such is preserved for your parsing pleasure, but the data is somewhat obfuscated. Of course use it at your discretion.

For bonus points, I actually made it into a TextMate command. Now I can have my test data, and feel reasonably safe about it.

Add comment May 1, 2009

OpenGem

Ok, trying to post a little more frequently, and less ambitiously.

This is just a tiny note to mention OpenGem a plugin for rubygems 1.3.2.

It’s quite handy, just do:

gem open rails

And rails will open up in your favorite text editor ($EDITOR).

1 comment April 28, 2009

Previous Posts


AdamSanderson

del.icio.us

Feeds

Pages

 

November 2009
M T W T F S S
« Sep    
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Archives

Blogroll

Meta