Ruby vs. The World

Ruby vs. The World

RubyConf 2012 presentation covering Go, Clojure, Scala and how these languages affected the way I write Ruby code.
Video: http://confreaks.com/videos/1288-rubyconf2012-ruby-vs-the-world

C69521d6e22fc0bbd69337ec8b1698df?s=128

Matt Aimonetti

November 02, 2012
Tweet

Transcript

  1. Friday, November 2, 12

  2. matt aimonetti @merbist http://matt.aimonetti.net http://github.com/mattetti Friday, November 2, 12

  3. special day! Friday, November 2, 12

  4. f dia de los muertos x Friday, November 2, 12

  5. Friday, November 2, 12

  6. Human Languages studied by linguists Friday, November 2, 12

  7. Sapir-Whorf Hypothesis linguistic differences have consequences on human cognition and

    behavior The principle of linguistic relativity holds that the structure of a language affects the ways in which its speakers conceptualize their world, i.e. their world view, or otherwise influences their cognitive processes. Friday, November 2, 12
  8. Studying Programming Languages Friday, November 2, 12

  9. How not to do it Friday, November 2, 12

  10. Hello World! Friday, November 2, 12

  11. Syntax Friday, November 2, 12

  12. How to do it Friday, November 2, 12

  13. Friday, November 2, 12

  14. Karl Marx Friday, November 2, 12

  15. Friday, November 2, 12

  16. Not Karl Marx Friday, November 2, 12

  17. Friday, November 2, 12

  18. interpreted compiled dynamically typed statically typed object oriented functional difficulty

    JavaScript X X X* X easy Ruby/Python X X X X normal CoffeeScript X X X X normal Objective-C X X X X * nightmare Java X X X hard Go X * structural X X* X normal Clojure X X (hints) * X normal/ nightmare/ hell Scala X * structural X inferred X X normal/ nightmare Friday, November 2, 12
  19. Friday, November 2, 12

  20. Part I Friday, November 2, 12

  21. How did that affect the way I write Ruby? Part

    II Friday, November 2, 12
  22. Format Friday, November 2, 12

  23. class Presentation attr_accessor :name, :topic def initialize(name, topic) self.name =

    name self.topic = topic end def start puts "Hi, my name is #{name} \ and I will talk about #{topic}." end end talk = Presentation.new("Matt", "Ruby") talk.start code example ported to each language Friday, November 2, 12
  24. .Use case .Philosophy .what I dislike .what I like .where

    to start Friday, November 2, 12
  25. Go Friday, November 2, 12

  26. compiled structural typed object oriented* functional learning: normal Friday, November

    2, 12
  27. package demo import "fmt" type Presentation struct { " Name,

    Topic string } func (p *Presentation) start() string { " return "This is " + p.Name + ", and I will talk about " + p.Topic + "." } func main() { " talk := &Presentation{Name: "Matt", Topic: "Go"} " fmt.Println(talk.start()) } Friday, November 2, 12
  28. Use case: concurrency Friday, November 2, 12

  29. package main import ( " "fmt" " "net/http" " "time"

    ) var urls = []string{ " "http://rubyconf.com/", " "http://golang.org/", " "http://matt.aimonetti.net/", } type HttpResponse struct { " url string " response *http.Response " err error } Friday, November 2, 12
  30. func asyncHttpGets(urls []string) []*HttpResponse { " ch := make(chan *HttpResponse,

    len(urls)) // buffered " responses := []*HttpResponse{} " for _, url := range urls { " " go func(url string) { " " " fmt.Printf("Fetching %s \n", url) " " " resp, err := http.Get(url) " " " ch <- &HttpResponse{url, resp, err} " " }(url) " } " for { " " select { " " case r := <-ch: " " " fmt.Printf("%s was fetched\n", r.url) " " " responses = append(responses, r) " " " if len(responses) == len(urls) { " " " " return responses " " " } " " default: " " " fmt.Printf(".") " " " time.Sleep(5e7) " " } " } " return responses } Friday, November 2, 12
  31. func main() { " results := asyncHttpGets(urls) " for _,

    result := range results { " " fmt.Printf("%s status: %s\n", result.url, result.response.Status) " } } Friday, November 2, 12
  32. $ go build concurrency_example.go && ./a.out .Fetching http://rubyconf.com/ Fetching http://golang.org/

    Fetching http://matt.aimonetti.net/ .....http://golang.org/ was fetched .......http://rubyconf.com/ was fetched .http://matt.aimonetti.net/ was fetched http://golang.org/ status: 200 OK http://rubyconf.com/ status: 200 OK http://matt.aimonetti.net/ status: 200 OK http://bit.ly/go-async-http Friday, November 2, 12
  33. Philosophy The new “C” Friday, November 2, 12

  34. what I dislike A bit too low level for some

    Not so great GC Limited adoption* Odd conventions at times Friday, November 2, 12
  35. what I like simple specs modern std libs concurrency (goroutines/channels)

    sensible conventions fast compilation flexible code organization simpler take on OO features of FP error handling documentation source as documentation Friday, November 2, 12
  36. how to get started http://tour.golang.org Friday, November 2, 12

  37. Clojure Friday, November 2, 12

  38. compiled dynamically typed (w/ hints) functional “object oriented*” learning: normal/nightmare

    Friday, November 2, 12
  39. (defprotocol Talk "A conference talk" (start [p] "return the speaker

    and topic.")) (defrecord Presentation [name topic] Talk; implement the Talk protocol (start [_] (str "Hi, this is " name " and I will talk about " topic "."))) (def talk (Presentation. "Matt" "Clojure")) (start talk) Friday, November 2, 12
  40. Use case: Data Processing Friday, November 2, 12

  41. (ns example.word-count (:use clojure.contrib.io clojure.contrib.seq-utils)) (defn parse-line [line] (let [tokens

    (.split (.toLowerCase line) " ")] (map #(vector % 1) tokens))) (defn combine [mapped] (->> (apply concat mapped) (group-by first) (map (fn [[k v]] {k (map second v)})) (apply merge-with conj))) (defn sum [[k v]] {k (apply + v)}) (defn reduce-parsed-lines [collected-values] (apply merge (map sum collected-values))) (defn word-frequency [filename] (->> (read-lines filename) (map parse-line) (combine) (reduce-parsed-lines))) Friday, November 2, 12
  42. Philosophy The pragmatic Scheme Friday, November 2, 12

  43. what I dislike not so simple not always consistent need

    to know a lot of functions/macros not really web focused brain stack overflow hard mental context switch meaningless error stacks Friday, November 2, 12
  44. what I like really great for data processing isolate problems

    efficiently fast java interop Friday, November 2, 12
  45. how to get started https://github.com/functional-koans/clojure-koans Friday, November 2, 12

  46. Friday, November 2, 12

  47. compiled static/inferred/dynamic type object oriented & functional Learning: normal/hard Friday,

    November 2, 12
  48. class Presentation(val name: String, val topic: String) { val start

    = "My name is "+ name + " and I will talk about " + topic +"." } val talk = new Presentation("Matt", "Scala") talk.start Friday, November 2, 12
  49. Use case: Service Oriented Architecture Whenever you need a more

    “entreprisey” Ruby Friday, November 2, 12
  50. Twitter’s Finagle Friday, November 2, 12

  51. Friday, November 2, 12

  52. Future + pipeline Friday, November 2, 12

  53. val authenticatedUser: Future[User] = User.authenticate(email, password) val lookupTweets: Future[Seq[Tweet]] =

    authenticatedUser flatMap { user => Tweet.findAllByUser(user) } Friday, November 2, 12
  54. for { user <- User.authenticate(email, password) tweets <- Tweet.findAllByUser(user) }

    yield tweets Friday, November 2, 12
  55. Async, blocking or not futures FP & OOP both fully

    embraced Friday, November 2, 12
  56. val service = new Service[HttpRequest, HttpResponse] { def apply(request: HttpRequest)

    = Future(new DefaultHttpResponse(HTTP_1_1, OK)) } val address = new InetSocketAddress(10000) val server: Server[HttpRequest, HttpResponse] = ServerBuilder() .name("MyWebServer") .codec(Http()) .bindTo(address) .build(service) Friday, November 2, 12
  57. Enumerable Anonymous methods Default method param Meta programming method_missing duck

    typing Mixins Focus on testing Friday, November 2, 12
  58. Focus on Tests Inner methods Lazy evals/streams Great GC Performance

    Tools/IDE Friday, November 2, 12
  59. Philosophy The academic version of Ruby Ruby minus scripting plus

    static typing Friday, November 2, 12
  60. what I dislike huge surface stiff learning curve abused o_O::

    syntax poor documentation feels like it’s trying to do everything JVM Friday, November 2, 12
  61. what I like Close to Ruby/Python Does a lot of

    what Clojure offers Easy to get started Inferred types Flexible functional approach Modern concerns (parallelism) Pattern matching Relatively rich ecosystem Community JVM/CLR Friday, November 2, 12
  62. where to get started Twitter’s Scala school A Tour of

    scala Coursera’s online class Friday, November 2, 12
  63. How did that affect the way I write Ruby? Part

    II Friday, November 2, 12
  64. Friday, November 2, 12

  65. Ruby is (also) a functional programming language Friday, November 2,

    12
  66. fp & oop are complementary Friday, November 2, 12

  67. fp when extend with new operations oop when extend with

    new data Friday, November 2, 12
  68. fp when your data doesn’t change oop when your data

    evolves Friday, November 2, 12
  69. It’s not because you can that you should Friday, November

    2, 12
  70. Typing Friday, November 2, 12

  71. Testing != Documentation Friday, November 2, 12

  72. Error handling Friday, November 2, 12

  73. Compilers Friday, November 2, 12

  74. Simplicity matters Friday, November 2, 12

  75. Ruby’s limits Friday, November 2, 12

  76. Performance Concurrency Freedom Friday, November 2, 12

  77. “ruby” developer Friday, November 2, 12

  78. problem solver Friday, November 2, 12

  79. product builder Friday, November 2, 12

  80. <insert language> is just a detail Friday, November 2, 12

  81. but choose wisely Friday, November 2, 12

  82. languages shape how you solve problems Friday, November 2, 12

  83. be curious learn a new language Friday, November 2, 12

  84. and go build awesome stuff Friday, November 2, 12

  85. Feedback Questions Friday, November 2, 12

  86. matt aimonetti @merbist http://matt.aimonetti.net http://github.com/mattetti Friday, November 2, 12