Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

dot rand

I was just installing faker and stumbled on the delicious little extension that it makes to Array. Namely, you can call array.rand, to select a random element from it.

This is really sweet. So instead of the clunky expression like

a = ['foo', 'bar', 'baz', 'boozer', 'frell', 'dren']
pick = a[rand(a.length)]

...You just call

a.rand

...It also works with ranges, though you have to splat them first (or call to_i):

[*1..20].rand

...Maybe I'm a weenie for thinking so, but I really, really like this! : )

(Note to shelf: Blogger needs a syntax highlighter!)

Syntactical Sugar: once

It dawned on me that I do things like this a lot:

did_it = false
array.each do |element|
unless did_it
puts "This is the first element"
did_it = true
end
# Do other stuff
end

...Yes, I know there are other ways of accomplishing this, some of which may be far more succinct, but I prefer this one because it's clearest.

Still, everytime I do this, I wince. It seems pretty verbose for the simple procedure of doing something on the first element of a foreach loop.

It would be nice if someone could concoct the syntactic sugar to make that look like this:

array.each do |element|
once do
puts "This is the first element"
end
# Do other stuff
end

...Perhaps the "once" function should be named "on_the_first_time", but whatever floats your boat. I like shorter (unabbreviated) names.

All this needs, of course, is some way of remembering that this function has already been called. Something like this:

def once
yield unless &yield.has_already_been_called
end

However, how to reference (&) the block being called by yield is beyond my ken, as is the magic behind has_already_been_called.

If you have any ideas on how to make this work, I'd like to hear them.

It dawns on me that this could be slightly uglier and probably work... something along the lines of:

def once_for(name)
unless @@already_called.include?(name)
yield
@@already_called[name] = true
end
end

array.each do |element|
once_for(:some_unique_name) do
puts "This is the first element"
end
# Do other stuff
end

...Any thoughnuts?

Programming

I love programming.

There are few things as satisfying as writing a script that does something truly useful. For example, this afternoon, I whipped up a script that discovers incoming files, predicts which files should be there, warns you which of those aren't, looks for matching "deletions" files, counts the number of events in them, moves the original file to the internal directory where we want it, reports on the deletions, and constructs a URI that will process the file.

This is a process that easily eats up 30 minutes of someone's day each time it's done, and we've been doing it for three years now. It took me well under an hour to write.

Isn't that a shame? Three years of processing these files once a month, and with an hour's writing code, it becomes a 5-minute task.

Tickles me pink. In a guilty-pleasure kind of way.

Silence

Introspective week.

[Now Playing: "Square Lamp" by esem... damn, what a fine track this is.]

I've been thinking a lot this week. About work, about quality, about ruby, about miniatures, about drawing... the whole shebang.

Work: I want to write more code. That's the long and the short of it. I'm weak at detail-oriented stuff, and I'm good at algorithms and problem-solving: I should be writing more code. I asked my boss about this today, and he supports the idea; we'll see where that goes. (To be honest, my experiences with such requests are usually taken quite seriously.)

Quality: I went to a local conference regarding the Bladridge criteria yesterday. I rather enjoyed it, though I would argue that I didn't learn anything, really. Rather, it was great idea-fodder, and allowed me some "blocked time" to contemplate how to make use of the criteria at work. ...I also considered working on a "personal criteria" for self-improvement.

Ruby: As I've mentioned, I've been pushing for it at work. There has been surprising support for it, and the lead programmer just asked me to try writing a web-page for a new project using ruby. ...My reflex was to say "no", because I wanted more time to learn it and discover best practices, but I think working on it would be an excellent way to explore the language. The first thing I went looking for was a DBASE-format library... and the first page I stumbled upon was an argument of why the writer had switched from ruby to python. ...And it's a recent article; a well-informed one. His reason, of course, was the robustness of the Py libraries out there.

Of course, this is something that will change with time. ...Or will it? ...I wonder if ruby will really be the last popular language. (Errr... last to rise to power, anyway.) I'm not terribly willing to write new libraries that are missing... I wonder if those who are willing are a dying breed. With the vast libraries available for Java and C++ on the low-end, and Perl and Python on the high-end... It will take a massive force of will to build ruby up to that level. I shudder to think how insurmountable an obstacle that will be for the languages of tomorrow.

Of course, MS has the weight to throw .NET out there, and the world will eat it. (Thank you, MaSter, may I have another?) But that's an exception.

Perhaps that's why DSL's are so popular these days... it's only in the small world of Doman-Specific problem sets that new languages have the ability to emerge.

Of course, the idea of "Very High Level Languages" may be the next big thing. Frameworks on steroids: programming languages built on today's languages. Imagine Rails, but without DHH's "convention over configuration" as a limitation: a meta-language smart enough to look at your database and figure out the schema.

Perhaps I've been drinking the MS water too much lately, since that sounds like "Visual Rails" to me.

Miniatures: Yes. Since playing miniatures with my son a few weeks ago, I've been contemplating building up my own collection. A lot. ...Obessing, really. I've decided to beef up my collection of Star Wars driod miniatures, and for D&D, I'm considering a "Devils and Blues" pool of minis... "blues" being Blue dragons. (The teams in D&D, called "Warbands", are alignment-specific, and blue dragons are LE, like Devils.) ...I'm also big on druids and constructs... but I can't quite come up with a Warband concept that seems solid enough to warrant investing in.

Strangely, my wife doesn't seem disturbed by this, and suggested I just build a collection over time, $20 a month or so. [shrug] I guess I will.

...I tried to stay the urge by installing Neverwinter Nights... but found it didn't scratch the itch.

Drawing: Okay, not much to say here. I've just thought about it a lot, lately. ...I haven't actually drawn much, however. ..A little. Enough to claim "I haven't stopped". But still, it's one of those frustrating situations where you know you should stop thinking and start doing... but something stops you. A Human curse.

Ruby (again)

Well, today... I did it.

I asked the Lead Programmer if we could put our foot down and say: "From here on out, we will write for windows in C#, and for the browser in Ruby."

I was surprised I asked... I had been telling myself to hold off on asking that for a few months... but I'm not good at keeping secrets and I'm impatient when it comes to asking questions: especially questions that improve my lifestyle. So... out it slipped.

I was also surprised at his reaction. I expected him to say "No, and shut up about Ruby already, freak!" But he started by saying "that would be cool", and then arguing on my behalf, saying that "we have more Ruby experience than ASP.NET". (ASP.NET is our current alternative, not counting ColdFusion, which... well... is fine for informational websites, but not for doing actual work. And even for informational websites, it's so much more painful than Rails.) He also pointed out the fact that ... well.. Ruby is free. So it's not like it's a risky investment in that respect.

He didn't want to make a decision, but suggested I "call a meeting" with all the programmers (at least the four core programmers), and argue my point.

I've told myself I would hold off on calling this meeting for a week or two.

We'll see how patient I am with that decision.


...Man, I am such a Kool-aid-drinker when it comes to Ruby. ...And Agile. ...And OO.

Does that make me a chump? (Thanks, Andy.)

Speaking of Agile, we had another pair-programming session (actually, trio-programming, yuk yuk) today. Our code is finally starting to "do stuff". It's a scanning program, and we have it building a list of all the items that need to be scanned (for a given order). That was cool. Today we added the functionality that, when you scan one of the items, it "checks" the item off of the list.

When the program worked, all three of us jumped out of our seats, hooting and hollering.

...Made me feel like a toddler who giggles when he hits the button and the catepillar sings an out-of-tune digital song.

We (programmers) have one of the best jobs on the planet.

Speaking of the scanning software, it's actually quite cool: it talks to a table with a list of barcode patterns (because we don't know the acutal number to be scanned, we only know kinda what it's supposed to look like): we read the patterns out of the DB, run through all the items on the order, and when one of the patterns match, we mark that item as "scanned" (which does all the appropriate work, including ticking off the checkbox in the display). The RegEx includes groups, so that a Strategy Pattern can tell the Barcode object what to do with the number it scans, including all kinds of study-specific work. So something like "^6([AP])(\d{5})$" returns two strings, the first of which tells us what kind of drug it is, and the second which bottle number it is. The initial '6' is just a flag to tell us we're scanning a bottle number: we don't need to remember it. ...That's just an example: the code is actually extremely extensible.

We're all rather proud of ourselves, so far. :)

Ruby

I got paid (for the first time) to write Ruby today.

A co-worker and I set up a new publications database and used Rails to get up the scaffolding for the website.

Of course, the only real "code" I wrote was to display a list of authors related to a partiucular title. I don't have the code in front of me, but it was something like:

<% for author in @title.authors %>
<%= link_to author.fname+' '+author.lname, :action=>'author_show', :id=> author.id %><%= ', ' unless author === @title.authors.last %>
<% end %>

I had to create the title_controller.rb's "author_show" method, which was something like:

def author_show
redirect_to(:controller=>'author', :action=>'show', :id=>params['id']);
end

...And aside from a "has_and_belongs_to_many" from titles to authors and vice-versa... that was all I had to write to get that functionality (of displaying all the authors for a title).

Of course, I'm a little disappointed that I had to put that "<%= ', ' unless author === @title.authors.last %>" in there. Really what I wanted was a ".join(', ')", but I couldn't get that to work: I'm not familiar enough with link_to and... well... with tying bits and pieces of ruby together.

Even still, the way it is is very readable. (Yeah... that's my excuse, and I'm sticking to it!)

The co-worker that I was helping was very impressed with the amount of ground we covered in the three hours we worked on it, anyway. ...Note that over half that time was wasted trying to connect to the SQL Server database... (Turns out there is not trick to it, just set the type to "sqlserver" and fill out the rest of the fields intuitively!)

On the down side, I was so wrapped up in the project that I totally forgot that I had a date with my wife for lunch--and I was away from my desk, so I didn't get her calls. ...So I'm in the doghouse, now. : | D'oh.

Weekend Update

Three things to report:

My son and I played three games this weekend. The first was one called Heroscape, and it was the most age-appropriate of the three (we played the "simple" version). I won, but just barely. The second was Dungeons and Dragons Miatures. ...The rules were a little too hard, but he did quite well. And I have to admit: he was faster than I was with the math! He kicked my butt. The third was Star Wars Miatures. He mauled me.

Anyway, this is newsworthy only because I've long felt guilt about not spending enough time "playing" with my son. This was a good start. I have to say I was impressed... he lost the first game very gracefully, he learned the rules fairly quickly, and he didn't do anything overtly "stupid", tactically speaking. He did get overly excited about the game, especially the last one, where he was really wiping the floor with me. ...but that's him: he can't sit still and can't stop talking. ; )

I had a medical "slip" this past week. Last week, the doctor upped my dosage of Levothyroxine from 100 micrograms to 125. ...I had been having these occasional episodes of lightheadedness and chest "weirdness", that I couldn't quite describe. After the dosage changed, I started having them all the time. (Well, okay, several times an hour.) I noticed that if I took my pulse while these were happening, my heart-rate was all screwy. So I looked up cardiac arhythmia on Wikipedia and, lo and behold, this is a symptom of hyperthyroidism.

But it didn't stop there. Since I work with a dozen pharmacists, I asked around about it. I also work with six people who either have a thyroid imbalance or their spouse does... and I learned that the way my doctor was giving me my medication was prone to heart palplitations: this is why people normally start at very low levels of thyroxine and titrate up to the levels they need.

Anyway, I called the doctor--we was out--but another doctor at the office told me to go back to 100 micrograms, and to make an appointment for when my regular doctor gets back. I did both. The heart palpitations are completely gone now (three days later). All is well again.

It looks like I really will be using ruby at work! On Thursday, I had a meeting with all the other programmers about a new web-project that the center will be doing, and I argued for heavy use of Ajax. I showed my own code as an example--they were all very impressed--and then I showed the programmer who will head the new project how I was accomplishing it with Cold Fusion. I went on to explain a lot of the leg-work I was doing in CF was built-in to Ruby on Rails, and he was totally sold. We will pilot Rails next week, doing some pair-programming.

Not only that, but the center's lead deveopler asked me to schedule some sessions with the other programmers to teach them Ruby (and Rails), so we can all add it to our arsenal.

(I don't know if I logged this or not, but we recently switched from Visual Basic to C#, and have started pair-programming in it: it's insanely fun. Ruby will only be an improvement.)

With RubyCLR coming to maturity, and Ruby in Steel another option, I can't think of any reason why we wouldn't use it. A lot.

The lead programmer and I have already been joking about using IronPython (their main site isn't up at the moment). I wouldn't mind that. ...Though I admit, I've got a bias for Ruby at the moment. ;)

Perl is beautiful, and I will always love it... but I have come to admore "real" OO programming, and while it's possible in Perl... it's hackish.

Perl 6, of course, will be a god-send. ...But I am not holding my breath.

Ruby is, IMO, currently the most ideal scripting language. Java's bulky, and C# is... well... Microsofty (but I do enjoy it). C++ still reigns supreme in terms of speed and capability... but I'm a high-level programmer. ...But, most importantly, Ruby is fun, and has an amazing community. And Rails is pretty slick. : )

A Possibility

A co-worker just asked me to look at his database ERD--a butchered version of Northwest Pubs (one of Microsoft's example DBs), to handle VA publication references.

I thought, "whoa, he could have this up in running in a few hours with Rails." I've been trying to sell rails to this co-worker for over a year now, so when I told him my thought this morning, he was prepared for it: ruby wasn't a foreign concept to him anymore. His response was "that's what I wanted to hear!".

We're scheduling some pair-programming to give it a whack in the near future.

Another co-worker overheard the conversation. ...He then asked me what I thought about another web-application...

[writhes hands insidiously]