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
  2. Getting started with Ruby Eindhoven.rb #4 — 5 april 2011

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

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

    Arjan van der Gaag
  5. None
  6. $ ruby app.rb

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

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

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

    -rubygems app.rb $ ruby -I lib app.rb
  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
  11. None
  12. $ ruby -e ‘puts “Hello, world”’

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

  14. while gets # execute app.rb here end

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

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

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

    $ ruby -p app.rb
  18. while gets # execute app.rb here print end

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

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

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

    $ ruby -p app.rb $ ruby -lpe app.rb
  22. while gets $_.chomp! # execute app.rb here print end

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

    $ ruby -p app.rb $ ruby -lpe app.rb
  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
  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
  26. while gets $F = $_.split $, # execute app.rb here

    end
  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
  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
  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
  30. #!/usr/bin/env ruby -wanp -r digest -F, next unless $F[1] gsub

    $F[1], Digest::SHA1.hexdigest($F[1])
  31. # Read all values in ARGV as files as #

    a single stream OR read from STDIN puts ARGF.read puts $<.read
  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
  33. - OptParse - StrScanner - OpenStruct - Set - CSV

    - Find - FileUtils - Tmpfile - Zlib - Abbrev Know your StdLib
  34. None
  35. $ ruby -h

  36. github.com/avdgaag @avdgaag