Activate your free membership today | Log-in

Monday, November 10th, 2008

Redshift: What Ruby and a nice API can do in the browser

Category: Rails, Ruby

Red writes like Ruby and runs like JavaScript

That is the tagline for Red, and they mean it:

The all-new Red is a Ruby-to-JavaScript transliterator backed by a custom ruby.js library designed to let your code run in the browser exactly* the way Ruby would run on your machine. The JavaScript output is optimized to contain only the fraction of the ruby.js source library needed to run your code.

What does this mean for today’s Ruby developer? Simple. You don’t need to learn JavaScript.

Trek Glowacki shows us what this is all about. When I wrote about running JRuby in the browser I talked about the desire for a nice API to the browser side of things. Having Ruby is fine and all, but what about the APIs? What about the standard library?

In the demo screencast below you get to see some of the APIs in question, such as:

RUBY:
    Document.ready? do
      Document['.photo_wrapper'].each do |el|
        PhotoBucket.new(el)
      end
    end

Posted by Dion Almaer at 8:08 am
16 Comments

++++-
4.8 rating from 17 votes

Wednesday, August 13th, 2008

Prefer Ruby syntax? See Red, and your Ruby will convert to JS

Category: JavaScript, Ruby

I am a fan of Ruby, and when I saw Red the framework that allows you to write Ruby and get JavaScript out the other end I was excited. It allows you to write this:

RUBY:
    class MyClass
       @@my_var = 500
       
       def initialize(arg)
         @arg = arg
       end

       def my_method
         alert(@arg)
       end
    end

And you end up with:

JAVASCRIPT:
  1.  
  2. 1| var MyClass = function(arg) { this.arg = arg;this.myMethod = function() { alert(this.arg); } }; MyClass.myVar = 500
  3.  

You can also have fun with blocks:

RUBY:
    [1,2,3].sort do |x,y|
      return y - x
    end
JAVASCRIPT:
  1.  
  2. [1, 2, 3].sort(function(x,y) { return y - x; })
  3.  

You also get convention conversion, which can lead to some weird things such as:

RUBY:
    Yahoo[:util]::Dom
JAVASCRIPT:
  1.  
  2. Yahoo.util.Dom
  3.  

On one hand I am excited about being able to think that I am hacking on Ruby, even though I am not. On the other hand, I worry about the abstraction leaking (ending with weird bugs) and how ruby is great because of all of the MOP, the libraries, and other things that you can't really do with this system. This makes it a bit of a tease.

Posted by Dion Almaer at 6:28 am
10 Comments

++---
2.9 rating from 34 votes

Monday, July 14th, 2008

Interview with the Gears on Rails team

Category: Gears, JavaScript, Podcasts, Rails, Ruby

With my Google hat on, I got to interview Michael Marcus and Rui Ma, two recent graduates from a masters program at NYU. They joined me to discuss Gears on Rails, their open source framework that makes it easier than ever to take a Rails code-base offline.

They take the approach of giving you a high level Ruby-ish way of developing your Rails app and having it work offline against local storage.

This means that you end up building actions like this:

RUBY:
    def create_local
     '
       post = Post.build(params("post"));
       Post.create_local(post);
       window.location.reload( false );
     '
    end

They build the local framework on the Jester framework that is a "JavaScript client for REST APIs that uses Rails conventions, and is inspired by Rails' own ActiveResource".

This means that you can write client side code like this:

JAVASCRIPT:
  1.  
  2. Resource.model("Twitter", {format: "json", prefix: "http://www.twitter.com", urls: {list: "/statuses/user_timeline/:username.json"}})
  3.  
  4. Twitter.find("all", {username: "bob"}, function(results) {
  5.   twitters = results
  6. });
  7.  

Listen to the audio interview directly (or subscribe via iTunes).

Posted by Dion Almaer at 4:29 pm
1 Comment

++---
2.7 rating from 10 votes

Monday, June 9th, 2008

Johnson: Wrapping JavaScript in a loving Ruby embrace, and ARAX

Category: JavaScript, Ruby

John Resig pointed us to Johnson, a project that "wraps JavaScript in a loving Ruby embrace."

It seems to give you a JavaScript interpreter that you can pass a context into to. The simple examples are:

RUBY:
    require "johnson"
     
    Johnson.evaluate("4 + 4") # => 8
    Johnson.evaluate("4 + foo", :foo => 4) # => 8

John saw the following code that gives you __FILE__ as a global that stores the file name a la Perl:

JAVASCRIPT:
  1.  
  2. (function(){
  3.   this.__defineGetter__("__FILE__", function() {
  4.     return (new Error).stack.split("\n")[2].split("@")[1].split(":").slice(0,-1).join(":");
  5.   });
  6. })();
  7.  

I am curious to see the itch that they guys are scratching here.

Of course, Silverlight is giving us ARAX according to John Lam. Darryl Taft reported on the news, and I quickly told him that JRuby has given us Ruby in the browser for quite some time. The latest Java plugin supports JNLP, and JRuby build a JNLP distribution for you if you want it. Silverlight is another vector for Ruby in the browser.

Ben said the following about the Silverlight version:

If this is about using Silverlight to host client-side browser scripting in Ruby, it's definitely an appealing notion, but the problem will always be about Silverlight being a Microsoft technology.

As long as Windows/Office dominates Microsoft's balance sheet, these cross-platform Microsoft plays always feel a bit like the story of the boy who upon encountering a rattlesnake picks it up after it promises not to hurt him, upon which the snake promptly bites. After the boy protests, the snake says: 'You knew what I was when you picked me up.' No matter what capabilities Silverlight may have, I think most of us in the community simply wouldn't dream of embracing architectures dependent on Microsoft's goodwill to support other OS vendors.

Posted by Dion Almaer at 6:05 am
5 Comments

+++--
3.6 rating from 19 votes

Wednesday, May 21st, 2008

classy_inputs: Rails plugin to add autoclass names

Category: CSS, Ruby

We all want to use input[type=text] but browser support doesn't seem to quite be there (IE 6?).

This lead James Coglan down the path of creating a teeny Rails plugin, classy_inputs:

Good lord do I ever hate input tags. (YUI hates them too, but I’ll leave that story for another time). All the different types should really have been different tag names, and they are a total pain to use with CSS. As such, I used to end up doing tedious stuff like adding a :class option to every form element when writing Rails templates. A while back, I tried to patch Rails to get it to do this automatically, but its test suite totally baffled me at the time by applying the classes in some places but not others.

So, a quick plugin to stave off RSI:

script/plugin install
http://svn.jcoglan.com/classyinputs/trunk/classy_inputs

With that installed, any input tag created using a Rails view method will gain a class name matching its type attribute. Maybe I’ll have another stab at patching Rails, but I’m not promising anything.

Posted by Dion Almaer at 9:50 am
6 Comments

++---
2.4 rating from 9 votes

Thursday, March 27th, 2008

HotRuby: Run Ruby on a JavaScript interpreter?

Category: JavaScript, Library, Ruby, Showcase

This is from the "wow, really?" department. HotRuby is an implementation of Ruby in JavaScript!

The way it works is that a HotRuby "VM" takes the resulting output from YARV and can grok it via JSON:

RUBY:
    VM::InstructionSequence.compile(cgi['src'], "src", 1, OutputCompileOption).to_a.to_json

Thus, you can embed Ruby by doing something like this:

HTML:
  1.  
  2.         <script type="text/ruby">
  3. class Pi
  4.         def initialize
  5.                 @a = 355.0
  6.         end
  7.        
  8.         def calc
  9.                 b = 113.0
  10.                 return @a / b
  11.         end
  12.        
  13.         PI = 'PI is about'
  14. end
  15.        
  16. puts Pi::PI
  17. puts Pi.new.calc
  18.         </script>
  19.  
  20. <body onload="prettyPrint(); new HotRuby().runFromScriptTag('/compileRuby.cgi')">
  21.  

Since you can run on any JavaScript interpreter or VM, they also support Flash.

For example, check out this Box2D example written in Ruby, running in Flash:

HotRuby Example

Posted by Dion Almaer at 8:01 am
3 Comments

+++--
3.5 rating from 28 votes

Wednesday, March 5th, 2008

Dynamic Silverlight Launched: Ruby and more in the browser

Category: Microsoft, Ruby

John Lam has posted about a new release, DSL: Dynamic Silverlight. I can't believe that Microsoft is going to take the DSL TLA ;)

Our team is happy to announce Dynamic Silverlight (DSL), which integrates our DLR dynamic languages with Silverlight. It requires Silverlight 2, which includes the cross-platform CLR and a set of libraries for rich graphics, media and web programming. It is packaged as a Silverlight extension, which means that it is downloaded in addition to Silverlight on an as-needed basis.

DSL has a runtime and an SDK component. The runtime consists of two assemblies: Microsoft.Scripting.dll, and Microsoft.Scripting.Silverlight.dll. You'll also need the language assemblies, which are IronRuby.dll and IronRuby.Libraries.dll for Ruby and IronPython.dll and IronPython.Modules.dll for Python. The runtime component is a small additional download. Today, the IronRuby Silverlight runtime is just a 712KB download, which takes less than 5 seconds to download over modern broadband.

Now you can get Ruby in your browser:

John also talked about Chiron, a mini Web server that lets you develop against a local file system with ease. We will find out more, and get access to the bits, on Friday when John gives his MIX talk.

Posted by Dion Almaer at 10:43 am
2 Comments

+++--
3.5 rating from 21 votes

Thursday, February 14th, 2008

Nextpoint: Taking Ajax to Court

Category: Prototype, Ruby, Scriptaculous, Showcase

I used to work in the healthcare sector, and was always amazed to see the amount of paperwork that was required. Literally paper work that is. The industry was full of drawers overflowing with paper.

I imagine that the legal profession has its fair share of this too, but one company Nextpoint, is trying to change that.

I had the opportunity to sit down with some members of the Nextpoint Lab, including Jim Halberg the Ajaxian, to get a tour and discuss their latest products. Below are a few questions about the Ajax implementation, and we finish up with a video showcasing the work.

What are the Nextpoint tools?

Nextpoint's flagship software product (marketing site, brochure), brings web 2.0 to the world of litigation software. We provide a powerful and easy to use way to manage the mountain of electronic information associated with any case.

What are some of the cool Ajax features?

The site has many small ajaxy features. Things like status bars for uploads or bulk operations, submitting small bits of data that shouldn't require traditional round-trips, or updating a small piece of the page with some results. The stuff we think Ajaxian readers would be more interested in mainly revolves around our work with images.

A real pain source for many years in trial litigation has been stamping documents. When you decide you want to use a document in court it must be stamped (i.e. "Defense Exhibit 1") and then communicated to the opposing team of lawyers. With most products today this means, locating the document in the management software; exporting it; printing it; physically stamping it; scanning it back in locally; uploading it back into the management software; and finally transmitting that electronic copy... it's not hard to see how this could get annoying after the 100th time you've had to do it this week. Nextpoint provides the ability to electronically add a customized stamp - these can be resized or repositioned as desired and because we're doing things electronically it's much easier to do things like "stamp these thousand documents as 'Defense Exhibits'". Believe it or not - the server normally can get this task done a bit quicker than a guy with a rubber stamp in his hand. When the machine is done stamping - they're already electronic copies - you're ready to go.

Our flashiest use of Ajax is in our presentation tool, "Theater". Many of our clients are using this tool to prepare document treatments before a trial, so that they're ready to call up at a moments notice in a pre-formatted state. It also may be used live in a courtroom to direct listeners attention to important points in a way that visually impresses. We'll show this off in the demo video we're going to provide.

What tools are used to create theater?

Theater is mostly a custom javascript application, using prototype and scriptaculous to simplify the Ajax communication and animation. The images are served from a Merb-based server, which uses the GD libraries to do scaling, rotating, cropping, and setting transparency on the fly. The transparency is especially important, so that HTML elements can be used as highlights behind the main image content, keeping the user interface very responsive.

What were the biggest challenges getting all of this Ajax stuff to work?

Even when using Ajax to keep the traffic between client and server light, the latency and server processing time can become quite apparent. Theater employs a few neat tricks to keep the application feeling responsive. The annotations on documents (mentioned above) are done with HTML elements, preventing the wait of a roundtrip to the server to get a new image. When loading new documents, a pre-generated medium-resolution image is loaded first, and then replaced with an exact-fit image once it's ready, which usually takes less than a second. Similarly, when creating callouts of an image section, the main document image is resized and cropped in a DIV in the browser as a low-resolution preview until the high-resolution version is available. The same thing happens when callouts get resized to make room on-screen for more callouts. Users feel more like they're working with an application when they can continue to work with the preview versions instead of waiting after each click.

Allowing users to resize and reposition a "stamp" on top of an image presented some challenges but most of the trouble in that interface emerged from making sure that the final position the user set with css/js was properly translated into coordinates that could be used for the actual image manipulation on the backend. Allowing a variety of stamp formats, variable amounts of text, and translating between entirely different measurement systems for fonts on the server vs. the browsers complicated things, as well as the oft-demanded rounded corners to make the stamps look "more natural".

Originally Theater was based around a tiled-image concept, like Google Maps. The images were pre-cropped into tiles at a few different "zoom" levels, and then further zoom levels were simulated by resizing in the browser. Unfortunately, storing 200+ images for each page of each document quickly became unmanageable. In addition, many browsers use a pretty low-quality algorithm for resizing images, leaving the in-between levels looking "chunky" or "distorted." Using the GD library, and a streamlined application server that doesn't load the entire Rails application, we were able to overcome this issue by generating images on the fly. That reduced the number of requests made to the server and creates pristine images of just the right size.

Demo

Below is a demo of the product. For a high quality version go here.

UPDATE: A new video showing the data stamping technique has been added. There is a normal version, and a high res one (must be a vimeo member for high res).

Posted by Dion Almaer at 9:01 am
6 Comments

++++-
4 rating from 41 votes

Sunday, February 10th, 2008

Heroku: Web based Rails Hosting

Category: Prototype, Rails, Ruby, Showcase

Heroku is a new YCombinator startup that joins the growing number of "use your browser to build your apps" type of applications.

You can create new Rails applications, and they are magically hosted up in the cloud. You can import your own Rails application, or you can use the inline editor and tools to built the application directly in the browser.

Heroku itself is a Rails application. I wonder if they now self hosting :)

Being able to quickly build an application and have it running live is great (using Amazon EC2), and this is just the beginning. They already tie into the usual tools like Rake, but there is room to go further and have nice DB utilities, cloning of functionality, and much more.

The editor itself could use a bunch of work too. I can never see where the cursor is, let alone have all of the Textmate / Aptana / IntelliJ goodness.

Heroku

Posted by Dion Almaer at 9:28 am
7 Comments

+++--
3.2 rating from 26 votes

Wednesday, January 23rd, 2008

Ext Scaffold Generator Plugin for Rails

Category: Examples, Ext, Rails, Ruby

Martin Rehfeld has released a Rails plugin that generates Ext JS scaffolds

The Ext Scaffold Generator Plugin provides a custom MIME type alias :ext_json to be able to handle requests from the Ext frontend separately. The generated controllers show how to do this.

To make data delivery to the Ext frontend easy, the plugin extends the Array and ActiveRecord::Base classes to provide a to_ext_json method. Here’s a simplified example of a potential index method in a PostsController:

RUBY:
    # GET /posts
    # GET /posts.ext_json
    def index
      respond_to do |format|
        format.html     # index.html.erb (will fire ext_json request)
        format.ext_json { render :json => Post.find(:all).to_ext_json }
      end
    end

He also has some other good articles on integrating Ext JS and Rails in general.

Posted by Dion Almaer at 6:54 am
2 Comments

+++--
3.9 rating from 28 votes

Thursday, January 10th, 2008

XSS: Flash and Rails

Category: Flash, Ruby, Security

A couple of good articles on XSS and security came out at the same time. One talks about XSS in Flash, and the other on Rails:

XSS Vulnerabilities in Common Shockwave Flash Files

Rich Cannings has written an article explaining the issue of XSS wrt Flash:

Critical vulnerabilities exist in a large number of widely used web authoring tools that automatically generate Shockwave Flash (SWF) files, such as Adobe (r) Dreamweaver (r), Adobe Acrobat (r) Connect (tm) (formerly Macromedia Breeze), InfoSoft FusionCharts, and Techsmith Camtasia. The flaws render websites that host these generated SWF files vulnerable to Cross-Site Scripting (XSS).

This problem is not limited to authoring tools. Autodemo, a popular service provider, used a vulnerable controller SWF in many of their projects.

Simple Google hacking queries reveal that hundreds of thousands of SWFs are vulnerable on the Internet, and a considerable percentage of major Internet sites are affected. We are only reporting XSS vulnerabilities that have been fixed by the vendors.

He talks through tools that cause the issue, and then solutions on how to keep yourself safe.

Is your Rails site XSS safe?

Stu Halloway of Relevance has written about SafeErb and how he got it working on a Rails 2.0 application. He kept notes as he went through the experience, allowing us to learn from his acts.

What is SafeErb?

Safe ERB lets you make sure that the string written by "<%= %>" in your rhtml template is escaped correctly. If you try to show the attributes in the ActiveRecord instance read from the database or the parameters received from the request without escaping them using "h" method, an exception will be raised. This will significantly reduce the possibility of putting cross-site scripting vulnerability into your web application.

The check is done using "tainted?" method in Object class which is a standard feature provided by Ruby - the string is "tainted" when it is read from IO. When ERB::Util#h method is called, this plugin "untaints" the string, and when "<%= %>" is called in your rhtml template, it raises an exception if the string you are trying to show is tainted.

Posted by Dion Almaer at 12:14 pm
Comment here

+++--
3.2 rating from 10 votes

Friday, January 4th, 2008

Zed Shaw interview on Rails community, enterprise, Ajax, patents, and a whole lot more

Category: Podcasts, Rails, Ruby

Rob Sanheim sat down with Zed Shaw at RailsConf and had an hour long conversation with him that covered his thoughts on the Rails community, the role of the Enterprise, the state of Ajax, JRuby and Rubinius, documentation, tests, tooling, the role of patents in software, and a whole lot of opinion.

Zed Shaw

It is very interesting to listen to this after the explosion that happened when Zed lambasted the Rails community. When you listen to this interview, you see some of the seeds of the rant, but it is a lot more toned down, and there is some good stuff in there. It is easy to blog a crazy rant.... but when you are talking to someone you get a different side of the coin. This gives you that side, from a time when he wasn't as upset as he may have been when he sat at the computer to type up his post.

Listen to the recording, or subscribe to the podcast. We will go back to more "standard" Ajax topics in the future.

Zed's Core Quotes

  • On Semantic Web: Einsteins brain on a crack whores body isn't going to happen
  • I'm waiting for someone to blind-side the entire Web stack
  • Some people hate me, but love Mongrel
  • Where is the XP for managers

And here are some of the thoughts that Zed expressed throughout the interview:

Thoughts on the Rails community, and enterprise (as big business)

  • Mixed feelings
  • Mongrel was an art project
  • Simpler software is better
  • Enterprise software is known to be complex, and survives to make money for consulting companies
  • Afraid of consulting companies getting behind it, as their interest is in selling 30 people vs. 3 people teams

What could an enterprise company sell?

  • Do enterprise stuff well such as Authentication
  • Stacks: Make it simple (no ClassLoader6)

JRuby

  • It is a huge deal
  • The only fear is that Sun will mess it up with the JCP.
  • The JRuby guys are rock stars

Rubinius

  • An open source project not controlled by anyone
  • A bunch of guys who really love Ruby
  • Massive "spec", working with the JRuby guys

State of Ajax

  • HTTP sucks
  • Needs to be a reset
  • Semantic Web: "Einsteins brain on a crack whores body isn't going to happen"
  • I'm waiting for someone to blind-side the entire Web stack
  • Ajax the technology doesn't impress me, but the new UIs that we are seeing is fantastic
  • Usability != better looking
  • "click here" actually does a really good job at having people click here!

What is going to come out with all of the work happening on top of Mongrel?

  • Swiftapply
  • Evented mongrel
  • DrProxy
  • OpenBSD clustering
  • X hits per day is meaningless. What is the peak?

Honest Open Source

  • Not all open source projects are equal
  • Make everything open and public immediately (e.g. SVN)
  • Corporate open source projects often lose their flavour
  • Outside commiters are key
  • Some people hate me, but love Mongrel
  • Documentation is poor for Rails and Ruby, Ruby doesn't have a culture for it
  • Rails core does a much better job that the Ruby community in general, and this is a reason why it took off
  • QRI command line. Way better than RI
  • If Rails core isn't using it, don't use it. Add: used_by

What tools do you use?

  • Vim
  • Use a generic tool, and pimp it
  • "I code with a thesaurus"
  • Vim is designed to be used on phone lines, and it is very safe
  • Good tools never cover your code

Testing

  • A bit of design up front
  • Design the API
  • Tests to measure how it is working
  • Quality comes from the design ahead of time

Posted by Dion Almaer at 7:55 pm
5 Comments

+++--
3 rating from 59 votes

Friday, November 30th, 2007

Placeshout: New Rails based Geo-cool site

Category: Mapping, Ruby, Showcase

Andre Lewis has a new site out there, Placeshout which offers a way to quickly call out your favourites place in various locations.

You could argue that we have other places for this... Yelp for example, or My Maps themselves. So, why Placeshout?

Sometimes, you just want a quick suggestion

When Andre and I are looking for a hole-in-the-wall Mexican restaurant or a park with a softball field, we usually just want a quick suggestion, not a lengthy review. We want to know if a place is worth visiting in 30 seconds.

Placeshout isn't about volume - it's about trying to express the positives and negatives of a destination in as few of words as possible. If people agree, that "shoutout" moves up...if they don't, the shoutout moves down and begins to disappear.

We hope Placeshout makes it easier for you to find local destinations.

There are some interesting features in this, very Web 2.0-looking, site. One that stands out is the enhanced mapping experience on top of Google Maps. As you move around, directional arrows tell you how far various other cities are away. It is kinda fun to watch:

Posted by Dion Almaer at 10:05 am
3 Comments

++---
2.9 rating from 17 votes