Ammar's Techno Babble http://www.ammaryousuf.com All kinds of technology related things I learn and jot down. posterous.com Wed, 09 Mar 2011 10:35:13 -0800 Converting a String to a Valid Date Object http://www.ammaryousuf.com/converting-a-string-to-a-valid-date-object http://www.ammaryousuf.com/converting-a-string-to-a-valid-date-object Today at work I had to fix a form that allowed users to enter expiration dates in the past.  Well the form was passing the dates to the controller as a simple string, when I tried to do a comparison on the string it obviously didn't work.  So I had to convert the string into a valid format to compare with a Date object.

I first thought I could create my Date object like this, but I got an error.

>> Date.new("03/01/2011")
NoMethodError: undefined method `-' for "03/01/2011":String

After some Googling I found the strptime method, you can call it on a Date object, with the string version of the date as a parameter and it outputs a nice, clean Date object that you are able to use in your comparison.

I did the following:

date_entered = Date.strptime("{#{params[:subscription][:expires_date]}}", "{ %m/%d/%Y }")

Then used the date_entered variable in my comparison, like this:

if(date_entered < Date.today)
  # DO SOMETHING HERE
end

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/922020/Screen_shot_2010-12-22_at_10.26.34_PM.png http://posterous.com/users/KzIymVZm3n Ammar Yousuf Ammar Ammar Yousuf
Tue, 22 Feb 2011 09:01:02 -0800 Preserving Parameters in Failed Form Submission in Rails http://www.ammaryousuf.com/preserving-parameters-in-failed-form-submissi http://www.ammaryousuf.com/preserving-parameters-in-failed-form-submissi I recently ran into a bug at work, I was trying to submit a form that had some hidden variables and when the form would fail saving it would call render :action => "new", which sent the user back to the form on the "new" page.  This is fine but since the form had some hidden fields that were being set by parameters being passed on the url, the hidden fields would fail to get set, causing the object to get saved but without some very important attributes.

I did some searching and found out that if I wanted to preserve the parameters when a form failed, I would have to set them in the form_for method call, with the url key, like this:

form_for(@product, :url => { :action => :create, :type => params[:type], :name => params[:name] }) do |f|

After setting the type and name like this, the parameters were successfully retained.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/922020/Screen_shot_2010-12-22_at_10.26.34_PM.png http://posterous.com/users/KzIymVZm3n Ammar Yousuf Ammar Ammar Yousuf
Sun, 20 Feb 2011 18:49:36 -0800 Understanding Ruby Blocks, Procs and Lambdas http://www.ammaryousuf.com/understanding-ruby-blocks-procs-and-lambdas http://www.ammaryousuf.com/understanding-ruby-blocks-procs-and-lambdas One of the most misunderstood parts of Ruby are Blocks, Procs, and Lambdas.  I've used them in the past a few times, but have never had the time to dig deep and truly understand the differences.  Lately I've been working with Rack and it's forcing me to really understand Blocks, Procs, and Lambdas.  I found a great article by Robert Sosinski, http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas.

Robert really gives a great explanation of this useful feature.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/922020/Screen_shot_2010-12-22_at_10.26.34_PM.png http://posterous.com/users/KzIymVZm3n Ammar Yousuf Ammar Ammar Yousuf
Sat, 15 Jan 2011 23:12:24 -0800 Postgres pg Gem Install Failure http://www.ammaryousuf.com/postgres-pg-gem-install-failure http://www.ammaryousuf.com/postgres-pg-gem-install-failure I ran into a few issues while installing the "pg" gem that is required for communication with the Postgres database.  The error that I was seeing was:

Can‘t find the ‘libpq-fe.h header
*** extconf.rb failed ***

I didn't understanf because I had Postgres installed correctly in the /Library/PostgreSQL directory.  After doing some research I figured it out.  The build architectures between Ruby and Postgres were not matching.

I ran the following to figure out what the architectures were.

For Ruby:

file $(which ruby)

/Users/ammarmayk/.rvm/rubies/ruby-1.8.7-p330/bin/ruby: Mach-O 64-bit executable x86_64

For my Postgres 8.4:

file $(which psql)

./psql: Mach-O universal binary with 2 architectures
./psql (for architecture ppc):    Mach-O executable ppc
./psql (for architecture i386):    Mach-O executable i386

As you can see Postgres was not build for the x86_64 architecture and that is all I had for my Ruby.  So I decided to upgrade the Postgres to the newest version, 9.0.

I downloaded the DMG installer from the Postgres site and installed it, I made sure to replace Postgres 8.4 with the new 9.0 in my .profile file.

I added this to the path /Library/PostgreSQL/9.0/bin.

Then after this I ran the file command again, this time Postgres showed that it had been built for the x86_64 architecture.

ammar-yousufs-macbook-pro:~ ammarmayk$ file $(which psql)
/Library/PostgreSQL/9.0/bin/psql: Mach-O universal binary with 3 architectures
/Library/PostgreSQL/9.0/bin/psql (for architecture ppc7400):    Mach-O executable ppc
/Library/PostgreSQL/9.0/bin/psql (for architecture i386):    Mach-O executable i386
/Library/PostgreSQL/9.0/bin/psql (for architecture x86_64):    Mach-O 64-bit executable x86_64

Now when I did a "gem install pg" it all went through without any problems.

Hope that helps anyone having Postgres woes.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/922020/Screen_shot_2010-12-22_at_10.26.34_PM.png http://posterous.com/users/KzIymVZm3n Ammar Yousuf Ammar Ammar Yousuf
Sat, 15 Jan 2011 08:40:00 -0800 RVM and Redline Errors on Snow Leopard http://www.ammaryousuf.com/rvm-and-redline-errors-on-snow-leopard http://www.ammaryousuf.com/rvm-and-redline-errors-on-snow-leopard

Today I was installing ruby-1.8.7-p330 on my MacBook Pro for my RVM instance.  I kept getting these weird errors about readline.c, I had no idea what the readline.c was or what it was used for.

readline.c: In function ‘username_completion_proc_call’:
readline.c:730: error: ‘username_completion_function’ undeclared (first use in this function)
readline.c:730: error: (Each undeclared identifier is reported only once
readline.c:730: error: for each function it appears in.)
make[1]: *** [readline.o] Error 1
make: *** [all] Error 1

After some Googl'in I learned that Readline is a C library that is used for reading in a line of text from a provided file handle and returns a character pointer.  When Ruby was trying to build it was having a conflict with the version of Readlines that were installed on the system, there were  two, one that came with OS X and one that was installed my MacPort.

OS X has it installed in /usr/lib and MacPorts has it installed in /usr/local/lib.

We have to use the version that was installed by MacPorts.

Specify the version of Readline to use when installing ruby-1.8.7-p33 like this:

rvm install ruby-1.8.7-p330 --with-readline-dir=/usr/local
Once I specified the location of readline the installed went through successfully.

 

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/922020/Screen_shot_2010-12-22_at_10.26.34_PM.png http://posterous.com/users/KzIymVZm3n Ammar Yousuf Ammar Ammar Yousuf
Thu, 06 Jan 2011 10:47:00 -0800 Ruby's Dir Class is Awesome http://www.ammaryousuf.com/rubys-dir-class-is-awesome http://www.ammaryousuf.com/rubys-dir-class-is-awesome
I'm working on a new project at work and I needed to require all the model classes in our project for a script, so I did it individually one by one, then my friend Tony showed me a nicer way to include all the model files in one line.  Thanks Tony!

He used Ruby's Dir class, which allows you to interact with the file system's directories. 

Dir.glob("#{LIBPATH}/../app/models/*.rb").each { |file| require file }

So now with this one liner I have successfully required all the models in my project.

To find more about the Dir class you can check out the Ruby doc.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/922020/Screen_shot_2010-12-22_at_10.26.34_PM.png http://posterous.com/users/KzIymVZm3n Ammar Yousuf Ammar Ammar Yousuf
Wed, 05 Jan 2011 15:00:00 -0800 Speeding up Project Search in Textmate http://www.ammaryousuf.com/speeding-up-project-search-in-textmate http://www.ammaryousuf.com/speeding-up-project-search-in-textmate
So I've been using Textmate for about three years now, overall I've been pretty happy with it, except for the fact that you can't do split screen and the searching is very very slow.  Well for my second problem I've found a solution, a friend of mine pointed me to the "Ack in Project" bundle that uses ack to speed up the searching in your project.  Here is a version of "Ack in Project", that allows you to search in particular filetypes, https://github.com/kemayo/ack-tmbundle

To install it:
cd ~/Library/Application\ Support/TextMate/Bundles
git clone git://github.com/kemayo/ack-tmbundle.git Ack.tmbundle
Then to find something in your project just enter (Command+Shift+a)

Screen_shot_2011-01-05_at_6

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/922020/Screen_shot_2010-12-22_at_10.26.34_PM.png http://posterous.com/users/KzIymVZm3n Ammar Yousuf Ammar Ammar Yousuf
Sat, 25 Dec 2010 23:15:00 -0800 iSight Camera Problems with Flash 10.6.5 http://www.ammaryousuf.com/isight-camera-problems-with-flash-1065 http://www.ammaryousuf.com/isight-camera-problems-with-flash-1065

So today I spent about 4 hours trying to get my MacBook Pro's iSight camera to work with sites that use webcams via Flash, sites like yFrog.com and Justin.tv.  After updating Flash to the latest version 10.6.5, everything iSight related stopped responding, no matter what browser I tried, Firefox, Safari or Chrome, all had the same problem.  I tried uninstalling and installing other versions of Flash without luck.

So then I came across this article in MacWorld, http://www.macworld.com/article/156357/2010/12/isight_flash.html, it seems that this is a pretty big problem that has been affecting many Mac users!  The first thing the article said was to run GoogleVoiceAndVideoUninstaller.app in /Library/Application Support/Google, the second I did this it fixed my problem!  So there seems to be a conflict between Google Video and Flash, something that screws up the iSight driver.  Hope this article solves your issue if you are having trouble.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/922020/Screen_shot_2010-12-22_at_10.26.34_PM.png http://posterous.com/users/KzIymVZm3n Ammar Yousuf Ammar Ammar Yousuf
Wed, 22 Dec 2010 21:17:00 -0800 Getting Google Top Stories with Nokogiri http://www.ammaryousuf.com/getting-google-top-stories-with-nokogiri http://www.ammaryousuf.com/getting-google-top-stories-with-nokogiri

So I wanted to get the Top Stories from Google News, to my surprise I found out that there was no API available for Google News, I had to resort to a brute force method and scrape.  Here is the scraper the I wrote in about 10 minutes.

require 'rubygems'
require 'nokogiri'
require 'open-uri'

url = "http://news.google.com/"
doc = Nokogiri::HTML(open(url))

doc.css(".topic").each do |topstory|
  item = topstory.at_css("a").text
  puts item
end

This produces a nice list for me:

Spider-Man
Dow Jones Industrial Average
Network neutrality
North Korea
Missile defense
Human rights
Los Angeles
Philadelphia 76ers
Green Bay Packers
Lindsay Lohan

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/922020/Screen_shot_2010-12-22_at_10.26.34_PM.png http://posterous.com/users/KzIymVZm3n Ammar Yousuf Ammar Ammar Yousuf
Wed, 22 Dec 2010 19:22:00 -0800 Ruby Inspect Method Behavior http://www.ammaryousuf.com/inspect-method-behavior http://www.ammaryousuf.com/inspect-method-behavior

So today while at work I was trying to debug a bit of code and see the contents of an Ruby object, which is a simple enough task.  I used the 'inspect' method to print a string representation of the object, this worked perfectly and I was able to see all the attributes of the object in the console.  Then later tonight I was working on Shoutreel and used 'inspect' again, but this time in the View, something that I had never really tried before, and to my surprise it failed to print a string representation of the object.  A hash sign was printed out.  Why is this?

So is this a bug in Rails, does the 'inspect' method in the view not work for model objects?  My guess is that the inspect method in ActiveRecord, which is what the model object inherits from, has a bug.

I'll investigate this and write back soon.

This is the code I had in the view:

<%= reel.inspect %>
<%= [ 1, 2, 3..4, 'five' ].inspect %>

The top line printed the hash, while the bottom line printed out the array as a string.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/922020/Screen_shot_2010-12-22_at_10.26.34_PM.png http://posterous.com/users/KzIymVZm3n Ammar Yousuf Ammar Ammar Yousuf