Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby (MRI) の好きなところ 30

Ruby (MRI) の好きなところ 30

プログラミング言語Ruby30周年記念イベント LT
#ruby30th

More Decks by Misaki Shioi(塩井美咲/しおい)

Other Decks in Programming

Transcript

  1. 3VCZ .3* ͷ޷͖ͳͱ͜Ζ ಉ͜͡ͱΛ͢ΔͨΊʹ ͍ΖΜͳํ๏͕༻ҙ͞Ε͍ͯͯ ଟ༷ੑ͕େࣄʹ͞Ε͍ͯΔͱ͜Ζ class Foo def foo

    puts "#ruby30th" end end Foo.new.foo # => #ruby30th Bar = Class.new do de fi ne_method(:bar) do stdout = IO.new(2) stdout.send(:write, “#ruby30th\n”) end end Bar.new.bar # => #ruby30th
  2. 3VCZ .3* ͷ޷͖ͳͱ͜Ζ ͳΜͰ΋ΦϒδΣΫτ ɹɹɹԿʹͰ΋ϝοηʔδΛૹΔ͜ͱ͕Ͱ͖Δͱ͜Ζ Class.new Module.new Exception.new Errno::ESHUTDOWN.new self.binding

    self.method(:__method__) self.method(:__method__).unbind TracePoint.new(:call) { |tp| tp.event } Ϋϥε΋ Ϟδϡʔϧ΋ ྫ֎΋ γεςϜΤϥʔ΋ ؀ڥ৘ใ΋ ϝιου΋ Ϩγʔόͷͳ͍ϝιου΋ τϨʔεϙΠϯτ΋FUDʜ
  3. 3VCZ .3* ͷ޷͖ͳͱ͜Ζ େମࣜ ɹɹɹͳͷͰ͜Μͳ͜ͱ͕Ͱ͖Δͱ͜Ζ ɹɹɹ͜Εͬͯ͘͢͝ͳ͍Ͱ͔͢  message = if

    ruby.birthday? “#ruby30th” else “Anyway I love Ruby everyday” end puts message # => #ruby30th
  4. 3VCZ .3* ͷ޷͖ͳͱ͜Ζ Ҿ਺͕͘͢͝ॊೈ ɹɹɹͳͷͰ͜Μͳ͜ͱ͕Ͱ͖Δͱ͜Ζ ɹɹɹ def foo(arg = true

    ? “#ruby30th” : “Anyway I love Ruby everyday”) puts arg # => #ruby30th end def bar(arg: def m; “#ruby30th”; end) puts arg # => :m puts self.method(arg).call # => #ruby30th end
  5. 3VCZ .3* ͷ޷͖ͳͱ͜Ζ ϒϩοΫͷଘࡏ͸͔ܽͤͳ͍ ɹɹɹϒϩοΫ͕ࣗ෼ͷߟ͑ํΛॊೈʹͯ͘͠ΕΔͱ͜Ζ i, chars = 0, %(#

    r u b y 3 0 t h) def foo(chars, &block) yield chars foo(&block) end foo(chars) { |chars| print chars[i]; i += 1; break if i >= chars.length } puts “\n” # => #ruby30th
  6. 3VCZ .3* ͷ޷͖ͳͱ͜Ζ ྫ֎ॲཧ ɹɹɹػೳ΋ἧ͍ͬͯΔ͠௚ײతͰ໎ࢠʹͳΒͳ͍ͱ͜Ζ def foo raise LightningTalkTimeout if

    Time.now > lightning_talk_started_at + 300 rescue puts “OMG” else puts “Yaaaaay” ensure puts “Anyway, #ruby30th” end
  7. 3VCZ .3* ͷ޷͖ͳͱ͜Ζ ਺஋ʹ۠੾Γจࣈ͕ೖΕΒΕΔ ɹɹɹɹ ɹɹɹͷͰ໨ʹ༏͍͠ “LT͸ը໘੾Γସ͑ͷ࣌ؒࠐΈͰ #{5} ෼Ͱ͢ʂ” “LT͸ը໘੾Γସ͑ͷ࣌ؒࠐΈͰ

    #{300} ඵͰ͢ʂ” “LT͸ը໘੾Γସ͑ͷ࣌ؒࠐΈͰ #{300_000} ϛϦඵͰ͢ʂ” “LT͸ը໘੾Γସ͑ͷ࣌ؒࠐΈͰ #{300_000_000} ϚΠΫϩඵͰ͢ʂ” “LT͸ը໘੾Γସ͑ͷ࣌ؒࠐΈͰ #{300_000_000_000} φϊඵͰ͢ʂ” IUUQTUXJUUFSDPNSVCZUITUBUVT T
  8. 3VCZ .3* ͷ޷͖ͳͱ͜Ζ 3VCZࣗ਎΋ զʑϓϩάϥϚʹରͯ͠Φʔϓϯ ɹ ಺෦৘ใ΁ͷ"1*͕ఏڙ͞Ε͍ͯΔ  ɹɹɹ $

    ruby -e "pp RubyVM::AbstractSyntaxTree.parse('puts 1 + 2')" (SCOPE@1:0-1:10 tbl: [] args: nil body: (FCALL@1:0-1:10 :puts (LIST@1:5-1:10 (OPCALL@1:5-1:10 (LIT@1:5-1:6 1) :+ (LIST@1:9-1:10 (LIT@1:9-1:10 2) nil)) nil))) $ ruby -e "puts RubyVM::InstructionSequence.compile('puts 1 + 2').disasm" == disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(1,10)> (catch: false) 0000 putself ( 1)[Li] 0001 putobject_INT2FIX_1_ 0002 putobject 2 0004 opt_plus <calldata!mid:+, argc:1, ARGS_SIMPLE>[CcCr] 0006 opt_send_without_block <calldata!mid:puts, argc:1, FCALL|ARGS_SIMPLE> 0008 leave