Setting Core Objects’ Methods in RoR

Aug 09
2008

mkdir -p RAILS_ROOT/vendor/plugins/core_methods

mkdir -p RAILS_ROOT/vendor/plugins/core_methods/init.rb

The content of init.rb:

module Rails
 
  empty_and_nil = %q{def empty_and_nil; self.empty?? nil : self end}
 
  Array.send :class_eval, empty_and_nil
  Hash.send  :class_eval, empty_and_nil
 
end

The namespace Rails isn’t necessary, it’s a cosmetic feature.

to truncate or not?

Jul 10
2008
# RoR source code:
# File vendor/rails/actionpack/lib/action_view/helpers/text_helper.rb, line 34
def truncate(text, length = 30, truncate_string = "...")
  if text.nil? then return end
  l = length - truncate_string.chars.length
  text.chars.length > length ? text.chars[0...l] + truncate_string : text
end
 
# My point of view:
def truncate(text, _length = 30, truncate_string = "...")
  text[0..(_length-1)] + truncate_string
end

auto_link and highlight

Nov 01
2007
auto_link(text){|link| hightlight(link, phrase)}

Calendar

July 2010
M T W T F S S
« Apr    
 1234
567891011
12131415161718
19202122232425
262728293031  

Tags