Tag Archives: Ruby

We’re gearing up for the upcoming, very first Tulsa Ruby Workshop.

picture-1.png

Of course, all you need to know is on the website, but in short, it’s a beginner’s conference all about Ruby and Rails. If you’re new to Ruby and/or Rails, this is for you (if you’re in or near enough to Tulsa, Oklahoma that is). Tell everyone you know!

Just to save you a half-hour of your life, I thought I’d share that you should never create a named route called “directory” — it will break Rails migrations and other rake tasks in mysterious and hard-to-track-down ways.

I’ve been reading the book Advanced Rails by Brad Ediger. I first met Brad way back in 2006 when we were first trying to get our local Ruby user group going. He is a very smart fellow, and his newest book is definitely evidence of that. There’s a lot in there that’s over my head, but that’s nothing a few rereads can’t solve. :-)

The book has everything from optimization to deployment, from security to metaprogramming (my brain is still spinning from that one). Brad does an awesome job of including plenty of code and real-world examples.

If you use Ruby on Rails for fun or profit, this book is definitely a must-buy.

And, believe it or not, this something-other-than-perl-and-php book can be found in local bookstores (Brad V. found this one at the Tulsa 71st B&N):

2.jpg

From my post on Snippets:

First, create a plain Word document with text to replace like this:

Here is some text of my document. This will be replaced with [name]. Today’s date is: [date].

Then, run the following code. The text in square brackets will be replaced with the values in the code, and the file will be saved as a new name.


require 'win32ole'

word = WIN32OLE.new('Word.Application')
#word.Visible = true # uncomment if you want to see it happen
doc = word.Documents.Open('c:\file_to_open.doc')
{
  'name' => 'Tim Morgan',
  'date' => Date.today.strftime('%B %d, %Y')
}.each do |key, value|
  word.Selection.HomeKey(unit=6) # start at beginning
  find = word.Selection.Find
  find.Text = "[#{key}]” # text must be in square brackets
  while word.Selection.Find.Execute
    word.Selection.TypeText(text=value)
  end
end

doc.SaveAs(’c:\output_file.doc’)
doc.Close

Update: I’ve been using this for awhile now, and I still have to manually check my watchlist every day because my reader only gets updates every few days. I’m not sure, but it seems to be a caching issue on the WP API and not so much the script’s fault.

Update 2: The load on my server was too much, so I removed the hosted script from my server. You can still download the source and host it yourself.

Update 3: Turns out it’s better to just subscribe to each page’s feed rather than to use a script like this.

Back before Wikipedia had an API, I created a script that scraped the HTML and generated a plain RSS feed of my watchlist. Now, it’s even easier as the new API does most of the work for you, provided you correctly authenticate with it. So, version 2 of the script is much simpler, and simply acts to authenticate a user and grab the feed as-is and hand it to the browser/aggregator/etc.

The script source is here.

Basically, the URL would look like this:

http://71m.org/watchlist.cgi?username=USERNAME&password=PASSWORD

Before you go trying it on my server, be aware that: (I have removed the script from my server. Sorry.)

  1. Your password and username are in the URL in plain-text (and thus in my server logs if you use this script directly from my server). This might scare you. I’m not an evil person, but then again, I wouldn’t believe someone else telling me that for a second. If you don’t trust me, then grab the script and stick it on your own server.
  2. I will leave my script running for you to use directly, but if the load becomes too much on my lowly server, I will disable it.

So, there’s two reasons to grab the script and run it on your own server.

Thanks to Jyotirmoy Bhattacharya for his Python script that does basically the same thing.

It occured to me that my gems of wisdom on my Snippets page likely go unnoticed by you faithful blog readers, so you might want to subscribe to my snippets feed as well so you don’t miss beauties like this (yes, I know this will seem lame to some, but I think it’s clever):

class Object
 def in?(object)
   object.include? self
 end
end

>> 1.in? 2..3
=> false
>> 1.in? 1..3
=> true

My favorite programming language had a bit of a facelift. Now the homepage is as beautiful and elegant as the language itself.

Update: Version 2 is available here.

Today, I finally got fed up with Wikipedia’s lack of watchlist feeds, so I wrote a script that generates them.

A few of the photos I had uploaded to the Wikimedia Commons have been marked for deletion for some time now due to inadequate license information (my fault entirely), but I didn’t know it because I didn’t think about the Commons having its own separate watchlist. Oops. That got me thinking… these watchlists should have feeds so I don’t have to keep pulling up my bookmarks every day.

Now, until they actually add feeds to those pages, I have my own solution in a few lines of Ruby code: WatchlistRSS.zip This code can be run as a CGI on a web server and generates feeds like this.

So, I’m sharing this because the other two or three scripts out there written to do this suck IMO. (Or at least I couldn’t get them to work — which in that case means I suck :-)

I don't know how many people are switching from Python to Ruby these days, but I've learned a bit and thought it would be helpful to a few people if I cross-referenced my Python knowledge to my (growing) Ruby knowledge. I got this idea from James on the OK.rb mailing list.

Part One will focus on where to find what in the Ruby community. With any programming language or framework, you have a set of resources you go to often to find what you're looking for. I had a list of Python-related bookmarks I frequented; now I have a list of bookmarks for Ruby stuff.

One disclaimer: this is not a comprehensive list or comparison. It is just a representation of my Python world cross-referenced with my Ruby world.

Language Reference

Standard Library: Python - Ruby
Built-in Classes: Python - Ruby

Interacting

Shell: python - irb

File Extensions

Standard: .py - .rb
Console-less: .pyw - .rbw
Compiled/Bytecode: .pyc, .pyo - none

Web Frameworks

Beastly: Zope - none
MVC: Django, TurboGears - Ruby on Rails
Lite: Quixote, CherryPy - Camping

Content Management Systems

Plone - Radiant

Object-Relational Mapping

SQLObject - ActiveRecord

In the spirit of Release Early - Release Often, I'm posting this in the hopes I will think of stuff to add to it later.

Here's my Ten Rails Tips, expanded a bit to try to explain my thinking…

  1. Scaffolding won't write your app for you, otherwise DHH would have called it "foundationing".
    Too often I see people addicted to the Rails scaffolding feature. I've found that it's often a crutch for actually thinking about the problem at hand. Scaffolding is great for a one-table model, but breaks down quickly when you have has_manys and other such relationships.I almost wish the Rails screencasts didn't include the demonstration of this feature, because it makes people think that's how you really start a Rails app. In reality, I rarely use scaffolding.
  2. Find inefficiencies using the development log.
    There's good stuff in there! Much of the time, looking through the log is unnecessary simply because Rails gives you such descriptive and pretty error messages inside the browser. But seeing the SQL being executed is invaluable for finding where your app needs to improve on the ActiveRecord side of things.
  3. Use :include where possible to minimize the number of db queries.
    This is where the dev log comes in handy; it will point out places in your code where you probably should have used Eager Loading (:include => :whatever). Seeing the (mostly) same SQL query repeated over and over again is good motivation for using this handy feature. When used correctly, multiple queries are combined into one, thus reducing the overhead of communicating with the database.
  4. Learn SQL, including joins; it will help you to make your queries more efficient.
    You can probably go your whole life without ever learning a drop of SQL (thanks to ActiveRecord), but I would recommend getting a decent grasp of it, because the fact is, it's still handy to know what the heck is going on. I've found that ActiveRecord isn't the silver bullet for ridding the world of SQL forver. Instead, it simply makes the mundane stuff a lot funner.
  5. Learn to "feel" the presence of helpers and shortcuts — chances are, if you think there should be one, there probably is one.
    Few things are more irritating than writing a bunch of code and then finding a one-liner in the Rails API docs that does the same thing, especially when it does it better! Turns out, you can start to get pretty good at "feeling" the existence of such things. When writing stuff that seems redundant, think to yourself, is this a task specific to my application, or is everyone dealing with this? If the latter, then there's probably a helper/shortcut/whatever to do it for you. Don't waste lines of code on stupid stuff.
  6. Create models to handle your data. Models aren't just database tables.
    Rails doesn't care if your models are ActiveRecord or not. Create models to do your heavy lifting. If you find yourself writing more than a few lines in a controller method, ask yourself, is this something the model could be doing itself? If yes, move it to the model. It's easier to work with models on the console than controllers and views.
  7. Write a plugin if you think you'll ever use the code in another app or across different controllers and models.
    Don't repeat yourself. If you need the same bit of code again, put it in a plugin so it's available all over your app. Then, it's also easy to reuse that code in your next app.Creating a plugin is as easy as "script/generate plugin my_plugin".
  8. Learn to use the console. Try out complex code there before you put it in your app.
    "script/console" is a command line for your Rails app. Use it. Learn to live in it.
  9. Instead of just "testing your app," write "tests for your app."
    Grep (or "tail -f" rather) your brain for the word "test". When you think I should test that. Don't! Write a test to do it instead… then run the test. It only takes a tiny bit more time, then you get the added bonus of being able to run all your tests again later.
  10. Keep http://api.rubyonrails.com in a Firefox tab and turn on "begin finding when you begin typing."
    I live in the API docs. Firefox has a nifty feature that makes finding stuff in the docs much easier… just start typing! Need to know what arguments the render method takes? Type render and hit enter!