Use Session Sparingly (or The Benefits of Flash)

As tempting as it is to use it for everything, the rails session should be used sparingly. In reading through the code for my current project, I noticed that we are using the session for storing return urls and query strings when doing login redirects. This is bad.

Read the rest of this entry »

Comments

Ruby Reminder: Array Subtraction

Just a reminder when working with arrays of objects generated with Active Record.

[Model.find(1), Model.find(2), Model.find(3)] -
  [Model.find(1)] != [Model.find(2), Model.find(3)]

This does not behave as you would expect, because array subtraction compares elements on their object ids. Each find generates a new object, with a new id, so subtraction does not work. To get the behavior desired, do this instead:

to_be_removed = [Model.find(1)].map{|m| m.id}
[Model.find(1), Model.find(2), Model.find(3)].
  delete_if{|m| to_be_removed.include?(m.id)}

It’s not as beautiful, but it gets the job done.

Comments (1)

Where am I?

I was really on a roll there for a while, right? A gem of a post every day or so, presentations at meetings, reading on scheme and now, I’m nowhere to be found. Well, please allow me to explain. It goes like this: got new job, quit old job, working both jobs for two weeks until finished with my final two weeks.

So yes, working two jobs is keeping my quite busy right now, leaving very little time for any decent posting. Wait until next week, when I’ll once again make you privy to the thoughts bubbling around in my head.

In the meantime, I’ll leave you with a nice unordered list of what’s been catching my eye:

  • Camping is awesome.
  • Zed Shaw and his Mongrel project rock!
  • I want to build a proxy server in ruby using mongrel and mongrel plugins but don’t know when I’ll have time.
  • I want a piece of software that will scan and parse weather, news headlines, and my top rss feeds, placing them into a video file that I can watch on my iPod.
  • Java will probably move into the same category as VB in my about page.

You can look look forward to (or know to avoid) posts on these topics as soon as I’m able to return.

Comments

Picks and Keyboards

I read an article a few years ago, I believe in Guitar World, written by famed Phish guitarist Trey Anastasio. In it, he explained that one of the keys to being great at guitar is to know how to get to ‘C’ from any other note on the fretboard. Once this is known, he explains, this knowledge can be transferred to every other key.

So what does this have to do with programming? A lot I believe. Read the rest of this entry »

Comments

Ruby Makes It Easy

Oh ruby, you make my job so simple. I need a class method you’re lacking, and you just let me add it to you.

class Array
  def map_with_index(procedure=null)
    result = []
    if block_given?
      each_with_index {|item, i| result[i] = yield(item, i) }
    elsif procedure
      each_with_index {|item, i| result[i] = procedure.call(item, i) }
    else
      result = self
    end
    return result
  end
  alias :collect_with_index :map_with_index
end

[1, 2, 3, 4].map_with_index {|elem, i| elem + i } => [1, 3, 5, 7]

See how easy that was?

Comments

nyc.rb In The House!

Last night’s meeting was a smashing success. We had our largest turnout ever with what I think was 28 people. My presentation went very well, with only a few minor corrections coming from the illustrious David Black. For those of you following along via the web, I’ve updated the copy of the presentation available here to reflect those corrections. Moving past my accomplishments, Zed Shaw gave a highly informative presentation on the use of statistics to gauge performance. Also, Greg Brown talked about his reporting library Ruport, which looks very interesting. Needless to say, the meeting was amazing, and I hope to see you at the next one.

Comments

Getting to Done

Tonight’s presentation on higher order procedures is now listed on the Presentations page to your right. I’ll be adding some practice exercises later, but feel free to check out what I have already.

Comments

Objects from Scratch

Look ma, I made my own object! It doesn’t handle state that well, but at least it can be passed messages and told to do things.

def make_number(x)
  lambda do |op, *y|
    if op == '+'
      make_number(x + y[0])
    elsif op == '-'
      make_number(x - y[0])
    elsif op == 'val'
      x
    end
  end
end

my_num = make_number(5)         => <#Proc>
my_num.call('+', 7).call('val') => 12

In case you can’t tell, this object creation makes use of higher order procedures. The make number procedure returns the anonymous procedure created by lambda. This form of object creation is not terribly practical, but it sure is cool.

Comments

« Previous Page« Previous entries « Previous Page · Next Page » Next entries »Next Page »