Skip to content

existence, refactored

With kindness comes naïveté. Courage becomes foolhardiness. And dedication has no reward.

Archive

Category: Programming

Some posts just write themselves. Today’s post comes from my reply to a guy in PhRUG who still thinks you need a Mac before you can develop Rails applications.

windows and ruby

This is probably the biggest problem the Ruby/Rails community has when trying to spread the word in this country: the lack of interest in supporting Windows.

I mean, a typical response to the legitimate question “I’m using Windows, how to I practice RoR?” is the fanboy answer: “Get a Mac!”

And that, my dear readers, is a dick move. If I was an average college student and you told me that, I’ll immediately think “WTF?!? I just want to try out this open-source language and web framework and I need to shell out a couple of years worth of tuition?!?

Answering “Format your hard drive and install Linux” is less of a dick move, but a dick move nonetheless.

Thus, if we rubyists want to spread the word about Ruby, we’ll have to make Windows a viable OS for Ruby development. Here are a few options available to us:

continue reading…

Problem:

Rails doesn’t automatically convert newline (“\n“) characters in strings to line breaks (“<br>“) with h.

<%= h @user.address %>

Solution:

Manually replacing the \n with <br> without using h will make your site vulnerable to XSS attacks. The proper approach would be to convert the line breaks after h. Note that Rails already has a method, simple_format, that converts line breaks.

<%= simple_format(h @user.address) %>

Problem:

The following line of code for calculating the total order cost returns “#”.

@purchase_items.reduce() {|cost, item| cost += item.qty_ordered * item.unit_price }

Cause:

The default memo item for a reduce operation in Ruby is the first item on the list (because of this, it starts the reduce on the second item).

Solution:

You can set the initial memo item by passing it to the reduce function.

@purchase_items.reduce(0) {|cost, item| cost += item.qty_ordered * item.unit_price }