Ik_guy

Geekskillz_logo1


"Everything that boots is beautiful."


Programming

Feed-icon Subscribe to Programming articles

Jan 16
2008

A Class Inspection Trick for Ruby

Tags: Programming   Ruby on Rails


I was struggling with SOAP for the first time today. No, today is not the first day I bathe! SOAP is some crazy XML communication protocol that seems like overkill to me.

Anyway, I was using Ruby to send and receive the request, and I was getting a baffling SOAP::Mapping::Object object, and I had no idea how to use it. It's a class with dynamically generated methods, and none of the ones I was expecting to see were there.

Blogosphere to the rescue! Marty Haught's Weblog gave a simple trick that saved the day and helped me narrow down my problem to let me see the solution within minutes.

To inspect the resulting object, I was looking at the output of this:
@results.public_methods.sort.join(' ')

Which gives a huge list of methods.

Thanks to Marty, I did this instead:
(@results.public_methods - Object.public_methods).sort.join(' ')

Subtracting Object.public_methods removes all the methods that all Ruby objects get, showing me only the methods that are different. I got this list:
[] []= __add_xmlele_value __xmlattr __xmlele out out=

Sure enough, the "out" method stood out, so I investigated it. A few minutes later, I had navigated through the class and found what I needed. Sweet!

I post this here so that others might discover this simple trick and solve whatever problem is keeping them up late at night.


Sep 15
2007

Windows Search For All Files

Tags: Programming   Rants


Here's a tip that programmers might appreciate. Coders dealing with huge projects rely on search quite a bit, especially when they need to make project-wide changes.

Say you use a function named do_something_that_crashes(). At some point in time you may need to find all instances of that function and remove it. It could be in dozens of files. Hundreds even!

On UNIX or using cygwin, you could find all files containing that function using the find command. On a Mac, it's even easier: you just use Spotlight (with plugins like this one). Why can't every operating system have Spotlight?? On Windows, you have the crappiest search capability of any operating system: that stupid orange dog that couldn't find a dead bird to save his life.

The problem is that Windows search ignores files with extensions that it doesn't understand. So, if you're a web programmer trying to search through all your .php files, Windows won't be able to do it. Ruby on Rails programmer? Windows will ignore all your .rb, .rhtml, and .rxml files. And even better, it will make it look like it searched them but didn't find anything! It will proudly say, "Nope, those files don't contain what you're looking for." You can easily test this. Open one of your files, choose a word that you find in the file, and then tell that inept Windows search dog to find the word in the file. It will happily tell you that the file doesn't contain that word. Your eyes deceive you! Trust Windows!

Click to continue reading...


Aug 22
2007

Sitemaps In Ruby on Rails

Tags: Programming   Ruby on Rails


If you’re using Ruby on Rails to build your website, then you’ve probably got a lot of dynamically generated pages. In this blog, I have one page for every article I write. I also have a page for each month, and a page for each year. I also have a page for each tag which lists all the articles with those tags. It isn’t too much, so Google's web crawler can keep up with it and get everything indexed.

But I have another site with hundreds of individual pages, and Google’s crawler stops indexing the site after only a few dozen pages, missing the important ones. If one of my pages isn't in Google's index, then it isn't working for me! It's just wasting hard drive space. It was obvious that I needed to tell Google what to crawl by giving it a sitemap. Read Google's docs about sitemaps, and be familiar with Google's Webmaster Tools in order to use submit sitemaps. Basically, a sitemap acts as a table of contents for Google, so it can understand which pages it should be looking at when it visits your site.

Because Rails is so awesome, there’s an easy way to generate sitemaps using Ruby. It should only take you a few minutes to write your sitemap code once you have a look at how it’s done. I’ll give a brief overview here.

Click to continue reading...


Aug 14
2007

Rails 1.2 vs OmniFind Yahoo! Edition

Tags: Programming   Ruby on Rails


I'm back from vacation, back in the city, back in the workforce, and back with my MacBook on my lap. Time to show this blog some loving again.

I'll warm up with a quick programming article. My regular readers will leave now and not notice how rusty my writing is. People stumbling in will have low expectations. Perfect!

For some reason, I decided to install the long-named IBM OmniFind Yahoo! Edition search engine on my home web server that my web sites run on. I have a web site that could really benefit from a dedicated web crawler, since Google has been ignoring it and only indexing a few pages.

So I point it to the new website, and get this error: "The URL does not have any pages available to crawl". The site has almost 300 pages to crawl, so I scratched my head in confusion and dug deeper to find the root cause of the problem.

I found out that the OmniFind web crawler was requesting pages in XML format, rather than HTML. Since I had not set up the site to respond properly to XML requests, it was giving empty pages to the crawler. So of course, the crawler thought it was looking at an empty web site. From my testing, this will only happen with resources in Ruby on Rails 1.2. Older Rails apps received requests for HTML as I had expected. So, Rails 1.2 is interpreting the OmniFind crawler requests as being for XML, although I could not find anything in the request that indicated this. Could be a bug in Rails 1.2, or ambiguity in the requests.

Click to continue reading...


Jul 20
2007

The Ruby Is The Message

Tags: Programming   Ruby on Rails


I found an interesting presentation about Ruby aimed at Java programmers: 10 Things Every Java Programmer Should Know About Ruby. It says it isn't trying to convince you that Java sucks and Ruby will make you a much happier programmer, but you might start to feel that way by the time you get to the last point. I don't think Ruby can replace Java, but that doesn't stop me from wishing. Ruby is an object-oriented language, but differs from Java's implementation in some important ways.

Number 8 was new information to me, and I had to think about it for a while. It says that almost everything in Ruby is a message. When you call a class method, that call is a message. The method and its arguments are the message, and the class (or instance of the class) is the destination of the message. The class and the message are not joined to each other in any special way. The message could have just as easily been directed at another class object. I now think of Ruby classes this way: Classes are written to receive messages.

Is this only a distinction without a difference? I don't think so. I'll try to illustrate with an example.

Click to continue reading...


Jul 05
2007

Using Indexes to Improve Rails Performance

Tags: Programming   Ruby on Rails


Here’s another article about performance in Ruby on Rails apps. This one has information that is brutally obvious to anyone with database experience, but if you’re more of a web developer than a database admin, then this article may be insightful for you. Check out my first article, Using :select in Rails for Better Performance, for other tips.

I have looked at a lot of other people’s Rails code as I was learning, including their database migrations, and have rarely seen indexes being created with the tables. For those who aren’t using database migrations, take the time to learn about them and you’ll never go back to writing CREATE TABLE statements yourself again! Seriously, migrations might be my favorite feature in Rails!

Anyway, a table without an index is not much of a table at all. Querying a table without an index is a performance problem waiting to happen.


What’s An Index?

In a database, you have a few different kinds of objects. You’ve got tables, which are just big containers in which you put your data. The data is not sorted, so if you want to find something in a table, you need to look through everything until you find what you want.

Click to continue reading...


Jun 26
2007

Using :select in Rails for Better Performance

Tags: Programming   Ruby on Rails


Here’s an article for the Ruby on Rails programmers out there. Geek Skillz is home-made using Rails, as an exercise for me to learn this popular framework. Rails makes it very easy to use databases with your web sites, which is a great thing, but with a little problem. As I search for Rails info on the web, I find that a lot of writers don’t seem to have the database background to understand performance issues. They let Rails deal with the database and think nothing more about it. I think it’s very important that if you use databases in your code, in whichever language and framework you choose, you should take time to learn about them in depth!

DISTINCT in Rails

I was looking into a way to list all the values that appear in one column of a table. I didn’t want all the rows, just the unique values that appear in a column. In SQL, this is done with the “DISTINCT” keyword. For example, “SELECT DISTINCT year FROM blog_posts” would list all the years that are in the year column of the blog_posts table.

Click to continue reading...




About Me

ThinkGeek

Feed-icon Technorati


Loot For Geeks:
4inkjets Great Prices and Best Quality!
Man's Wig! All size heads! Handsome! Sideburns! Modacrylic Fiber!
Protection
!
Make money online selling grit! Famous men sell grit!