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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  5. $ ruby app.rb

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  9. # 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 full-size slide

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

    View full-size slide

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

    View full-size slide

  12. while gets
    # execute app.rb here
    end

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  22. $ 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 full-size slide

  23. $ 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 full-size slide

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

    View full-size 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 full-size slide

  26. $ 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 full-size slide

  27. #!/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 full-size slide

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

    View full-size slide

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

    View full-size slide

  30. - $/ 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 full-size slide

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

    View full-size slide

  32. github.com/avdgaag
    @avdgaag

    View full-size slide