Ruby2.0 Getting Started

  • View
    2.837

  • Download
    2

Embed Size (px)

DESCRIPTION

Ruby2.0の新機能やパフォーマンスなどの変更点をまとめました。

Text of Ruby2.0 Getting Started

  • 1. Ruby2.0 Getting Started Ruby2.0

2. Ruby2.0 Agenda 3. About Ruby2.0Ruby2.0About Ruby2.0 4. 2013224 Ruby20 Ruby1.9 2.0.0 Ruby2.0 matzAbout Ruby2.0 5. Future Future 6. Enumerable#lazy Module#prepend Refinments Future 7. Keyword Arguments Keyword Arguments Future 8. 1.9def hello(options = {})# options = {name: "guest"}.merge(options)puts "Hello #{options[:name]}!!"endhello #=> Hello guest!!hello(name: "taro") #=> Hello taro!!Keyword Arguments Future 9. 2.0def hello(name: "guest")puts "Hello #{name}!!"endhello #=> Hello guest!!hello(name: "taro") #=> Hello taro!!Keyword Arguments Future 10. # Ruby1.9def hello(options = {})options = {name: "guest"}.merge(options)puts "Hello #{options[:name]}!!"end# Ruby2.0def hello(name: "guest")puts "Hello #{name}!!"endKeyword Arguments Future 11. def hello(name: "guest")puts "Hello #{name}!!"endhello(nama: "jiro") #=> ArgumentError: unknown keyword: namaKeyword Arguments Future 12. **Hashdef hello(name: "guest", **options)msg = "Hello #{name}!!"msg.upcase! if options[:upcase]puts msgendhello(name: "hanako") #=> Hello hanako!!hello(name: "hanako", upcase: true) #=> HELLO HANAKO!!Keyword Arguments Future 13. Hashdef foo(somehash, option: 1)# do something...end# Hashhash_arg = {name: "guest"}foo(hash_arg) #=> ArgumentError: unknown keyword: name# Hashfoo(hash_arg, {})Keyword Arguments Future 14. HashKeyword Arguments Future 15. Enumerable#lazy Enumerable#lazy Future 16. Enumerable#lazy Enumerator =() selectmap Enumerable#lazy Future 17. 115# lazy(1..100000000).select{|num| num.odd?}.take(5)#=> [1, 3, 5, 7, 9]# lazy(1..100000000).lazy.select{|num| num.odd?}.take(5).force#=> [1, 3, 5, 7, 9]Enumerable#lazy Future 18. Enumerable#lazy IORange selectmap Rails WEBhttp://www.techscore.com/tech/Ruby/ruby-2.0/lazy/Enumerable#lazy Future 19. Module#prependModule#prepend Future 20. Module#prepend Module#include include prepend Module#prepend Future 21. prependinclude - 1module IMdef hoge; puts "IM#hoge"; endendmodule PMdef hoge; puts "PM#hoge"; super; endendclass Cinclude IMprepend PMdef hoge; puts " C#hoge"; super; endendModule#prepend Future 22. prependinclude - 2# ancestorsC.ancestors #=> [PM, C, IM, Object, Kernel, BasicObject]obj = C.newobj.hoge# PM#hoge# C#hoge# IM#hogeModule#prepend Future 23. Module#prepend alias_method_chain(ActiveSupport) RailsActiveRecordModule#prepend Future 24. Symbol list literalSymbol list literal Future 25. %i %I # [:hoge, :fuga, :piyo] #=>[:hoge, :fuga, :piyo]# %wmap%w(hoge fuga piyo).map(&:intern) #=>[:hoge, :fuga, :piyo]# - %i ()%i(hoge fuga piyo) #=>[:hoge, :fuga, :piyo]# - %I ()%I(a#{1+1} a#{1+2})#=>[:a2, :a3]Symbol list literal Future 26. RefinementsRefinements Future 27. Refinements Ruby2.0 ActiveRecord+Refinments# gem activerecord-refinements# User.where(name: "matz")User.where{:name == "matz"}Refinements Future 28. experimental DSL 2013Ruby2.1Refinements Future 29. OthersOthers Future 30. UTF-8 __dir__ to_h Hash Enumerable#size ( ) TracePointDtraceOthers Future 31. Incompatibility Incompatibility 32. 1.9 2.0 iconvRuby1.9 IO#lines, #chars, #codepoints, #bytesEnumerator Object#inspect #to_s UTF-8 ABI1.9.so.bundleIncompatibility 33. PerformancePerformance 34. bitmap marking GC VM Kernel#require Performance 35. 1 - require benchmarkputs RUBY_VERSIONputs Benchmark::CAPTIONputs Benchmark.measure {100000000.times{|i| 1.0 * i}}user systemtotalreal1.8.7 | 2.040000 0.000000 2.040000 ( 2.086681)1.9.3 | 1.120000 0.000000 1.120000 ( 1.161166)2.0.0 | 0.720000 0.000000 0.720000 ( 0.724945)Performance 36. Railstime RAILS_ENV=production rails runner ""user systemreal1.9.3 | 0m4.104s 0m0.356s 0m4.620s2.0.0 | 0m2.154s 0m0.365s 0m2.594sPerformance 37. Conclusion Conclusion 38. Ruby2.0 lazyprepend 1.9 RailsConclusion