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.

Rubyistを誘うScalaの世界 2.0

2,143 views

Published on

in Kawasaki Ruby Kaigi

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Rubyistを誘うScalaの世界 2.0

  1. 1. Rubyist Scala 2.0 in Kawasaki Ruby Kaigi @Peranikov
  2. 2. • (Matsukubo Yuto) • @Peranikov • Kawasaki.rb 
 http://kawasakirb.github.io/ • Ruby • )Socket Ruby Scala • We’re hiring!
  3. 3. T
  4. 4. Rubyist Scala Kawasaki.rb Ruby
  5. 5. Scala ?✋
  6. 6. Rubyist Scala
  7. 7. • Ruby 
 Scala • 
 (Rubyist ) •
  8. 8. Scala
  9. 9. Scala • • JVM ( .NET ) • Java BetterJava
  10. 10. Ruby ?
  11. 11. Ruby ( ) • • • Mix-In • Open Class • method_missing •
  12. 12. Ruby ( ) • • • Mix-In • Open Class • method_missing • Scala !
  13. 13. Ruby 1 + 2 # => 3 1.+(2) # => 3
  14. 14. Scala 1 + 2 // => 3 1.+(2) // => 3
  15. 15. Scala class MyClass { // () def smile: String = { " :-)" } // def smile(str: String): String = { str + " :-)" } // {} def smile2(str: String) = str + " :-)" }
  16. 16. Scala val obj = new MyClass // () // obj.smile // obj.smile("Hi,") // 1 () obj smile "Hi,"
  17. 17. Ruby • Enumerable#map(collect) • Enumerable#flat_map • Enumerable#reduce(inject) • Enumerable#select • Enumerable#find • etc…
  18. 18. Scala • Traversable#map • Traversable#flatMap • Traversable#reduceLeft • Traversable#filter • Traversable#find • etc…
  19. 19. : Ruby map [1, 2, 3, 4].map { |i| i * 2 } # => [2, 4, 6, 8]
  20. 20. Scala List(1, 2, 3, 4).map { i => i * 2 } // => List(2, 4, 6, 8) Ruby !
  21. 21. Scala ( ) List(1, 2, 3, 4).map { _ * 2 } // => List(2, 4, 6, 8)
  22. 22. Q. ? A. Ruby • Scala map List(1,2,3).map { i => i * 2 } List(1,2,3).map( i => i * 2 ) Scala {}
  23. 23. Ruby lambda # Ruby f = lambda { |i| i + 10 } f.call(20) # => 30 // Scala val f = (i: Int) => i + 10 f(20) // => 30
  24. 24. :size or length? • List • size length (size length ) • Ruby List(1,2,3).length // => 3 List(1,2,3).size // => 3
  25. 25. Mix-In
  26. 26. Ruby module Mix-in module Monster def roar ' ' end end class Godzilla include Monster end Godzilla.new.roar # => " "
  27. 27. Scala Trait trait Monster { def roar = " " } class Godzilla extends Monster (new Godzilla).roar // => " "
  28. 28. trait Monster { def roar = " " } trait HasTail { def swing = " " } class Godzilla extends Monster with HasTail
  29. 29. trait Monster { def roar: String } class Godzilla extends Monster { def roar = " " } (new Godzilla).roar // => " "
  30. 30. Open Class
  31. 31. Ruby Open Class class String def replace_to_scala self.gsub('ruby', 'scala') end end "ruby is nice!".replace_to_scala # => "scala is nice!"
  32. 32. Scala Open Class( ) implicit class MyString(val s: String) def replaceToScala = { s.replaceAll("ruby", "scala") } } "ruby is nice!".replaceToScala // String = scala is nice!
  33. 33. implicit class MyString(val s: String) def replaceToScala = { s.replaceAll("ruby", "scala") } } "ruby is nice!".replaceToScala // String = scala is nice!
  34. 34. implicit conversion • • def concat(i: String, j: String): String = { i + j } implicit def intToString(src: Int): String = { src.toString } concat(100, 200) // => 100200
  35. 35. method_missing
  36. 36. Ruby method_missing class MyClass def method_missing(name) "#{name} is missing!!" end end MyClass.new.foo # => "foo is missing!!"
  37. 37. Scala method_missing (Dynamic ) import scala.language.dynamics class MyClass extends Dynamic { def selectDynamic(name: String): String = { s"${name} is missing!" } } (new MyClass).foo // => foo is missing!
  38. 38. Duck Typing
  39. 39. Ruby Duck Typing •
  40. 40. Scala Duck Typing
 (Structural Subtyping) • • • : roar
  41. 41. 1. type Roarabel = { def roar: String }
  42. 42. 2. class KingGhidorah { def roar: String = " " } class Mothra { def roar: String = " " }
  43. 43. 3. def doRoar(target: Roarabel) = { target.roar } doRoar(new KingGhidorah) // => “ ” doRoar(new Mothra) // => “ ”
  44. 44. Scala • • Future • Scala.js … JS Scala • Scala Native … Scala LLVM • Dotty … Scala
  45. 45. • Scala • Ruby Scala • Rubyist Scala !

×