Euruko 2013 Opal lightining talk

For those who missed it here's the lightning talk I gave in Athens at Euruko 2013 (at minute 17:00):

euruko-talk-poster Downlaod it here

(beware: the slides in the video have the wrong aspect raito)

Reading meh's code


I don't always read meh's code
but when I do it, I convert tabs to spaces.

The bookmarklet: ↦ meh` (drag it to your bookmark bar)

UPDATE: Now works with GitHub expanding tabs to non-breaking-spaces (history here)

Opal Talk at RubyDay 2013 video availble!

Hey, the talk I gave at this year's (2013) RubyDay is now on youtube!
I'll update the post with the slides soon :)

(LANGUAGE: Italian).

Past and Future of Ruby and Rails

As a sad orphan of railsapi.com I’m proud to present you valuable links to ruby and rails documentation in SDoc format:

Searchable Ruby 2.0 API documentation


Searchable Rails 3.2 API documentation


Autocomplete "git" as "g"

For all you lazy Git bums, here’s how you can type “g” got “git” and still have autocompletion (paste into your ~/.bash_profile or .dotfiles system):

alias g='git'
complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
    || complete -o default -o nospace -F _git g

Let jRuby and Pow!™ be happy together

Turns out that Pow! won’t support jRuby anytime soon but the trick described in josh/nack#32 actually works, so rollup your shirt’s sleeves!

Go open node_modules/nack/lib/nack/server.rb inside ~/Library/Application Support/Pow/Current and replace sock.close_read on line 132 with:

# WAS: sock.close_read
rescue IOError
  raise unless RUBY_ENGINE == 'jruby'

Happy hacking!

rvm install opal

Get back Rails backtrace links on Safari 6

If you’re back to Safari and want your clickable Rails backtraces here’s the solution: Download RailsnStacktracenlinks.safariextz

Thanks to http://kangoextensions.com that made supereasy transform a userscript into a Safari extension.

Links again!

[slides] Rails views for the OO-savvy Rubyist

Rails OO views from Elia Schito

note: transitions and music are lost with slideshare, but you can get an idea of them in this video:

I'll speak at RubyDay.it tomorrow!

I was asked to write about the talk I’ll give tomorrow at RubyDay.it.

Without giving away too much here’s a post I wrote some time ago about @instance_variables usage in Rails implementation.

Last I add a promise: it will be fun!

Cross posted from MIKAMAYHEM

“Use Node.js FOR SPEED” — @RoflscaleTips

So I obeyed, and wrote the opal NPM package, this way, finally, Rails and opal-rails are no longer necessary. Here’s my first node app:

server = Server.new 3000
server.start do
  [200, {'Content-Type' => 'text/plain'}, ["Hello World!\n"]]

For it I had to write the Server class:

class Server
  def initialize port
    @http = `require('http')`
    @port = port

  def start &block
      this.http.createServer(function(req, res) {
        var rackResponse = (block.call(block._s, req, res));
        res.end(rackResponse[2].join(' '));

Put them in app.opal and then start the server:

$ npm install -g opal      # <<< install the NPM package first!
$ opal-node app

The running app

YAY! now I can roflSCALE all of my RAils apps!!1

Stop writing JavaScript and get back to Ruby!

Remeber that feeling that you had after having tried CoffeeScript getting back to a project with plain JavaScript and felt so constricted?

Well, after re-writing a couple of coffee classes with OpalRuby I felt exactly that way, and with 14.9KB of footprint (min+gz, jQuery is 32KB) is a complete no brainer.

So let’s gobble up our first chunk of code, that will salute from the browser console:

puts 'hi buddies!'

You wonder how that gets translated?

Fear that it will look like this?

='user.agent',hb='webkit';var fc=window,k=document,ec=fc.__gwtStatsEvent?functi
on(a){return fc.__gwtStatsEvent(a)}:null,zc,pc,kc,jc=l,sc={},Cc=[],yc=[],ic=[],
vc,xc;ec&&ec({moduleName:m,subSystem:x,evtGroup:cb,millis:(new Date()).getTime(
riptsLoaded){fc.__gwt_scriptsLoaded={}}function oc(){var b=false;try{b=fc.exter

Nope! it’s Chuck Testa

(function() {
  return this.$puts("hi buddies!")

But it will be obviously a mess to integrate it with existing javascript!!1

Let’s how to add #next and #prev to the Element class:

class Element
  def next(selector = '')
    element = nil
    `element = jQuery(this.el).next(selector)[0] || nil;`
    Element.new element if element

But I’m on Rails!

Here you served:

gem 'opal-rails'

Which will provide you requirable libraries for application.js

//= require opal
//= require rquery

and the .opal extension for your files

# app/assets/javascripts/hi-world.js.opal

puts "G'day world!"

A template handler:

# app/views/posts/show.js.opal.erb

Element.id('<%= dom_id @post %>').show

and a Haml filter!

-# app/views/posts/show.html.haml

%article= post.body

%a#show-comments Display Comments!

  - post.comments.each do |comment|
    .comment= comment.body

  Document.ready? do
    Element.id('show-comments').on :click do
      false # aka preventDefault

Improve browser image rendering in Rails

Improve browser image rendering in Rails

Use dimensions by @sstephenson to add automatic images size and improve browser rendering

UPDATE: I just released the dimensions-rails gem which adds default width and height to the image_tag helper!

Open the current url from TextMate

Whenever you’re within some text in TextMate you can see the urls properly underlined. Today I finally (and accidentally) discovered how to open those urls in the browser.

The command is and on my macbook can be reached by using together fn and .

Now that I know it I can start implementing the lynx bundle!

Jedi training on Cesano Boscone (the planet)

Another reason to avoid instance variables into Rails controllers? /2

They have an insane implementation:

(still in rails head)

# Define some internal variables that should not be propagated to the view.
self.protected_instance_variables = [
  :@_status, :@_headers, :@_params, :@_env, :@_response, :@_request,
  :@_view_runtime, :@_stream, :@_url_options, :@_action_has_layout

Another reason to avoid instance variables into Rails controllers?

They can’t be deprecated. (at least AFAIK)

Now go, and add gem 'decent_exposure' to your Gemfile.

Awkward Rails 2 routes

Don’t use parenthesis to mark a parameter as optional in a Rails 2 route

Set subdomain in Rails 3 request specs

Every request spec runs inside an Integration::Session hence just add:

before { host! "mysubdomain.#{host}" }

This of course works for Rails3+Rspec2.

Quiet volume change on the Mac

hold SHIFT while changing the volume and you won’t hear the feedback sound

Rails2/RSpec1 subdomain testing (with SubdomainFu)?

Just stub SubdomainFu.current_domain like this in your spec_helper (or test_helper)

class << SubdomainFu
  def current_subdomain= subdomain
    @current_subdomain = subdomain
  alias original_current_subdomain current_subdomain
  def current_subdomain request
    @current_subdomain || original_current_subdomain(request)

and in your specs before (or test setup):

SubdomainFu.current_subdomain = fair.domain

Note: This works for me with Rails2 + RSpec1 acceptance specs done with Steak and Capybara

Have you fought the rvm(rc) battle?

Are you fighting with the rest of your team on custom .rvmrc settings?

Recently I had a discussion about the use of gemsets. “questionable personal taste” I said, “vital company standard” said others…

A local .rvmrc to the rescue

Put this in the common .rvmrc, put your personal prefs (like gemsets) in your (.gitignore'd) .rvmrc.local and make peace!

# Load a local .rvmrc
test -f .rvmrc.local && source .rvmrc.local