I like to use descriptive variable names, and I try to err on the side of more-descriptive if I think there's
any chance of confusion.
isn't terribly long, but if you're building
up all of its members from different sources (i.e., you can't really loop over it), it can get cumbersome
to type and worse, to read
. Moreover, it's different from just "participants" and they certainly
aren't "contracts," so shortening it in this case wasn't going to happen.
contract_participant.first_name = "Joe"
contract_participant.last_name = "Smith"
contract_participant.on_drugs? = params[:on_drugs?]
It doesn't really violate the spirit of DRY
, but it's
I wanted to just have a scope for contract participants and not have to type it every time:
first_name = "Joe"
last_name = "Smith"
on_drugs? = params[:on_drugs?]
Unfortunately, unlike some languages, Ruby doesn't have such a construct. Fortunately, it does have
the facilities to create one. I thought there was already an implementation, but I couldn't find it at
first. So I started to write my own.
For some reason I was looking through Utility Belt
it had an implementation of
def with(object, &block)
Unbelievable! My implementation was running into a WTF in its own right, and here was this one-liner.
Unfortunately, Utility Belt's
didn't pass the test cases that demonstrate
how I wanted to use it
, so I had to move on.
was created as a result of those efforts. It
works how I want it to in the simple cases I've demonstrated. It still needs some work on left-hand-side
variables that are not members of the aforementioned
. It needs some tests for
more complex uses like nested blocks (and
code to make them pass). But it works for what I imagine the majority usage would be.
I opted for the syntax
With.object(foo) do ... end
so as to not force
you to use a
. However, you can
if you prefer
to just use
. There's also a conditional patch on
does not exist. It is added and removed in the same method.
It requires Ruby2Ruby
to do its magic, though that will change soon due to compatibility problems with Ruby 1.9 and other implementations
(for which another project exists, if I've read correctly).
To install, you can use:
sudo gem install codeodor-with -s http://gems.github.com
Let me know
if you're using it and encounter any problems. I'd like to fix them quickly, if I can.
Hey! Why don't you make your life easier and subscribe to the full post
or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate
wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!
Leave a comment
There are no comments for this entry yet.
Leave a comment