Episode 6: Shortcut Blocks with Symbol to_proc

Posted about 7 years back at Railscasts

It may have a goofy syntax, but the Symbol#to_proc feature Rails adds allows you to do simple blocks very quickly and easily.

AR Model Initialization Notes

Posted about 7 years back at zerosum dirt(nap) - Home

If you’re new to Rails but not to Ruby, you might be surprised to learn that Rails doesn’t use the standard Ruby initialize() method when Model.new is invoked or when a model instance is returned from a find.

If you need to add some initialization code to an ActiveRecord model, use the after_initialze callback instead:

def after_initialize
  @thing = SomethingElse.new(self)
  @foo = 'bar'
end

Kid

Posted about 7 years back at The Hobo Blog

Any of you Ruby folk taken a look over the fence into Python land and seen Kid templates?

They call it an “attribute language” because unlike DRYML, there are smart attributes rather than smart tags. e.g. from their docs:

<p py:if="system() == 'Linux'">
  Good for you!
</p>

When you’ve stopped sniggering about those empty brackets, you might agree that it’s rather elegant and compact – particularly for this specific example: a conditional.

Over in the forums we’ve been chatting about how all the if_this, if_that tags in DRYML are kinda icky. I’m wondering about supporting this style just for if. Something like

<p if="logged_in?">Logged in as <display_name obj="#current_user"/></p>

Is that just too inconsistent when everything else is done with tags? How about the else? Whadayathink?

p.s. I was going to post this in the forums, but I thought I’d give this way a try. Let me know if you think this is noise in the context of the blog.

So that’s two questions for you. I just put a fresh pad of blank comments in the server so knock yourself out.

Kid

Posted about 7 years back at The Hobo Blog

Any of you Ruby folk taken a look over the fence into Python land and seen Kid templates?

They call it an “attribute language” because unlike DRYML, there are smart attributes rather than smart tags. e.g. from their docs:

<p py:if="system() == 'Linux'">
  Good for you!
</p>

When you’ve stopped sniggering about those empty brackets, you might agree that it’s rather elegant and compact – particularly for this specific example: a conditional.

Over in the forums we’ve been chatting about how all the if_this, if_that tags in DRYML are kinda icky. I’m wondering about supporting this style just for if. Something like

<p if="logged_in?">Logged in as <display_name obj="#current_user"/></p>

Is that just too inconsistent when everything else is done with tags? How about the else? Whadayathink?

p.s. I was going to post this in the forums, but I thought I’d give this way a try. Let me know if you think this is noise in the context of the blog.

So that’s two questions for you. I just put a fresh pad of blank comments in the server so knock yourself out.

Nearly forgot

Posted about 7 years back at The Hobo Blog

If you are updating an existing app that uses Rapid to Hobo 0.5, you will need to add the following at the top of your views/hobolib/application.dryml

<taglib src="plugins/hobo/tags/rapid"/>

<set_theme name="default"/>

Nearly forgot

Posted about 7 years back at The Hobo Blog

If you are updating an existing app that uses Rapid to Hobo 0.5, you will need to add the following at the top of your views/hobolib/application.dryml

<taglib src="plugins/hobo/tags/rapid"/>

<set_theme name="default"/>

Hobo 0.5 and more...

Posted about 7 years back at The Hobo Blog

It was 17 and a half hours in the end but we seem to have made it. Although without a great deal of surplus energy for a riveting blog post!

Hope you enjoy the new site – we’ve tried to have a bit of fun with it :-). Also don’t miss: two new screencasts, the docs, and of course Hobo 0.5 (changelog).

Let me know what breaks!

ZZZZZzzzzzz…

Hobo 0.5 and more...

Posted about 7 years back at The Hobo Blog

It was 17 and a half hours in the end but we seem to have made it. Although without a great deal of surplus energy for a riveting blog post!

Hope you enjoy the new site – we’ve tried to have a bit of fun with it :-). Also don’t miss: two new screencasts, the docs, and of course Hobo 0.5 (changelog).

Let me know what breaks!

ZZZZZzzzzzz…

Docs

Posted about 7 years back at The Hobo Blog

The Hobo Tutorial

Stop Press! The new, improved and up-to-date (Hobo 0.7.1) tutorial is now available!

beta.hobocentral.net

Not only that, but the source-code to beta.hobocentral.net is available! The idea is that this source-code will heavily commented in order to act as a form of documentation. At the moment there’s not a huge amount there, but the model layer has a few nuggets of knowledge in the comments. Also note that a lot of the functionality can be found in vendor/plugins as reusable Hobo bundles. More coming!

svn checkout svn://hobocentral.net/hobo/trunk/hobocentral

Docs

Posted about 7 years back at The Hobo Blog

The Hobo Tutorial

Stop Press! The new, improved and up-to-date (Hobo 0.7.1) tutorial is now available!

beta.hobocentral.net

Not only that, but the source-code to beta.hobocentral.net is available! The idea is that this source-code will heavily commented in order to act as a form of documentation. At the moment there’s not a huge amount there, but the model layer has a few nuggets of knowledge in the comments. Also note that a lot of the functionality can be found in vendor/plugins as reusable Hobo bundles. More coming!

svn checkout svn://hobocentral.net/hobo/trunk/hobocentral

16 hours and counting...

Posted about 7 years back at The Hobo Blog

…the new site is still broken, the docs aren’t done, I’ve got the ‘Hyperactive’ playlist going in iTunes, it’s dark and we’re wearing sunglasses.

Hit it.

(OMG half of you are probably too young to even have heard of that)

16 hours and counting...

Posted about 7 years back at The Hobo Blog

…the new site is still broken, the docs aren’t done, I’ve got the ‘Hyperactive’ playlist going in iTunes, it’s dark and we’re wearing sunglasses.

Hit it.

(OMG half of you are probably too young to even have heard of that)

Episode 5: Using with_scope

Posted about 7 years back at Railscasts

Learn how to use with_scope - a very powerful method which will allow your custom find methods to accept any find options. Just like magic!

Ruby - Power, Simplicity, and Beauty

Posted about 7 years back at Cody Fauser

Tonight while reading through Refactoring by Martin Fowler I was once again amazed by power, beauty, and simplicity of Ruby. Wait, hold on, aren't the code examples in Refactoring in Java? Yes, that is precisely the point. All day I am lucky enough to write Ruby, but sometimes I need to take a look back over the other side of the fence to appreciate where I'm at. Let me tell you that the grass is definitely not green over there.

Consider the simple method being extracted on page 27 of the book:

1
2
3
4
5
6
7
8
9
10

private double getTotalCharge() {
  double result = 0;
  Enumeration rentals = _rentals.elements();
  while (rentals.hasMoreElements()) {
    Rental each = (Rental) rentals.nextElement();
    result += each.getCharge();
  }
  return result;
}

Basically the code is just iterating over the collection of Rental objects and calculating the total charge. Simple enough. The equivalent using inject with Ruby:

1
2
3
4

  def get_total_charge
    rentals.inject{ |sum, rental| sum + rental.get_charge }
  end

Then, with the new sum method added to Enumerable in Rails it gets even simpler:

1
2
3
4

  def get_total_charge
    rentals.sum{ |r| r.get_charge }
  end

This just goes to show the power and expressiveness of Ruby. With so many fewer keystrokes, and lines of code, it is so much easier to focus on the problem at hand.

Episode 4: Move Find into Model

Posted about 7 years back at Railscasts

Move a find into the model to clean up the controllers and remove duplication. Also see how you can call these custom find methods through an association.