Ruby on rails certifications
December 6, 2007
There are none so far.
Neither for only the Ruby language.
Official certifications, I mean. The Java unofficial certification community JavaBlackBelt has a beta exam for Ruby certification. Yes, it’s still a beta because the number of questions on their database so far is not enough to release the exam. So if you are a Ruby programmer and you are willing to collaborate, you can start taking a look at the exam objectives and writing some questions for them.
Understanding Ruby symbols
December 4, 2007
Symbols are one of the most misunderstood Ruby features. Let’s try to explain it as lay as possible.
The way I finally catched it is thinking about symbols as constants, whose value is the same as their name. The same as the integer 5 has the name 5 and the value 5, the symbol :thing has the name thing and the value “thing”. That’s why we can open an irb and do this:
irb(main):008:0> puts :myname myname => nil irb(main):009:0> :myname => :myname
Remember, symbols are not strings, therefore this is not possible:
irb(main):010:0> puts :myname + :othername
NoMethodError: undefined method `+' for :myname:Symbol
from (irb):10
Instead we can get the value of the symbol, and convert it to a String to get this:
irb(main):011:0> puts :myname.to_s + :othername.to_s mynameothername => nil
Very cute. So what’s the point?Symbols are mainly used to save memory. They have only one occurrence within a Ruby program, while strings with the same name are stored in different positions of memory every time. Check this:
irb(main):012:0> "string".object_id => 24125620 irb(main):013:0> "string".object_id => 24122170 irb(main):014:0> :string.object_id => 68258 irb(main):015:0* :string.object_id => 68258
And don’t forget that symbols are objects, just like everything in Ruby. They have a name and a string value, as we said, but an integer value to be recognized by the program as well, a value that is always constant. That brings many performance benefits (while comparing symbols, for example). They are like strings with a value that must not be changed.
Ruby on Rails screencasts
December 3, 2007
A fast post just to recommend a couple of links for those looking for Rails tutorials in video (screencasts).
For people yet not involved with Rails, the official Rails webpage gives somes good video examples on what is Rails about.
For people currently working with Rails we have the podcast Railscasts, which is constantly actualized and brings a new screencast every 10 days aprox.
And for people interested not only in the Rails framework, BestTechVideos provides technical videos about Ruby on Rails and other languages and frameworks.
Nice helper for testing model validation
December 3, 2007
I found this this morning in Mr. Matt’s blog. Is a fantastic helper method to save us work testing our ActiveRecord models validation, available under the Creative Commons Attribution 2.0 UK: England & Wales License.
# In test_helper.rb
# Give this function an array of invalid values, an array of valid values, the object and
# attribute name to test, and it will ensure that only valid values are allowed
def validation_tester ( valid_values, invalid_values, attribute_name, test_object )
valid_values.each do |val|
test_object.[]=(attribute_name, val)
assert test_object.save, "#{attribute_name} should be valid: #{test_object.[](attribute_name)}"
assert test_object.valid?
assert !test_object.errors.invalid?(attribute_name)
end
invalid_values.each do |val|
test_object.[]=(attribute_name, val)
assert !test_object.save, "#{attribute_name} should NOT be valid: #{val}"
assert !test_object.valid?
assert test_object.errors.invalid?(attribute_name)
end
end
And is used as easy as this:
def test_email_validation
valid_emails = ['matt@test.com', 'matt.hall@test.com', 'matt@test.ing.com', 'matt.hall@test.ing.com']
invalid_emails = ['@test.com', 'test.com', 'matt@', 'matt.hall@com', '.matt@test.ing.co.uk', 'matt@@test.com', '', '.@.']
u = create_user
validation_tester( valid_emails, invalid_emails, 'email', u)
end