Pivotal Labs

Main menu

Skip to primary content
Skip to secondary content
  • About
  • Case Studies
  • Team
    • Executives
    • Locations
      • San Francisco (HQ)
      • Boston
      • Boulder
      • Denver
      • London
      • Los Angeles
      • New York
  • Community
    • Blogs
    • Tech Talks
    • Events
  • Careers
    • Lifestyle
    • Principles & Practices
    • Benefits
    • FAQ
    • Apply
  • Tools
  • Contact
    • Press Room
    • Press Releases
    • In The News
    • Press Kit
  • All
  • Labs
  • Standup
  • Tracker

Monthly Archives: August 2010

Will Read

Standup 2010-08-20: Database Woes and Eval Nuances

Will Read
Sunday, August 22, 2010

Help!

A Pivot is working on a project where 1,500 records living in a CSV are iterated over (loaded in to ActiveRecord objects) and loaded in to a Postgres database… or at least that was the intention. The database just crashed on him in a repeatable way. Anyone seen anything like this before?

On another project, there’s a has_many association on a table with 40,000 records in MySQL. It seems that pulling these in through standard ActiveRecord means is not releasing memory (you can watch the memory grow as it executes). The work around was to use ActiveRecord::Base.connection.select_all(), but it would be nice to know why GC didn’t kick in and clean up house. Maybe AR is holding on to object references somewhere outside of scope?

Interesting Things

Speaking of scope, there’s a subtle difference in Ruby 1.9.2 that a pivot ran into – previously you could set variables inside a call to eval() and have that variable defined in the same scope as the call. That is no longer the case in 1.9.2 where eval has its own scope inside the call.

And lastly, on this day in 19..(mumble mumble), NASA launched the Voyager 2. Later in its career, Voyager was given it’s own drama series and a female captain to command the vessel.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Pivotal Labs

Adding Routes for tests / specs with Rails 3

Pivotal Labs
Saturday, August 21, 2010

If you ever need to test an abstract base controller independently from any subclass, you’ll likely need to add a route for your test. Here’s one example of how to do this in Rails 3:

*UPDATE: * Thanks to the tip by Austin Putman I’ve updated the gist to include an example use of the excellent with_routing method.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Mike Gehard

Using ActiveSupport::Concern for easy mix-ins…

Mike Gehard
Friday, August 20, 2010

If you are writing you own mix-in modules in Rails3 and haven’t taken a look at ActiveSupport::Concern yet, I recommend checking out this blog posting for an outline of why you should be using it:

http://www.strictlyuntyped.com/2010/05/tweaking-on-rails-30-2.html

If you are already using this then good for you and you can continue on.

Here’s a little “stumbling block” with ActiveSupport::Concern that I found the other day that I wanted to share with people. If you have the following code:

module Bar
  extend ActiveSupport::Concern

  included do
    attr_accessor :baz
  end

  def baz
    "Baz"
  end
end

class Foo
  include Bar
end

puts Foo.new.baz

You might expect the puts to write out “Baz” but it writes out nil instead. Why is that? It has to do with the order in which ActiveSupport::Concern tacks all of the module code into the class including the module. If you
change “attr_accessor” to attr_writer” all works as planned.

So yes you might be saying “why do you have attr_accessor when you define a getter method for baz?” and my response is “because it worked ok before I factored the code out into a module for reuse”.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Pivotal Labs

JavaScript constructors, prototypes, and the `new` keyword

Pivotal Labs
Friday, August 20, 2010

Are you baffled by the new operator in JavaScript? Wonder what the difference between a function and a constructor is? Or what the heck a prototype is used for?

I’m going to lay it out straight.

Now, there’s been a lot of talk for a long time about so-called “pseudo-classical” JavaScript. Mostly, the new guard of JavaScript folk don’t like to use the new keyword. It was written into the language to act more like Java, and its use is a little confusing. I’m not going to take sides here. I’m just going to explain how it works. It’s a tool; use it if it’s practical.

What is a constructor?

A constructor is any function which is used as a constructor. The language doesn’t make a distinction. A function can be written to be used as a constructor or to be called as a normal function, or to be used either way.

A constructor is used with the new keyword:

var Vehicle = function Vehicle() {
  // ...
}

var vehicle = new Vehicle();

What happens when a constructor is called?

When new Vehicle() is called, JavaScript does four things:

  1. It creates a new object.
  2. It sets the constructor property of the object to Vehicle.
  3. It sets up the object to delegate to Vehicle.prototype.
  4. It calls Vehicle() in the context of the new object.

The result of new Vehicle() is this new object.

1. It creates the new object.

This is nothing special, just a fresh, new object: {}.

2. It sets the constructor property of the object to Vehicle.

This means two things:

vehicle.constructor == Vehicle  // true
vehicle instanceof Vehicle      // true

This isn’t an ordinary property. It won’t show up if you enumerate the properties of the object. Also, you can try to set constructor, but you’ll just set a normal property on top of this special one. To wit:

vehicle;                          // {}

var FuzzyBear = function FuzzyBear() { };
vehicle.constructor = FuzzyBear;

vehicle;                          // { constructor: function FuzzyBear() }
vehicle.constructor == FuzzyBear; // true
vehicle instanceof FuzzyBear      // false
vehicle instanceof Vehicle        // true

The underlying, built in constructor property is something you can’t set manually. It can only be set for you, as part of construction with the new keyword.

3. It sets up the object to delegate to Vehicle.prototype.

Now it gets interesting.

A function is just a special kind of object, and like any object a function can have properties. Functions automatically get a property called prototype, which is just an empty object. This object gets some special treatment.

When an object is constructed, it inherits all of the properties of its constructor’s prototype. I know, it’s a brainful. Here.

Vehicle.prototype.wheelCount = 4;
var vehicle = new Vehicle;
vehicle.wheelCount;         // 4

The Vehicle instance picked up the wheelCount from Vehicle‘s prototype

Now this “inheritance” is more than simply copying properties to the new objects. The object is set up to delegate any properties which haven’t been explicitly set up to its constructor’s prototype. That means that we can change the prototype later, and still see the changes in the instance.

Vehicle.prototype.wheelCount = 6;
vehicle.wheelCount;         // 6

But if we like, we can always override it.

vehicle.wheelCount = 8;
vehicle.wheelCount          // 8
(new Vehicle()).wheelCount  // 6;

We can do the same thing with methods. After all, a method is just a function assigned to a property. Check it.

Vehicle.prototype.go = function go() { return "Vroom!" };
vehicle.go();                              // "Vroom!"

4. It calls Vehicle() in the context of the new object.

Finally, the constructor function itself is called. Inside the function, this is set to the object we’re constructing. (Why? Because that’s what Java does.) So,

var Vehicle = function Vehicle(color) {
  this.constructor;       // function Vehicle()
  this.color = color;
}

(new Vehicle("tan")).color;   // "tan"

Side note: Above, I said the use of the new keyword returned the constructed object. This is correct unless the constructor returns something explicitly. Then that object is returned, and the constructed object is just dropped. But really. JavaScript slaves over a hot CPU to create this object for you and then you just throw it away? Rude. And confusing to people who use your constructor. So unless you have a really good reason, don’t return anything from constructor functions.

Putting it all together

Given this tool, here’s one way (the intended way, but not the only way) to implement something like classes in JavaScript.

// Class definition / constructor
var Vehicle = function Vehicle(color) {
  // Initialization
  this.color = color;
}

// Instance methods
Vehicle.prototype = {
  go: function go() {
    return "Vroom!";
  }
}

“Subclassing”

This “pseudoclassical” style doesn’t have an exact way to make subclasses, but it comes close. We can set the prototype of our “subclass” to an instance of the “superclass”.

var Car = function Car() {};
Car.prototype = new Vehicle("tan");
Car.prototype.honk = function honk() { return "BEEP!" };
var car = new Car();
car.honk();             // "BEEP!"
car.go();               // "Vroom!"
car.color;              // "tan"
car instanceof Car;     // true
car instanceof Vehicle; // true

Now, there’s a problem here. The Vehicle constructor only gets called once, to set up Car‘s prototype. We need to give it a color there. We can’t make different cars have different colors, which is not ideal. Some JavaScript frameworks have gotten around this by defining their own implementations of classes.

And for my last trick…

Sometimes you don’t want a notion of classes. Sometimes you just want one object to inherit the properties of another (but be able to override them). This is how most prototype-based languages work, but not JavaScript. At least, not without a little massaging.

This function lets us accomplish it. It’s been tossed around for a long time and is sometimes called “create” and sometimes “clone” and sometimes other things.

function create(parent) {
  var F = function() {};
  F.prototype = parent;
  return new F();
}

var masterObject = {a: "masterObject value"}

var object1 = create(masterObject);
var object2 = create(masterObject);
var object3 = create(masterObject);
var object3.a = "overridden value";

object1.a; // "masterObject value"
object2.a; // "masterObject value"
object3.a; // "overridden value"

masterObject.a = "new masterObject value"

object1.a; // "new masterObject value"
object2.a; // "new masterObject value"
object3.a; // "overridden value"

You said a mouthful.

The JavaScript prototype chain is a little different than how most languages work, so it can be tricky understand. It doesn’t make it any easier when JavaScript gets syntax that makes it looks more like other languages, like inheriting Java’s new operator. But if you know what you’re doing, you can do some crazy-cool things with it.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Mike Gehard

Capybara/Cucumber and waiting for Javascripty stuff to happen

Mike Gehard
Friday, August 20, 2010

A little ditty that makes it really easy to wait for something Javascripty to happen…like a Javascript dialog to appear that gets populated from an AJAX call.

The has_css? method call will wait until the element shows up and if it doesn’t show up before the Capybara timeout expires then it will return false.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Pivotal Labs

Standup 8/19/2010: Bundler and RVM Gemsets, Rails 3 and JQuery, Why Day

Pivotal Labs
Thursday, August 19, 2010

Interesting

  • Bundler and RVM Gemsets work! For a while now, Bundler has been putting gems in the system gems location (instead of a private folder as it did previously). This means that it now works well with RVM Gemsets. Use It.
  • Rails 3 & JQuery: Someone has written a nice generator that will unplug Prototype from Rails 3 and install JQuery.
  • Ruby 1.9.2 was released yesterday.
  • Today is international Why Day, commemorating the day that Why the Lucky Stiff disappeared from the online community. Interestingly August 19th is also the day that 3 witches where put on trial in Samlesbury, England in 1612 and that 5 witches where executed in Salem, Massachusetts in 1692. Coincidence? I think not.
  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Pivotal Labs

Standup 8/18/2010: Time#to_json in milliseconds, Encoded vs. encrypted Session Cookies

Pivotal Labs
Wednesday, August 18, 2010

Interesting

  • Make Ruby Time#to_json always return time in milliseconds: It turns out that while Firefox and Chrome can, Safari cannot parse the default time format that Time#to_json produces. The team decided to override Time#as_json to return an integer number of milliseconds, which to_json will then render into a string (JavaScript can easily work with number-of-milliseconds-since-the-start-of-the-epoch).
  • A pivot wanted to remind everyone that in Rails 2.x, session cookies are not encrypted. Reassuringly, all present were already aware of this. Session cookies are Base64 encoded, and if you ever need to take a look at their contents, here’s how. If you want to encrypt your session cookies, there are Rails plugins available for that purpose.
  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Mike Gehard

Testing Resque with Cucumber

Mike Gehard
Wednesday, August 18, 2010

For some reason the Universe keeps sending great Cucumber related stories to me via Twitter.

Here is another great one from the folks at Square on how to test Resque based functionality via Cucumber:

http://corner.squareup.com/2010/08/cucumber-and-resque.html

  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Pivotal Labs

Standup 8/17/2010: RubyMine shortcuts, client visible Cucumber results

Pivotal Labs
Tuesday, August 17, 2010

Help

  • RubyMine Keyboard Shortcuts:

    • How do I move from one side of a split editor window to the other side? Answer: Ctrl-Tab.
    • So how do I figure this out for myself?
      Helpful-Non-Answers: Cmd-Shift-A pulls up a search box that you can type in commands (e.g. “move”) and get suggestions. Preferences -> Keymap has an even better search box, and also a reverse-search (click the funnel next to the search box) that lets you type in a keystroke and see what command it is. Of course, unless you know that the thing you need is called the “Switcher”, you’re still not going to find out. Sometimes you just have to resort to interacting with another human (let’s call it “using the meat-net”). Company wide standups FTW.
  • How do I make the results of our integration tests (e.g. using cucumber) easily visible to a non-technical product owner by putting them on a web-page, a là Fit? One answer: use the –format html option, capture the output and copy to a web server. One project also has a url that a client can visit that will cause the cucumber tests to be run.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Mike Gehard

Musings on the use of Cucumber

Mike Gehard
Sunday, August 15, 2010

I found this article to be quite enlightening about the use of Cucumber , and more specifically the “verbiage” of Cucumber features.

http://elabs.se/blog/15-you-re-cuking-it-wrong

Make sure you read down through the comments as there are some interesting replies from both the people behing Cucumber and Pickle.

I’ve always gotten that “not so fresh” feeling when including CSS selectors in my Cucumber features and now I have a reason why.

This article becomes really important if you start to have your clients define their Tracker stories as Cucumber features, something I am toying with right now. Just think of how much development time you could save if all you needed to do was copy/paste a feature from Tracker into a feature file. Sure it may never happen 100% of the time but even if it only 10% of the time it is still more than 0%.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter

Topics

  • agile (783)
  • rails (117)
  • testing (90)
  • ruby (86)
  • ruby on rails (71)
  • jobs (62)
  • javascript (59)
  • techtalk (44)
  • ironblogger (42)
  • rspec (39)
  • bloggerdome (34)
  • productivity (34)
  • activerecord (30)
  • rubymine (30)
  • git (29)
  • gogaruco (29)
  • nyc (27)
  • design (24)
  • mobile (23)
  • pivotal tracker (22)
  • process (21)
  • cucumber (21)
  • jasmine (19)
  • ios (18)
  • tracker ecosystem (17)
  • webos (17)
  • objective-c (17)
  • fun (16)
  • android (16)
  • palm (16)
  • ci (16)
  • "soft" ware (16)
  • bdd (15)
  • tdd (15)
  • cedar (15)
  • rails3 (14)
  • performance (14)
  • css (14)
  • gem (13)
  • mouse-free development (12)
  • selenium (12)
  • goruco (12)
  • bundler (12)
  • api (12)
  • keyboard (11)
  • meetup (11)
  • railsconf (11)
  • nyc-standup (11)
  • capybara (10)
  • mac (10)
Subscribe to Community Feed
  1. ←
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. →
  • About
  • Case Studies
  • Team
  • Community
  • Careers
  • Tools
  • Contact
  • Labs
  • Events

Contact Us

contact@pivotallabs.com
+1 415-77-PIVOT
TwitterLinkedInFacebook

Pivotal Tracker

Tracker is the award-winning agile project management tool that enables real-time collaboration around a shared, prioritized backlog.
Visit pivotaltracker.com >