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. $ ruby app.rb $ ruby -r “mylib” app.rb $ ruby

    -rubygems app.rb $ ruby -I lib app.rb
  2. # 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
  3. $ ruby -e ‘puts “Hello, world”’ $ ruby -n app.rb

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

    $ ruby -p app.rb $ ruby -lpe app.rb
  5. $ 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
  6. $ 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
  7. $ 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
  8. $ 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
  9. #!/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
  10. # Read all values in ARGV as files as #

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

    - Find - FileUtils - Tmpfile - Zlib - Abbrev Know your StdLib