tips learn from confident ruby


The book confident ruby

After reading the book confident ruby, I watch the screen cast with that book and learned following tips

use eachwithobject instead of initialize an empty object before the each


topics_list = []
another_list.each do |i|
  topics_list << i
end

should turn into


result = another_list.each_with_object([]) do |i, topics_list|
  topics_list << i
end

don’t use collection.present? to check if collection exist and nonempty ,use Array()

skip the iteration at the top of the block


next unless topics[id]

accept user or user_id with confident


user_id = user
user_id = user.id unless user.class == Fixnum

To reduce the duplication, refactor to: “`ruby

private def useridfor(userorid) case userorid when Integer then userorid else userorid.id end end ”`

if define the private method, then use:


def self.user_id_for
...
end
private_class_method :user_id_for

return early(using guard) instead of using if at the top of the method

rescue at the top level of the method, to avoid the indent and begin block

comments powered by Disqus