Category Archives: development

3 Browser Based Editors to Watch

In the last couple of weeks, there has been a fair amount of talk about three web based editors. Google has started embeding CodeMirror into Google Project Hosting, IBM is developing Orion, and Mozilla is merging Bespin/SkyWriter into Ace, Cloud9’s editor. Everything from office suites to music players have been migrating online, it seems likely that these editors will be the next category of apps to live in your browser. Will they replace vi, emacs, or TextMate? Probably not any time soon, but they do offer some interesting opportunities, let’s take a look at each of them.

CodeMirror

CodeMirror seems to the be one of the oldest browser based editors. If you’re interested in how it’s implemented, there is a great article covering CodeMirror’s creation. The focus so far seems to be around making a decent editor you can embed in existing projects. Of the three editors, it seemed to be the slowest, but also the most versatile, well documented, and mature editor. It is also worth noting that google has been using it for a while in their KML playground, patch submission for Google Project, and the Google API Playground.

Orion

Little has been said so far about Orion, and it’s obviously in an early stage, but it sounds like some of the Eclipse team is working on it, which is interesting news. I couldn’t find an online demo, however it’s quite easy to download and run the Orion server. The editor itself is pretty snappy, and already has some unique features such as code annotation. Where as CodeMirror is aimed at embedding in existing pages, Orion is setting out to be a full IDE which handles loading and saving files as well as managing user accounts. The general interface surrounding the editor is quite rudimentary at this point, but what is working so far looks good.

Ace / Cloud9

As I mentioned above, Mozilla’s Bespin/SkyWriter is being merged into Ace, the editor for the Cloud9 IDE . Bespin was a very interesting project, and I think Ace shows some great potential. The editor itself is quite fast, and seems natural to use. There isn’t much information out there concerning the full IDE, so I tried signing up for the hosted beta, but found that you can already download and install Cloud9 the IDE yourself. The Cloud9 IDE let’s you easily serve up any directory and edit it through your browser.  The interface is surprisingly polished, and seems like it could really offer up a good experience in the future.

So where does all this leave us? None of these editors have the additional functionality that we expect in our day to day editors, but the promise of being able to edit files quickly within your browser is interesting.  These editors could decrease some of the barriers to web applications, perhaps becoming as useful as tools like Firebug.  I’m not about to throw out TextMate, but as a web developer, I think these editors will fill a useful niche in the future.

Advertisements

10 Comments

Filed under development, Web Development

The Strange Ruby Splat

This article has been republished on Monkey and Crow.

As of ruby 1.9, you can do some pretty odd things with array destructuring and splatting. Putting the star before an object invokes the splat operator, which has a variety of effects. First we’ll start with some very useful examples, then we will poke around the dark corners of ruby’s arrays and the splat operator.

Method Definitions

You can use a splat in a method definition to gather up any remaining arguments:

  def say(what, *people)
    people.each{|person| puts "#{person}: #{what}"}
  end
  
  say "Hello!", "Alice", "Bob", "Carl"
  # Alice: Hello!
  # Bob: Hello!
  # Carl: Hello!

In the example above, what will get the first argument, then *people will capture however many other arguments you pass into say. A real world example of this can be found in the definition of Delegator#method_missing. A common ruby idiom is to pass a hash in as the last argument to a method. Rails defines an array helper Array#extract_options! to make this idiom easier to handle with variable argument methods, but you can actually get a similar behavior using a splat at the beginning of the argument list:

  def arguments_and_opts(*args, opts)
    puts "arguments: #{args} options: #{opts}"
  end
  
  arguments_and_opts 1,2,3, :a=>5
  # arguments: [1, 2, 3] options: {:a=>5}

Now this example only works if you are guaranteed to pass in a hash at the end, but it illustrates that the splat does not need to always come at the end of a method’s parameters. There are also some other odd uses for the splat in method defitions, for instance this is valid:

  def print_pair(a,b,*)
    puts "#{a} and #{b}"
  end
  
  print_pair 1,2,3,:cake,7
  # 1 and 2

Outside of letting you mimic javascript calling conventions, I’m not sure what the practical use is:

  function print_pair(a,b){ 
    console.log(a + " and " + b);
  }
  
  print_pair(1,2,3, "cake", 7);
  //=> 1 and 2

Calling Methods

The splat can also be used when calling a method, not just when defining one. If you wanted to use the say method from above, but you have your list of people in an array, the splat can help you out:

  people = ["Rudy", "Sarah", "Thomas"]
  say "Howdy!", *people
  # Rudy: Howdy!
  # Sarah: Howdy!
  # Thomas: Howdy!

In this case, the splat converted the array into method arguments. It doesn’t have to be used with methods that take a variable number of arguments though, you can use it in all kinds of other creative ways:

  def add(a,b)
    a + b
  end
  
  pair = [3,7]
  add *pair
  # 7

Array Destructuring

First of all, lets quickly cover a few things you can do without splatting:

  a,b = 1,2               # Assign 2 values at once
  a,b = b,a               # Assign values in parallel
  puts "#{a} and #{b}"
  # 2 and 1

With the above samples in mind, let’s try some fancier stuff. You can use splats with multiple assignment to extract various elements from a list:

  first, *list = [1,2,3,4]          # first= 1, list= [2,3,4]
  *list, last  = [1,2,3,4]          # list= [1,2,3], last= 4
  first, *center, last = [1,2,3,4]  # first= 1, center= [2,3], last=4
  
  # Unquote a String (don't do this)
  _, *unquoted, _ = '"quoted"'.split(//)
  puts unquoted.join
  # quoted

Array Coercion

If for some reason the previous examples seemed like great ideas to you, you’ll be thrilled to know that the splat can also be used to coerce values into arrays:

  a = *"Hello"  #=> ["Hello"]
  "Hello".to_a  #=> NoMethodError: undefined method `to_a' for "Hello":String
  a = *(1..3)   #=> [1, 2, 3]
  a = *[1,2,3]  #=> [1, 2, 3]

This can be a nice way to make sure that a value is always an array, especially since it will handle objects that do not implement to_a

The splat is a wily beast popping up in odd corners of ruby. I rarely actually use it outside of in method definitions and method calls, but it’s interesting to know that it is available. Have you found any useful idioms that make use of the splat? I would love to hear about them.

35 Comments

Filed under development, ruby

Qwandry 0.1.0 – Now Supporting More Languages

I just finished updating Qwandry so that it can support any number of other languages or packaging systems. Want to use perl, python, or node with Qwandry? No problem:


qw -r python numpy # opens python's numpy library
qw -r perl URI     # open perl's URI library
qw -r node express # open express if it is installed for node

Qwandry will probe these dynamic languages and detect their load paths. This is just the first step towards making code more accessible to people. I would love to hear what you think of it, and if you have any suggestions.

Go ahead and install it with ruby’s package manager:


  gem install qwandry

Warning

If you had customized Qwandry before, this release will break your custom init.rb file. Configuration commands looked like this:


  add 'projects', '~/toys'
  add 'projects', '~/samples'

Now they look slightly different:


  register 'projects' do
    add '~/toys'
    add '~/samples'
  end

Awesome

By wrapping the commands that actually add paths to Qwandry’s search path in a block, we can defer slow operations like probing. Furthermore, we now only need to build up the paths for what you are looking for. By deferring configuration until it is needed, we can add support for any language or package scheme we like without slowing Qwandry down.

So what would you like to see Qwandry support next?

2 Comments

Filed under development, ruby

Qwandry

When you’re happily coding away, and you run into a bug, or are curious about a library you’re using, what do you do? I like to look at the source, so I wrote open_gem.

There were two problems with this. First, open_gem doesn’t open Ruby’s standard library. Second, each language has its own packaging system, npm, cabal, cpan, and so on.

Qwandry gives you a single way to easily open all your projects and libraries. Since I use Ruby most of the time, that’s what you get by default, but telling Qwandry about other languages is easy.

Here are a few examples:

qw matrix # opens ruby's matrix library
qw activerec 3.1 # will find version 3.1 of activerecord

If you want to use Qwandry with Node.js, Perl, Python, Haskell, R, or any other language you can dream up just run the customize command:

qw --customize

Installation is easy:

gem install qwandry

The code is on GitHub as usual, so hack away (qw qw).

4 Comments

Filed under development, ruby