Ruby

 Ruby

Ruby紹介のスライド

F588267d3e7e23475cad3933226c1e96?s=128

Shintaro Abe

March 11, 2014
Tweet

Transcript

  1. 3.

    RUBY VERSIONS ruby 1.6 ܥ ruby 1.7 ܥ ruby 1.8

    ܥ ruby 1.9 ܥ ruby 2.0 ܥ ruby 2.1 ܥ ruby 1.6 ܥ ruby 1.7 ܥ ruby 1.8 ܥ ruby 1.9 ܥ ruby 2.0 ܥ ruby 2.1 ܥ
  2. 4.

    RUBY 2.1.0 Ϋόʔδϣχϯάʹ͍ͭͯ MAJOR: MINOR ϦϦʔεͰରԠͰ͖ͳ͍ޓ׵ੑͷͳ͍มߋ͕͋Δ৔߹ ʹ૿Ճ͢ΔɻಛผͳΠϕϯτͷͨΊʹ༧໿͞ΕΔɻ MINOR: ΫϦεϚε͝ͱʹ૿Ճ͢ΔɻAPI ϨϕϧͰͷඇޓ׵͕͋Γ͑Δɻ

    TEENY: API ޓ׵ੑΛҡ࣋͢ΔηΩϡϦςΟϑΟοΫε΍όάϑΟοΫ εɻ2.1.11 ͷΑ͏ʹ10Ҏ্ͷ஋ʹͳΓ͑Δɻ2ɺ3ϲ݄͓͖ʹϦϦʔε͞Ε Δɻ PATCH: ௚લͷ MINOR ϦϦʔε͔Βͷίϛοτ਺ɻ (MINOR ͕ϦϦʔ ε͞ΕΔ͝ͱʹ 0 ʹ໭Δ) {MAJOR}.{MINOR}.{TEENY}p{PATCH} https://www.ruby-lang.org/ja/news/2013/12/21/semantic-versioning-after-2-1-0/
  3. 7.

    จࣈͷѻ͍ʹڧ͍ σϑΥϧτจࣈίʔυ͸ UTF-8 
 
 
 ! # coding: euc-jp

    # UTF-8 3bytes t1="͍͋͏͓͑" t1.bytesize #=> 15 t1.length #=> 5 ! # UTF-8 4bytes t2 = "㘏㚁䆰䆵䇁" t2.bytesize #=> 20 t2.length #=> 5
  4. 8.

    จࣈͷѻ͍ʹڧ͍ M17N:multilingualization ϚϧνϦϯΨϥΠθʔγϣϯ ! ! euc_str = "೔ຊޠ".encode("euc-jp") => “\x{C6FC}\x{CBDC}\x{B8EC}"

    ! sjis_str = "ʹͬΆΜ".encode("sjis") => "\x{82C9}\x{82D9}\x{82F1}\x{82B2}" ! euc_str.encode('utf-8') + sjis_str.encode('utf-8') => "೔ຊޠʹͬΆΜ"
  5. 9.

    จࣈͷѻ͍ʹڧ͍ ਖ਼نදݱ logs = ['192.168.0.1 - - [14/Feb…’, '192.168.0.2 -

    - [14/Feb…’, '192.168.0.3 - - [14/Feb…’ ] # ਖ਼نදݱ: regs = /(?<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ # ܁Γฦ͠ॲཧ:ɹdo ~ end ͸ϒϩοΫ {} ͱ΋ॻ͚Δ logs.each do |log| log.match regs do |m| p m[:ip] end end ! # =>”192.168.0.1"ɹ“192.168.0.2"ɹ"192.168.0.3"
  6. 12.

    STRING "Niigata.rb".include? "rb" #=> true "Niigata.rb".start_with? "Niigata" #=> true "Niigata"+".rb"

    #=> "Niigata.rb" "ruby" * 3 #=> "rubyrubyruby" "%02d݄%02d೔" % [8,31] #=> "08݄31೔" "Niigata.rb"[0,7] #=> "Niigata" "rubyrubyruby".scan(/ruby/) #=> ["ruby", "ruby", "ruby"] "ruby".split("") #=> ["r", "u", "b", "y"] euc_str = "೔ຊޠ".encode("euc-jp") #=> "\x{C6FC}\x{CBDC}\x{B8EC}"
  7. 13.

    ARRAY ["Niigata"] + ["rb"] #=> ["Niigata" ,"rb"] ["ruby"] * 3

    #=> ["ruby", "ruby", "ruby"] ["Niigata", "rb"].join(".") #=> "Niigata.rb" [1,2,3,4,5,6,7,8,9,10].inject(:+) #=> 55
  8. 14.

    HASH # ॳظԽ hash = {one: 1, two: 2} #=>

    {:one => 1, :two => 2} ! #ΞΫηε hash.each do |key,val| puts "#{key}: #{val}" end ! #ߋ৽ hash[:one] = 1.0 #=> {:one => 1.0, :two => 2} hash.merge!({one: '1', two: ‘2'}) #=> {:one => '1', :two => '2'}
  9. 16.

    ENUMERATOR::LAZY Enumerator ([1,2,3,4,5].each / (1..10).map) Enumerator::Lazy ͍͢͝େ͖͍഑ྻ࢖͏࣌ʹ࢖͑Δ big_data = (1..Float::INFINITY).lazy

    squares = big_data.map{|a| (a**2).to_s } p squares.select{|a| a.match /7/}.take(1).force #=> ["576"]
  10. 18.

    ͢΂͕ͯΦϒδΣΫτ ϝιουఆٛ p self # => main p self.class #

    => Object ! def self.my_method :my_method end ! p self.my_method #=> :my_method # 2.1.0 new feature m = def my_method :my_method end ! p send(m) #=> :my_method p self # => main p self.class # => Object ! def my_method :my_method end ! p my_method #=> :my_method
  11. 21.

    ͢΂͕ͯΦϒδΣΫτ Πϯελϯεϝιουఆٛ Foo = Class.new Foo.module_eval do def my_method :my_method

    end def my_metho2 :my_metho2 end end obj1 = Foo.new p obj1.my_method #=> :my_method ! obj2 = Foo.new p obj2.my_method #=> :my_method p obj2.my_metho2 #=> :my_metho2
  12. 22.
  13. 23.

    ͢΂͕ͯΦϒδΣΫτ Πϯελϯεϝιουఆٛ class Foo < Object def my_method :my_method end

    def my_metho2 :my_metho2 end def self.my_class_method :my_class_method end end p Foo.my_class_method #=> :my_class_method obj1 = MyClass.new p obj1.my_method #=> :my_method p obj1.my_metho2 #=> :my_metho2
  14. 24.

    ΦʔϓϯΫϥε class Foo def hello(name) puts "hello #{name}" end end

    class Foo def goodbye puts "goodbye" end end class Foo def hello(name) puts "hello #{name} san" end end Foo.new.hello 'rubico' => "hello rubico san" Foo.new.goodbye => 'goodbye'
  15. 26.

    MODULE.INCLUDE class Foo def hello(name) puts "hello #{name}" end end

    module Goodbye def goodbye puts "goodbye" end end class Foo include Goodbye end ! Foo.new.hello name:'rubico' => "hello rubico" Foo.new.goodbye => 'goodbye'
  16. 27.

    MODULE.PREPEND class Foo def hello(name) puts "hello #{name}" end end

    module SanTukero def hello(name) super(name + " san") end end class Foo prepend SanTukero end ! Foo.new.hello 'rubico' => "hello rubico san" ! !
  17. 28.

    MODULE. module Bar refine Numeric do def bar "bar" *

    self end end end ! using Bar puts 5.bar # ruby 2.1 module Huga using Bar puts 5.bar end => "foofoofoofoofoo" ! !
  18. 30.

    ϦϑϨΫγϣϯ TRACEPOINT trace = TracePoint.new(:call) do |tp| puts "#{tp.defined_class}##{tp.method_id}" puts

    '---------' end trace.enable ! def my_method :my_method end p my_method #=> Object#my_method --------- :my_method