At DemoCampMontreal4, I showed the results of time spent programming Ruby and RubyOnRails within the excellent e Text Editor for Windows. You can read about the presentation in my DemoCampMontreal4 report here at YashLabs, MTW and Marc-André Cournoyer’s blog.
e Text Editor and the extensible Bundle system
e is compatible with (the Mac OS X-only) TextMate bundles and can load in its snippets for faster Ruby and Rails programming. However, I also wanted to have quick access to all the methods associated to the common Ruby data structures:
String
Array
Hash
Fixnum
The Design
A keyboard shortcut for launching the menu
A menu displaying all methods is displayed
From the menu, the user can scroll and select the method or jump to it with a keypress for the first letter of the method
On selection, the menu disappears and the appropriate method name is inserted at the current cursor position
Ruby’s reflection capabilities
Ruby is a great object-oriented language. In Ruby, everything is an object and hence all the object-oriented principles I learnt are implemented in Ruby quite well and simply, which is more than can be said for C++ and Java. For instance, in Ruby, you can do this:
10.times
or
"Hello".length
Ruby is great for introspection as it has good reflection capacities – an object can provide information about its internals. The inbuilt .methods method gives all the methods associated with a data structure. e.g.
puts Array.methods.sort.inspect
gives
["< ", "<=", "<=>", "==", "===", "=~", ">", ">=", "[]", "__id__", "__send__", "allocate", "ancestors", "autoload", "autoload?", "class", "class_eval", "class_variable_defined?", "class_variables", "clone", "const_defined?", "const_get", "const_missing", "const_set", "constants", "display", "dup", "eql?", "equal?", "extend", "freeze", "frozen?", "hash", "id", "include?", "included_modules", "inspect", "instance_eval", "instance_method", "instance_methods", "instance_of?", "instance_variable_defined?", "instance_variable_get", "instance_variable_set", "instance_variables", "is_a?", "kind_of?", "method", "method_defined?", "methods", "module_eval", "name", "new", "nil?", "object_id", "private_class_method", "private_instance_methods", "private_method_defined?", "private_methods", "protected_instance_methods", "protected_method_defined?", "protected_methods", "public_class_method", "public_instance_methods", "public_method_defined?", "public_methods", "respond_to?", "send", "singleton_methods", "superclass", "taint", "tainted?", "to_a", "to_s", "type", "untaint"]
However, [].methods.sort contains additional methods not contained within Array as shown by:
res= [].methods - Array.methods
puts res.inspect
This gives the additional methods of []:
["select", "[]=", "transpose", "< <", "&", "indexes", "partition", "map!", "uniq", "empty?", "fetch", "values_at", "*", "grep", "+", "shift", "clear", "-", "reject", "insert", "reverse!", "indices", "delete", "first", "concat", "member?", "flatten!", "|", "find", "join", "delete_at", "each_with_index", "nitems", "unshift", "index", "collect", "fill", "all?", "uniq!", "slice", "length", "entries", "compact", "last", "detect", "delete_if", "zip", "each_index", "map", "sort!", "assoc", "rindex", "any?", "to_ary", "size", "sort", "min", "push", "find_all", "each", "slice!", "pack", "reverse_each", "replace", "inject", "collect!", "rassoc", "at", "reverse", "compact!", "sort_by", "max", "reject!", "flatten", "pop"]
Therefore, instead of Array.methods, I’d rather get [].methods.
Cygwin
Cygwin’s great UNIX-like programming environment is used by e for the bundle system. This is interesting because from there you can run Ruby code within the e Bundle system and communicate with Cygwin through to the Operating System.
There’s a great post by Ben Kittrell describing how he made a great Mac-like environment for Rails development on Windows using Cygwin.
wxCocoaDialog
CocoaDialog is a lightweight Objective-C application for Mac OS X to provide easy access to common GUI widgets and is particularly suitable for object-oriented scripting languages.
Fortunately, some kind soul ported CocoaDialog to use the cross-platform and open-source wxWidgets toolkit. Actually, it is e Text Editor and its TextMate-compatible bundle system that gave rise to wxCocoaDialog.
And in this wxCocoaDialog port, we have three additional runmode items not present in CocoaDialog on Mac OS X, including…menu!
Putting it all together
Provided you have e installed (wxCocoaDialog comes with it) as well as Cygwin and Ruby for Cygwin, here is how to proceed.
- In e, press CTRL-SHIFT-B to open the Bundle Editor
- On the left tree-view pane, select Ruby
- Click on the big + button lower down and choose New Command
- Name the command RubyMethodsString (or anything suitable for you)
- Paste in the following code I wrote which connects Ruby, Cygwin, wxCocoaDialog and e. Be careful when pasting as currently the code formatting plugin does weird things with quotes – all the quotes are straight except after index= – the outermost ones really are backticks to access the system
#!/usr/bin/env ruby
#Access Ruby Methods for strings
#August 2007 - Josh Nursing - josh.nursing AT gmail.com
SUPPORT = ENV['TM_SUPPORT_PATH']
DIALOG = SUPPORT + '/bin/CocoaDialog.exe'
sel = ENV['TM_CURRENT_WORD']
x = "--xpos #{ENV['TM_CARET_XPOS']} "
y = "--ypos #{ENV['TM_CARET_YPOS']} "
am="".methods.sort
menu=[]
#Populate menu with formatted entries
am.each do |w|
menu.push "'" + w.to_s + "' "
end
#CocoaDialog menu
index =`"#{DIALOG}" menu --items #{menu} #{x} #{y}`.to_i - 1
#Insert the selected method text at caret position
print '.' + am[index]
- In the upper right corner, as Environment, select Cygwin
- Lower down, select as Input: Selected Text or Word
- As Output, select Insert as Text
- As Activation, select Key Trigger and press a key combination. I used CTRL-SHIFT-Y
- Close the bundle editor and in your Ruby file within e, after a string variable name or string, press the key shortcut

- Navigate the menu either with the Up or Down Arrows or jump straight to a method by pressing its first letter
The selected method including the dot is inserted within your code in e.
From here you can derive the very similar codes for the other data structures and add them with new shortcuts. My shortcuts are in a row on the keyboard (CTRL-SHIFT-Y, -U, -I, -O).
This menu extension can be accessed when you’re developing a Ruby on Rails application as well.
This shows how e can be usefully extended to work better with your favorite programming language.
Recent Comments