posted 6 months ago on July 18th, 2011 at 18:18 via buzz /
posted 7 months ago on June 15th, 2011 at 16:47 via hailtothehammer /

«I’m not able to open PDF files - I don’t use a Mac. Luckily, my assistant was able to open your PDF, print it, scan it and save it as a JPG. I have to say, the colors you’ve chosen are horrible. Everything looks muddy. But I do like how the whole picture is slanted; it looks totally MTV.»

— (via clientsfromhell)

posted 11 months ago on March 6th, 2011 at 00:42 via clientsfromhell /
posted 11 months ago on March 5th, 2011 at 20:44 via thecultureofme /
blackandwtf:

1865
  This Grizzly Bear Chair was a  gift from a hunter named Seth Kinman to US president Andrew Johnson.
(via theclearlydope)

I want that

blackandwtf:

1865

This Grizzly Bear Chair was a gift from a hunter named Seth Kinman to US president Andrew Johnson.

(via theclearlydope)

I want that

posted 11 months ago on March 4th, 2011 at 18:12 via blackandwtf /
posted 11 months ago on February 22nd, 2011 at 13:49 via nilmethod /

Revisiting rebinding self on lambdas

In my previous post on the topic I determined the best method would be to pass the current self into the lambda:

fun = -> context {context.thing}
fun.call(self)

But in a recent project I revisited the problem and discovered you can do this:

class Deal
  def emit_a_funky(funk)
    #returns a lambda which takes a name argument and calls the lambda
    #passed in with the name argument and self rebound to be an instance of Deal.
    -> name do
      self.instance_exec(name, &funk)
    end
  end

  def doathing(name)
    p "hey #{name} I'm doin a thing"
  end
end

a = -> name do
  doathing(name)
end

b = Deal.new
c = b.emit_a_funky(a)
c.call 'max'
# prints "hey max I'm doin a thing"
c.call 'Hulk Hogan'
# prints "hey Hulk Hogan I'm doin a thing"

The actual use case I had for it was writing an RSpec helper that takes a list of user authentication levels and a block and spits out a seperate example for each user level. The examples evaluate the block in the context of the RSpec example and make an assertion on the block’s return value. It came in handy for DRYing up my specs:

def assert_granted(*args, &blk)
  if args.last.instance_of?(Hash)
    options = args.pop
    options = Hash[options.collect {|option,value| [option.to_sym, value]}]
  else
    options = {:granted => true}
  end

  if args.length == 0
    args = [:user, :fulfillment_admin, :engine_admin, :admin]
  end

  message = options[:granted] ? 'is' : 'is not'
  args.each do |name|
    it "#{message} granted for #{name}" do
      auth_level = User.auth_levels.invert[name.to_sym]
      user = stub_grant_current_user(auth_level)
      assert_grant_error(!options[:granted]) do
        #rebind self for blk
        self.instance_exec(user, &blk)
      end
    end
  end
end

def assert_grant_error(raises = true, &blk)
  assertion = raises ? 'should' : 'should_not'
  blk.send(assertion, raise_error(Grant::Error))
end

There are still instances where passing the current self as an argument to a lambda may be useful, but this makes it unnecessary in cases where you don’t need access to both contexts.

posted 11 months ago on February 22nd, 2011 at 13:35 /

Nested joins with Arel

Today I had to convert this old Active Record finder to the new Arel backed Active Record:

@promotional_offers = Offer.find(:all, :joins => {:business => :city}, :conditions => {:businesses => {:cities => {:id => @city.id}}})

I couldn’t quickly find docs for Arel on how to do this, but after a couple stabs at it I discovered that this does the trick

 @promotional_offers = Offer.joins(:business => :city).where(:businesses => {:cities => {:id => @city.id}})

It’s good to know that Arel’s joins method takes a hash so we can do nested joins with relative ease.

posted 1 year ago on January 19th, 2011 at 18:50 /
drudgeons:

gemma correll is the best.

drudgeons:

gemma correll is the best.

posted 1 year ago on December 15th, 2010 at 17:20 via drudgeons /

Ruby Procs and their execution context

I had a problem just now which involved creating a proc in one class, and executing it in the context of a different class. I pondered the problem for a little while, did a little research, and then realized the obvious solution would be to pass the current self into the proc.

Example:

stuff = lambda do |context, thing|
  context.some_method(thing)
end

Thought I might save someone the trouble of thinking of that somewhat obvious solution.

posted 1 year ago on November 4th, 2010 at 17:59 /