ActiveSupport事始め2012/8/30 yoyogi.rb
自己紹介curl http://cui-about.me/nysalor
今回どうしよう
今回どうしよう
ActiveSupportについて
ActiveSupportについてパクリ意外と知らないメソッドが多い復習の意味で
ActiveSupport?https://github.com/rails/rails/tree/master/activesupportRailsに入っている便利ライブラリRailsをインストールすると勝手に入るRails抜きでも使える
試してみよう
試してみよう% gem install activesupportFetching: activesupport-3.2.8.gem (100%)Successfully installed activesupport-3.2.81 gem i...
ついでにpryも
ついでにpryも% gem install pry% gem install pry-coolline
pry?http://pryrepl.org/irbの強化版
インストール
インストール% gem install pry% gem install pry-coolline
起動
起動% pry1.9.3 (main):0 >
ActiveSupportを読み込む
ActiveSupportを読み込む1.9.3 (main):0 > require active_support/all=> true1.9.3 (main):0 >
String系
pluralize
pluralize1.9.3 (main):0 > magazine.pluralize=> “magazines”1.9.3 (main):0 > fantasy.pluralize=> “fantasies”
singularize
singularize1.9.3 (main):0 > magazines.singularize=> “magazine”1.9.3 (main):0 > fantasies. singularize=> “fantasy”
camelize,under_score
camelize,under_score1.9.3 (main):0 > active_support.camelize=> “ActiveSupport”1.9.3 (main):0 > ActiveSupport.underscore=> ...
constantize
constantize1.9.3 (main):0 > CurrentUser.constantize=> CurrentUser
from
from1.9.3 (main):0 > ‘active_support’.from(3)=> “ive_support”1.9.3 (main):0 > ‘active_support’[3..-1]=> “ive_support” # 同じ
blank?
blank?1.9.3 (main):0   > nil.blank?=> true1.9.3 (main):0   > .blank?=> true1.9.3 (main):0   > ‘ ‘.blank?=> true1.9.3 (main...
present?
present?1.9.3 (main):0   > true.present?=> true1.9.3 (main):0   > blank.present?=> true1.9.3 (main):0   > ‘‘.present?=> fa...
presence
presence1.9.3 (main):0   > true.presence=> true1.9.3 (main):0   > blank.presence=> blank1.9.3 (main):0   > ‘‘.presence=> n...
presence
presence1.9.3 (main):0 > ‘cool’.presence || ‘oops!’=> “cool”1.9.3 (main):0 > ["aaa", "bbb", "ccc"].map{|x|1.9.3 (main):0 *...
inquiry
inquiry1.9.3 (main):0 > ‘tiger’.inquiry=> “tiger”1.9.3 (main):0 > tiger.inquiry.tiger?=> true1.9.3 (main):0 > tiger.inquir...
どうやっているのか?http://goo.gl/qt8Sz
DateTime系
Time.zone
Time.zone1.9.3 (main):0   >   Time.zone=> nil1.9.3 (main):0   >   Time.zone = ‘Tokyo’=> “Tokyo”1.9.3 (main):0   > Time.zon...
yesterday,tommorow
yesterday,tommorow1.9.3 (main):0   > Time.zone.now=> Thu, 30 Aug   2012 17:41:49 JST +09:001.9.3 (main):0   > Time.zone.no...
ago
ago1.9.3 (main):0   > Time.zone.now=> Thu, 30 Aug   2012 17:41:49 JST +09:001.9.3 (main):0   > 3.days.ago=> Mon, 27 Aug   ...
since
since1.9.3 (main):0   > Time.zone.now=> Thu, 30 Aug   2012 17:41:49 JST +09:001.9.3 (main):0   > 3.days.since=> Sun, 02 Se...
引数
引数1.9.3 (main):0   > Time.zone.now=> Thu, 30 Aug   2012 17:41:49 JST +09:001.9.3 (main):0   > Time.zone.now.ago(3.days)=> ...
始まりと終わり
始まりと終わり1.9.3 (main):0   > Time.zone.now=> Thu, 30 Aug   2012 17:41:49 JST +09:001.9.3 (main):0   > Time.zone.now.beginning...
任意の時刻
任意の時刻1.9.3 (main):0   > Time.zone.now=> Thu, 30 Aug   2012 17:41:49 JST +09:001.9.3 (main):0   > Time.zone.now.change(:hou...
範囲
範囲1.9.3 (main):0 > Time.zone.now=> Thu, 30 Aug 2012 17:41:49 JST +09:001.9.3 (main):0 > Time.zone.now.all_day=> 2012-08-30...
Array/Hash系
Array#from
Array#from1.9.3 (main):0 > ‘cool’.presence || ‘oops!’=> “cool”1.9.3 (main):0 > ["aaa", "bbb", "ccc"].map{|x|1.9.3 (main):0...
Hash#assert_valid_keys
Hash#assert_valid_keys1.9.3 (main):0 > hash = {1.9.3 (main):0 *   :tiger => 10.4,1.9.3 (main):0 *   :lion => 10.71.9.3 (ma...
Array#forty_two
Array#forty_two1.9.3 (main):0 > [1,2,3,4].forty_two=> nil1.9.3 (main):0 > (1..100).to_a.forty_two=> 42
なにこれ?
なにこれ?
質疑応答
質疑応答if available?  Question.all.map{|x|    x.try(:answer).presence ||    x.to_a.forty_two}
Upcoming SlideShare
Loading in …5
×

Active support事始め

669 views
543 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
669
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Active support事始め

    1. 1. ActiveSupport事始め2012/8/30 yoyogi.rb
    2. 2. 自己紹介curl http://cui-about.me/nysalor
    3. 3. 今回どうしよう
    4. 4. 今回どうしよう
    5. 5. ActiveSupportについて
    6. 6. ActiveSupportについてパクリ意外と知らないメソッドが多い復習の意味で
    7. 7. ActiveSupport?https://github.com/rails/rails/tree/master/activesupportRailsに入っている便利ライブラリRailsをインストールすると勝手に入るRails抜きでも使える
    8. 8. 試してみよう
    9. 9. 試してみよう% gem install activesupportFetching: activesupport-3.2.8.gem (100%)Successfully installed activesupport-3.2.81 gem installed
    10. 10. ついでにpryも
    11. 11. ついでにpryも% gem install pry% gem install pry-coolline
    12. 12. pry?http://pryrepl.org/irbの強化版
    13. 13. インストール
    14. 14. インストール% gem install pry% gem install pry-coolline
    15. 15. 起動
    16. 16. 起動% pry1.9.3 (main):0 >
    17. 17. ActiveSupportを読み込む
    18. 18. ActiveSupportを読み込む1.9.3 (main):0 > require active_support/all=> true1.9.3 (main):0 >
    19. 19. String系
    20. 20. pluralize
    21. 21. pluralize1.9.3 (main):0 > magazine.pluralize=> “magazines”1.9.3 (main):0 > fantasy.pluralize=> “fantasies”
    22. 22. singularize
    23. 23. singularize1.9.3 (main):0 > magazines.singularize=> “magazine”1.9.3 (main):0 > fantasies. singularize=> “fantasy”
    24. 24. camelize,under_score
    25. 25. camelize,under_score1.9.3 (main):0 > active_support.camelize=> “ActiveSupport”1.9.3 (main):0 > ActiveSupport.underscore=> “active_support”
    26. 26. constantize
    27. 27. constantize1.9.3 (main):0 > CurrentUser.constantize=> CurrentUser
    28. 28. from
    29. 29. from1.9.3 (main):0 > ‘active_support’.from(3)=> “ive_support”1.9.3 (main):0 > ‘active_support’[3..-1]=> “ive_support” # 同じ
    30. 30. blank?
    31. 31. blank?1.9.3 (main):0 > nil.blank?=> true1.9.3 (main):0 > .blank?=> true1.9.3 (main):0 > ‘ ‘.blank?=> true1.9.3 (main):0 > ‘ ‘.blank?=> true1.9.3 (main):0 > ‘blank‘.blank?=> false
    32. 32. present?
    33. 33. present?1.9.3 (main):0 > true.present?=> true1.9.3 (main):0 > blank.present?=> true1.9.3 (main):0 > ‘‘.present?=> false1.9.3 (main):0 > ‘ ‘.present?=> false1.9.3 (main):0 > nil.present?=> false
    34. 34. presence
    35. 35. presence1.9.3 (main):0 > true.presence=> true1.9.3 (main):0 > blank.presence=> blank1.9.3 (main):0 > ‘‘.presence=> nil1.9.3 (main):0 > ‘ ‘.presence=> nil1.9.3 (main):0 > nil.presence=> nil
    36. 36. presence
    37. 37. presence1.9.3 (main):0 > ‘cool’.presence || ‘oops!’=> “cool”1.9.3 (main):0 > ["aaa", "bbb", "ccc"].map{|x|1.9.3 (main):0 * x.gsub("a","").presence &&1.9.3 (main):0 * x.upcase}=> [nil, "BBB", "CCC"]
    38. 38. inquiry
    39. 39. inquiry1.9.3 (main):0 > ‘tiger’.inquiry=> “tiger”1.9.3 (main):0 > tiger.inquiry.tiger?=> true1.9.3 (main):0 > tiger.inquiry.bunny?=> false1.9.3 (main):0 > tiger.inquiry.class=> ActiveSupport::StringInquirer
    40. 40. どうやっているのか?http://goo.gl/qt8Sz
    41. 41. DateTime系
    42. 42. Time.zone
    43. 43. Time.zone1.9.3 (main):0 > Time.zone=> nil1.9.3 (main):0 > Time.zone = ‘Tokyo’=> “Tokyo”1.9.3 (main):0 > Time.zone=> (GMT+09:00) Tokyo1.9.3 (main):0 > Time.zone.now=> Thu, 30 Aug 2012 17:41:49 JST +09:00
    44. 44. yesterday,tommorow
    45. 45. yesterday,tommorow1.9.3 (main):0 > Time.zone.now=> Thu, 30 Aug 2012 17:41:49 JST +09:001.9.3 (main):0 > Time.zone.now.tomorrow=> Fri, 31 Aug 2012 17:41:49 JST +09:001.9.3 (main):0 > Time.zone.now.yesterday=> Wed, 29 Aug 2012 17:41:49 JST +09:00
    46. 46. ago
    47. 47. ago1.9.3 (main):0 > Time.zone.now=> Thu, 30 Aug 2012 17:41:49 JST +09:001.9.3 (main):0 > 3.days.ago=> Mon, 27 Aug 2012 17:41:49 JST +09:001.9.3 (main):0 > 10.month.ago=> Sun, 30 Oct 2011 17:41:49 JST +09:00
    48. 48. since
    49. 49. since1.9.3 (main):0 > Time.zone.now=> Thu, 30 Aug 2012 17:41:49 JST +09:001.9.3 (main):0 > 3.days.since=> Sun, 02 Sep 2012 17:41:49 JST +09:001.9.3 (main):0 > 10.month.since=> Sun, 30 Jun 2013 17:41:49 JST +09:00
    50. 50. 引数
    51. 51. 引数1.9.3 (main):0 > Time.zone.now=> Thu, 30 Aug 2012 17:41:49 JST +09:001.9.3 (main):0 > Time.zone.now.ago(3.days)=> Mon, 27 Aug 2012 17:41:49 JST +09:001.9.3 (main):0 > Time.zone.now.since(3.years)=> Sun, 30 Aug 2015 17:41:49 JST +09:00
    52. 52. 始まりと終わり
    53. 53. 始まりと終わり1.9.3 (main):0 > Time.zone.now=> Thu, 30 Aug 2012 17:41:49 JST +09:001.9.3 (main):0 > Time.zone.now.beginning_of_day=> Thu, 30 Aug 2012 00:00:00 JST +09:001.9.3 (main):0 > Time.zone.now.end_of_year=> Mon, 31 Dec 2012 23:59:59 JST +09:00
    54. 54. 任意の時刻
    55. 55. 任意の時刻1.9.3 (main):0 > Time.zone.now=> Thu, 30 Aug 2012 17:41:49 JST +09:001.9.3 (main):0 > Time.zone.now.change(:hour =>19)=> Thu, 30 Aug 2012 19:00:00 JST +09:00
    56. 56. 範囲
    57. 57. 範囲1.9.3 (main):0 > Time.zone.now=> Thu, 30 Aug 2012 17:41:49 JST +09:001.9.3 (main):0 > Time.zone.now.all_day=> 2012-08-30 00:00:00 UTC..2012-08-30 23:59:59UTC1.9.3 (main):0 > Time.now.all_day=> 2012-08-30 00:00:00 +0900..2012-08-30 23:59:59+0900
    58. 58. Array/Hash系
    59. 59. Array#from
    60. 60. Array#from1.9.3 (main):0 > ‘cool’.presence || ‘oops!’=> “cool”1.9.3 (main):0 > ["aaa", "bbb", "ccc"].map{|x|1.9.3 (main):0 * x.gsub("a","").presence &&1.9.3 (main):0 * x.upcase}=> [nil, "BBB", "CCC"]
    61. 61. Hash#assert_valid_keys
    62. 62. Hash#assert_valid_keys1.9.3 (main):0 > hash = {1.9.3 (main):0 * :tiger => 10.4,1.9.3 (main):0 * :lion => 10.71.9.3 (main):0 * }1.9.3 (main):0 > hash.assert_valid_keys(1.9.3 (main):0 * :tiger, :leopard, :lion)=> {:tiger=>10.5, :leopard=>10.6, :lion=>10.8}1.9.3 (main):0 > hash.assert_valid_keys(1.9.3 (main):0 * :tiger, :bunny)=> ArgumentError: Unknown key: lion
    63. 63. Array#forty_two
    64. 64. Array#forty_two1.9.3 (main):0 > [1,2,3,4].forty_two=> nil1.9.3 (main):0 > (1..100).to_a.forty_two=> 42
    65. 65. なにこれ?
    66. 66. なにこれ?
    67. 67. 質疑応答
    68. 68. 質疑応答if available?  Question.all.map{|x| x.try(:answer).presence || x.to_a.forty_two}

    ×