Wednesday, January 16, 2008

What was Sun missing? DB => MySQL

http://blogs.mysql.com/kaj/sun-acquires-mysql.html/

Add methods to an instance

list = ["first item", "second item", "third item"]

# Add some useful methods to list

class << list
def total
self.size
end
def second
self[1]
end
def third
self[2]
end
end

>> list.second
# "second item"
>> list.total
# 3
>> list.third
# "third item"
>> list.first
# "first item" (first is a pre-defined Array method)

Select a random record from a DB table

In Rails:
Post.find(:first, :order => "rand()")

In SQL:
SELECT * FROM posts ORDER BY RAND() LIMIT 1;

Monday, January 14, 2008

Removing instance variables

@new_user = User.find(:first)
.
.
@old_user = remove_instance_variable(:@new_user)
# @new_user is nil now
------------------
More:
- http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/20911
- Page 555 of pickaxe book under Object (or search for string remove_instance_variable)

Thursday, January 10, 2008

Bug Severity and Priority definitions

Severity - an objective, absolute, and technical assessment of the bug:
  1. Bug causes system crash and/or data loss.
  2. Bug causes major functionality or other severe problems; or product crashes in obscure cases.
  3. Bug causes minor functionality problems; or bug relates to wording issues in high visibility areas - impact is on "fit anf finish".
  4. Bug contains typos, unclear wording or error messages in low visibility areas.
Priority - a subjective, relative, and "business or consumer" assessment of the bug:
  1. Critical: Must fix NOW - bug is blocking further progress in testing.
  2. High: Must fix as soon as possible - prior to next build.
  3. Medium: Should fix soon, before product release.
  4. Low: fix if time; somewhat trivial. May be postponed.

----
Johanna Rothman provides another simplified way:
Instead of priority and severity, I use risk as a way to deal with problem reports, and how to know how to fix them. Here are the levels I choose:
  • Critical: We have to fix this before we release. We will lose substantial customers or money if we don’t.

  • Important: We’d like to fix this before we release. It might perturb some customers, but we don’t think they’ll throw out the product or move to our competitors. If we don’t fix it before we release, we either have to do something to that module or fix it in the next release.

  • Minor: We’d like to fix this before we throw out this product.

Here is the full article:
http://www.stickyminds.com/sitewide.asp?Function=edetail&ObjectType=COL&ObjectId=6288

Tuesday, January 8, 2008

Struct

Question=Struct.new(:question, :answer)
=> Question
q1=Question.new("what's your first name?","subbu")
=> #
q1.question
(behaves like an object)
=> "what's your first name?"
q1[:question]
(behaves like a symbol)
=> "what's your first name?"
q1['question']
(behaves like a hash)
=> "what's your first name?"
q1[0]
(behaves like an array)
=> "what's your first name?"
---------------------------
This means, I can treat my Struct based on what the situation demands. Sometimes I can treat it like an array or a hash or an object or a symbol.

Create a project using earlier versions of Rails

rails _1.2.2_ myapplication

Just make sure 1.2.2(or whichever) version of rails gems including its dependencies are installed.

Wednesday, January 2, 2008

Undocumented Rails features - render collection with index

When I want to display the index of a collection in the following statement:
<%= render :partial => "subscription", :collection => @subscriptions %>

I can say:
<%= subscription_counter + 1 %>

The pattern is "partial_name_counter"

More here:
http://www.pgrs.net/2007/7/20/render-partial-with-collection-has-hidden-counter