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

西日暮里.rb 1周年記念回LT Crystal

西日暮里.rb 1周年記念回LT Crystal

Takuya Arita

June 29, 2015
Tweet

More Decks by Takuya Arita

Other Decks in Programming

Transcript

  1. ͔֬ʹRubyͬΆ͍ # http://crystal-lang.org/ ʹࡌͬͯΔαϯϓϧίʔυ # A very basic HTTP server

    require "http/server" server = HTTP::Server.new(8080) do |request| HTTP::Response.ok "text/plain", "Hello world!" end puts "Listening on http://0.0.0.0:8080" server.listen
  2. ʂ

  3. ίʔυྫ(hello.cr) message = "hello #{ARGV[0]}" puts message puts 1 +

    10 p [1, 2, 3] p ({a: 1, b: 2}) puts "hoge %s, %d, %.2f" % ["hogehoge", 1 , 1.12] [1, 2, 3, 4].each do |i| puts i end ([1,2,3] of Int32).each do |i| puts i end (1..10).each do |i| puts i end # ҎԼུ
  4. DockerͰϏϧυ࣮ͯ͠ߦͯ͠ΈΔ # Macͷboot2docker؀ڥ͕લఏͰ͢ $ # Crystal͕ೖ͍ͬͯΔΠϝʔδͰ.crΛίϯύΠϧͯ͠/mntʹॻ͖ग़͢ $ docker run -it

    -v `pwd`:/mnt ariarijp/crystal crystal build -o /mnt/hello / mnt/hello.cr $ # Ϗϧυͨ͠όΠφϦʔΛCrystal͕ೖ͍ͬͯͳ͍Πϝʔδ্Ͱಈ͔͢ $ docker run -it -v `pwd`:/mnt ubuntu:14.04.2 /mnt/hello hoge hello hogehoge 11 [1, 2, 3] {:a => 1, :b => 2} hoge hogehoge, 1, 1.12 1 2 3 (ҎԼུ)
  5. ίʔυྫ(json_example.rb) require 'net/http' require 'json' HOST = 'api.openweathermap.org' PATH =

    '/data/2.5/weather?q=Shibuya' resp = Net::HTTP.get(HOST, PATH) weather = JSON.parse(resp) p weather['weather'][0]['main']
  6. ίʔυྫ(json_example.cr) require "http/client" require "json" URL = "http://api.openweathermap.org/data/2.5/weather?q=Shibuya" resp =

    HTTP::Client.get(URL) weather = JSON.parse(resp.body) puts (((weather as Hash)["weather"] as Array)[0] as Hash)["main"] จࣈྻ͸μϒϧΫΥʔτ Cast! Cast! Cast! ஫ɿΩϟετͷͱ͜Ζ͸CrystalΛਖ਼͘͠ཧղ͍ͯ͠Ε͹͜Μͳ͜ͱͳ͍ͷ͔΋͠Ε·ͤΜ ඍົʹRubyͱҧ͏ඪ४ϥΠϒϥϦʔ