Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

router-simple.cr

1,317 views

Published on

東京 Crystal 勉強会 #1 in 五反田

Published in: Technology
  • Be the first to comment

  • Be the first to like this

router-simple.cr

  1. 1. router-simple.cr id:karupanerura
  2. 2. about me • @karupanerura (Twitter/Github/etc..) • Perl/Swift/C/Java/etc.. • Crystal/Ruby beginner • Lead Engineer of Mobile Factory, Inc. • Talk on YAPC::Asia/YAPC::EU 2015
  3. 3. router-simple?
  4. 4. URI(Path) Router
  5. 5. Example: require "router-simple" dispatcher = Router::Simple::Dispatcher(Hash(Symbol, String)).new() dispatcher.add("/", { :controller => "Root", :action => "index", }) dispatcher.add("/users/:user_id", { :controller => "Users", :action => "fetch_by_id", }) dispatcher.match("/") {|result| p result.route.meta[:controller] # => "Root" } is_match = dispatcher.match("/users/1") {|result| p result.route.meta[:controller] # => "Users" p result.match["user_id"] # => "1" } p is_match # => true (it means that is matches the route!)
  6. 6. Repository: https://github.com/karupanerura/router-simple.cr
  7. 7. My favourite points
  8. 8. My favorite points • Ruby like syntax • Type safety + Optional type restrictions • Nil safety (#.not_nil!) • Generics • Using PCRE
  9. 9. Ruby like syntax • module/class/require • Dependency on Projectfile • &block + yield • *args • etc..
  10. 10. Optional type restrictions def foo() : String # = String return "Foo" end # or def foo() # = String return "Foo" end
  11. 11. Nil safety def foo() : String? # = String|Nil return "Foo" # or nil end foo = foo() if foo != nil then foo.not_nil! # = String end # foo.length => compile error!!
  12. 12. Nil safety • implementation is very simple! • https://github.com/manastech/crystal/ blob/master/src/object.cr#L149 • https://github.com/manastech/crystal/ blob/master/src/nil.cr#L117
  13. 13. Generics • Optional typing + generics = great! • MyClass(T) and use it. • POINT: Type specialization is supported • POINT: Type restriction is optional.
  14. 14. Using PCRE • Perl5 hacker loves PCRE :) • Perl extended regexp is available
  15. 15. How to create library on Crystal?
  16. 16. $ crystal lib create Foo::Bar
  17. 17. OK. Let’s write it!
  18. 18. Thank you for listening!

×