active_support

Rails�돪�³�돢���¡������­������blank?\���\½\���\���������»��������Ï��������­�����������돪�

���³�����я�������������������¡�blank? \���\½\���\��������ҏ��³�������������������돢Ï��¹���������µ���µ�����ď��������������¹��¡�

���µ��������¡񏪢��������䏢�������������������������������������Ï����� blank? �����ҏ����̏����Џ��·�����������Ï��������·�����ď������������¹���̏�¡��������¼���«����blank? �����������я�����\���\½\���\������µ��������������������¡ߏ����������䏢�������������������·���������������������·�����ď�¡����������Ï��·���������Ï��Ï���

require 'rubygems'
require 'active_support'

p !'hoge'.blank? #=> undefined method `blank?' for "hoge":String (NoMethodError)

���������������������������������Ï����� require 'active_support' ���·�������������������������������¼���폢�폢����»�ߏ����������ď����я��������¹���̏�¡񏢱���돩̏����� active_support ���� 3.0 �����������������������������������ď��³���������������·�����ď�¡�active_support 2.x ·��������� active_support3.0 ������ require ������\���\���\���\���\�����¼\¹���̡돫������Ï����я��������¹��¡��������������«���������¹�����������¡�¡�

active_support 2.x ·��������������������Ð�����������叢����������·�����������¡�Rails �돪�³�돢���¡������­������������ blank? \���\½\���\��������ҏ����̏����Џ��������­���������¹���̏�¡�active_support 3.0 ������������½Ð���������������»�����я��쏢��

����������㏢���������Ώ�������������¡�active_support 3.0 ������������ require 'active_support/core_ext' ���·�����������²����������̏���ߏ��̏����������������·���������������¹��¡��������������³�����������������������µ����������������¡��·���«������ i18n ���̏���̏���ߏ��������«�������������������������������������½������������ gem ������\���\���\¹\������¼\������·�����ď����²���������³�����Ï��������������������¹����

require 'rubygems'
require 'active_support/core_ext'

p !'hoge'.blank? #=> true

�����������Ï����� active_support ����3��������������돪㏪���²½���µ������������������������������������������¹��Џ����������������������Ï����������������������ď����Ώ����ď��������������¹��������¡�

# activesupport-3.0.0/lib/active_support/all.rb
require 'active_support'
require 'active_support/time'
require 'active_support/core_ext'

�����������������Ώ�������¡�active_support/core_ext.rb ������������ď�����������³�����я�����������������¡�core_ext �돪�²¼�����ҏ���������� require ���·���������������������������¹��¡�

# activesupport-3.0.0/lib/active_support/core_ext.rb
Dir["#{File.dirname(__FILE__)}/core_ext/*.rb"].sort.each do |path|
  require "active_support/core_ext/#{File.basename(path, '.rb')}"
end

¼�����돪����� Rails \��\��\������������������³�����Ï����������������� require ���·�������������������«���������¼������������»�ߏ��������������«�����������Ώ����ď�������¡�railties ������������������ Rails ������\³\��\���\���\���\���\������������� (railties-3.0.0/lib/rails/console/app.rb) ������ require 'active_support/all' ���·���������������·�����ď�����¡�

��������¡����������Ï��������ޏ����� Rails �돪�³�돢���¡������­������ blank? \���\½\���\���������»��������Ï����ď�����������������������������¡�

active_support 2.x ·���
require 'rubygems'
require 'active_support'

active_support 3.0
# sudo gem install i18n ���¹����������̏���ߏ�������
require 'rubygems'
require 'active_support/core_ext'

Rails������index_by������»���������������\���\���\·\���·���¼�돢����\������¼\��ď�����¼��؏���¼���������

rails ������»���������������������������\������¼\��ď�����¼������������·�����ď��������­��¡񏪮��只�я�����·���������\������¼\��ď��̏����ӏ��������������­������»������������������������������������������������������������Ï��������­���̏����������������¹��¡򏩡�������������������������»�������������������\������¼\��ď�����\��\���\»\¹���·�����ď��������������­���������«��¡�

# find(:all) ���¹���������������³�����я�����·���¼�돢���������ӏ�����������������������
[ 
  #<data1>, 
  #<data2>, 
  #<data3>, ... 
]

# ���³������·���¼�돢����¼��؏���¼��������������ď�����
{ 
  :key1 => #<data1>, 
  :key2 => #<data2>, 
  :key3 => #<data3>, ... 
}

���³���������������Ï�����\���\���\·\���������·���������¼��؏���¼��������������ď����������я��������¹��¡��³�����������������Ï����������������ď������������������������«����������������»�ߏ���������������������㏢���������Ώ����������� index_by ������������������\���\½\���\���������»��������Ï��³���������������������ޏ�����¼���������½Ð�����������������Ώ����ď��������������¹����������active_support ������������������������ Enumerable ������������²������µ������������\���\½\���\������������¹������¡�

���������������������ޏ�������¡��³���������������Ï�����\������¼\���\���������»���������������¼�����돪�����»����·�����������Ώ��������·�����ď�¡������á�����Ï���

# sample \������¼\���\���
+----+---------+---------+---------------------+
| id | value_1 | value_2 | created_at          |
+----+---------+---------+---------------------+
|  1 | hoge    | fuga    | 2009-09-15 23:40:35 | 
|  2 | foo     | bar     | 2009-09-15 23:40:35 | 
|  3 | hoge    | HOGE    | 2009-09-15 23:40:35 | 
|  4 | hoge    | fuga    | 2009-09-15 23:40:35 | 
|  5 | FOO     | BAR     | 2009-09-15 23:40:35 | 
+----+---------+---------+---------------------+

symbol ������»������������·�����ď��������� key ���������������������������¹��¡�symbol ��������돢���� & �����ҏ��������ޏ��������������ҏ����돢���������������������������Ï��������·���������������������µ��������¡�

Sample.find(:all).index_by(&:value_1)
#{
#  "hoge"=> #<Sample id: 4 ...>, # ���������«�����������������ď�����������¾���½��Џ��­���µ������������
#  "FOO" => #<Sample id: 5 ...>,
#  "foo" => #<Sample id: 2 ...>
#}

index_by �����������������������ҡ돫����������·���«������»����·�����������������ď�������¡񏩡�ޏ����� key ������»�������\������¼\��ď��̏����ď�ԏ�������¾���¹������������������������«������������������\������¼\��ď���������������¾���½��Џ��­���µ���������������¹��¡�¾�������������������������¡�hoge ������������������ key ���������Ð���·������ id:1 ������ id:3 ������\������¼\��ď�����¾���½��Џ��­���µ������������¾�����𡯏��������·���������������������¹�����������������돪؏��·���������������������µ��������¡�

�����������������Ώ����� group_by ������»��������Ï�������¡񏩡�ޏ����� key ���������Ð���·�����������ď�ԏ������������̏�������¾���¹�����¡񏪮��只�я����������ď�ԏ�����»����·���������������������������¹��¡����ď��������·��¡�\������¼\���¼«��������\���\���\·\�����������������������������¡񏪮��只�я����������ӏ�������������������������������������²��я����Ώ����ď����������������Џ����я����������������Џ��«��������������¡�¡�

Sample.find(:all).group_by(&:value_1)
#[
#  [ "hoge", [#<Sample id: 1 ...>, #<Sample id: 3 ...>, #<Sample id: 4 ...>] ],
#  [ "foo",  [#<Sample id: 2 ...>] ],
#  [ "FOO",  [#<Sample id: 5 ...>] ]
#]

���µ��������������¡�\������¼\���\������������µ������\«\���\��������� key ���������¹���������³������������²������½���������¹��¡�\���\���\���\���·���¼�돢���� key ������»������������¹���������³���������������������������������­�����������Ώ����ď��������������¹��¡��³�����������������������̏��������¹������������¡�

Sample.find(
  :all, 
  :select => %q/CONCAT(value_1, value_2) AS hoge/
).index_by(&:hoge))
#{
#  "foobar"   => #<Sample id: 2 ...>,
#  "hogefuga" => #<Sample id: 4 ...>,
#  "FOOBAR"   => #<Sample id: 5 ...>,
#  "hogeHOGE" => #<Sample id: 3 ...>
#}

Sample.find(:all).index_by { |sam| "#{sam.value_1}#{sam.value_2}" }
#{
#  "foobar"   => #<Sample id: 2 ...>,
#  "hogefuga" => #<Sample id: 4 ...>,
#  "FOOBAR"   => #<Sample id: 5 ...>,
#  "hogeHOGE" => #<Sample id: 3 ...>
#}

Ruby�����������䏪���؏�����·��»»���¹������������������active_support»�������������������

Ruby �����������䏪���؏����ҡ�·�����Ï��������­��������¡�active_support ����rails ������\���\���\¹\������¼\������¹�������������돫�½��������������Џ�����������������������\���\���\���\���\������� ���̏���������������������̏����������я��������¹���쏢�쏢�돢�������돢�������������������¹������������������������\³���¼\��������ҏ��«�����ď����叢���Ï��̏�����������������»�ߏ����Ï��������������������Ï��¾��¡�

#!/usr/bin/ruby
 
require 'rubygems'
require 'active_support'
 
t = Time.now # Mon Sep 07 00:24:07 +0900 2009
 
t - 5.days  # 5�����䏪���   (Wed Sep 02 00:24:07 +0900 2009)
t - 1.week  # 1½µ����ӏ���� (Mon Aug 31 00:24:07 +0900 2009)
t - 2.years # 2�����������   (Fri Sep 07 00:24:07 +0900 2007)
 
# xxx����돢���������� ago ������»���������
1.hour.ago # 1�����䏪��� (Sun Sep 06 23:24:07 +0900 2009)
 
# xxx������������������ since ������»���������
2.days.since # 2�����䏢���� (Wed Sep 09 00:26:07 +0900 2009)
 
t.yesterday # Mon Sep 06 00:24:07 +0900 2009
t.tomorrow  # Mon Sep 08 00:24:07 +0900 2009

t.beginning_of_month # ·���½���������  (Tue Sep 01 00:00:00 0900 2009)
t.last_month         # 1\���·������ (Fri Aug 07 00:24:07 0900 2009)

2.minutes.ago.class # Time \���\���\¹���̏����ӏ�����

���������²���������·���������¾���������쏢�������¹���쏢�½���������¾�����������ď��·�������������������ޏ�ď��·������̏����С돪؏��µ�����������������������������ď�������¡�1.days ���������«½��Џ��«�����돢���� 1.day ������½��Џ��ޏ����������ď��������������������������������«���������������¹���������������¹��������������¡�ago ������ since ���������������½���������¾������ until ������ from_now ������������������ alias ���̏����С돪؏��µ�����������������������������������Ï��������¹��¡������������������«�����ď��ޏ��������³�����я�����������������¡�

# ...lib/active_support/core_ext/numeric/time.rb
alias :second   :seconds
alias :minute   :minutes
alias :hour     :hours
alias :day      :days
alias :week     :weeks
alias :until    :ago
alias :from_now :since

�����������ď�¡�Time \���\���\¹���������ޏ�������������������������¡�Date \���\���\¹����������������ޏ����������������Ï�������������돫��¹���������³���������̏��������­���������¹��¡�

#!/usr/bin/ruby
 
require 'rubygems'
require 'active_support'
 
d = Date.today # Mon, 07 Sep 2009

d - 2.days # 2�����䏪��� (Sat, 05 Sep 2009)
 
(d - 2.days).class # Date \���\���\¹���̏����ӏ�����
(d - 1.hour).class # ¼«����돪���쏢����\­\���\¹\������µ������������ Time \���\���\¹���̏����ӏ�����

���³���³���������������¾���������쏢������������돫��������­��������������¡񏩡��䏪���؏����ҏ�ġ���������������³��⏢�·���������������������������­���������¹��������¡����������я�����������»������������������������«���������������¹��������¡��«���������³������������\³���¼\���������½��Џ��������������������­���������·������������¡�
karaage299 at gmail.com
��돪������������µ­»���
\���\��\���\���\���\���