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

Getting started with ruby

Getting started with ruby

A lightning talk for Eindhoven.rb about getting started with various command-line options for the ruby program.

Arjan van der Gaag

April 06, 2012
Tweet

More Decks by Arjan van der Gaag

Other Decks in Programming

Transcript

  1. Getting started
    with ruby
    Eindhoven.rb #4 — 5 april 2011
    Arjan van der Gaag

    View Slide

  2. Getting started
    with Ruby
    Eindhoven.rb #4 — 5 april 2011
    Arjan van der Gaag

    View Slide

  3. Getting started
    with Ruby
    Eindhoven.rb #4 — 5 april 2011
    Arjan van der Gaag

    View Slide

  4. Getting started
    with ruby
    Eindhoven.rb #4 — 5 april 2011
    Arjan van der Gaag

    View Slide

  5. View Slide

  6. $ ruby app.rb

    View Slide

  7. $ ruby app.rb
    $ ruby -r “mylib” app.rb

    View Slide

  8. $ ruby app.rb
    $ ruby -r “mylib” app.rb
    $ ruby -rubygems app.rb

    View Slide

  9. $ ruby app.rb
    $ ruby -r “mylib” app.rb
    $ ruby -rubygems app.rb
    $ ruby -I lib app.rb

    View Slide

  10. # lib/post.rb
    class Post
    attr_accessor :title, :author
    end
    # app.rb
    require "post"
    get '/' do
    @posts = Post.all
    erb :index
    end
    $ ruby -rubygems -r sinatra -I lib app.rb

    View Slide

  11. View Slide

  12. $ ruby -e ‘puts “Hello, world”’

    View Slide

  13. $ ruby -e ‘puts “Hello, world”’
    $ ruby -n app.rb

    View Slide

  14. while gets
    # execute app.rb here
    end

    View Slide

  15. while $_ = gets
    # execute app.rb here
    end

    View Slide

  16. $ ruby -e ‘puts “Hello, world”’
    $ ruby -n app.rb

    View Slide

  17. $ ruby -e ‘puts “Hello, world”’
    $ ruby -n app.rb
    $ ruby -p app.rb

    View Slide

  18. while gets
    # execute app.rb here
    print
    end

    View Slide

  19. while $_ = gets
    # execute app.rb here
    print $_
    end

    View Slide

  20. $ ruby -e ‘puts “Hello, world”’
    $ ruby -n app.rb
    $ ruby -p app.rb

    View Slide

  21. $ ruby -e ‘puts “Hello, world”’
    $ ruby -n app.rb
    $ ruby -p app.rb
    $ ruby -lpe app.rb

    View Slide

  22. while gets
    $_.chomp!
    # execute app.rb here
    print
    end

    View Slide

  23. $ ruby -e ‘puts “Hello, world”’
    $ ruby -n app.rb
    $ ruby -p app.rb
    $ ruby -lpe app.rb

    View Slide

  24. $ ruby -e ‘puts “Hello, world”’
    $ ruby -n app.rb
    $ ruby -p app.rb
    $ ruby -lpe app.rb
    $ ruby -pe
    “next unless /<{7}/../>{7}/”
    conflicted_file

    View Slide

  25. $ ruby -e ‘puts “Hello, world”’
    $ ruby -n app.rb
    $ ruby -p app.rb
    $ ruby -lpe app.rb
    $ ruby -pe
    “next unless /<{7}/../>{7}/”
    conflicted_file
    $ ruby -an -F, -e ‘puts $F[0]‘
    csv_file

    View Slide

  26. while gets
    $F = $_.split $,
    # execute app.rb here
    end

    View Slide

  27. $ ruby -e ‘puts “Hello, world”’
    $ ruby -n app.rb
    $ ruby -p app.rb
    $ ruby -lpe app.rb
    $ ruby -pe
    “next unless /<{7}/../>{7}/”
    conflicted_file
    $ ruby -an -F, -e ‘puts $F[0]‘
    csv_file

    View Slide

  28. $ ruby -e ‘puts “Hello, world”’
    $ ruby -n app.rb
    $ ruby -p app.rb
    $ ruby -lpe app.rb
    $ ruby -pe
    “next unless /<{7}/../>{7}/”
    conflicted_file
    $ ruby -an -F, -e ‘puts $F[0]‘
    csv_file
    $ ruby -i -p -e
    ‘$_.downcase!’ file

    View Slide

  29. #!/usr/bin/env ruby -w
    # Example input:
    #
    # john,secret
    # graham,test
    require 'digest'
    while line = ARGF.gets("\n") do
    parts = line.split ','
    next unless parts[1]
    print line.gsub parts[1],
    Digest::SHA1.hexdigest(parts[1])
    end

    View Slide

  30. #!/usr/bin/env ruby -wanp -r digest -F,
    next unless $F[1]
    gsub $F[1],
    Digest::SHA1.hexdigest($F[1])

    View Slide

  31. # Read all values in ARGV as files as
    # a single stream OR read from STDIN
    puts ARGF.read
    puts $<.read

    View Slide

  32. - $/ input record separator (default \n)
    - $\ output record separator (default \n)
    - $, output field separator (default nil)
    - $; input field separator (default nil)
    - $_ current line
    - $. number of current line
    - $F result of $_.split($;)
    Ruby globals

    View Slide

  33. - OptParse
    - StrScanner
    - OpenStruct
    - Set
    - CSV
    - Find
    - FileUtils
    - Tmpfile
    - Zlib
    - Abbrev
    Know your StdLib

    View Slide

  34. View Slide

  35. $ ruby -h

    View Slide

  36. github.com/avdgaag
    @avdgaag

    View Slide