Skip to content

Instantly share code, notes, and snippets.

@michaelvobrien
Last active October 5, 2017 18:33
Show Gist options
  • Save michaelvobrien/fd87b9941d30985234cbab6710299abf to your computer and use it in GitHub Desktop.
Save michaelvobrien/fd87b9941d30985234cbab6710299abf to your computer and use it in GitHub Desktop.

Ruby Overview

IRB and Signals

  • CTRL-C sends INT ("interrupt") signal. Ignores input and creates a new prompt.
  • CTRL-D sends EOT ("end of transmission") signal. Exits.
  • CTRL-Z sends TSTP ("terminal stop") signal. Nothing.
  • CTRL-\ sends QUIT signal. Unknown. (Note: does not dump core like other processes.)
  • exit

Kernel

Math

IO

Strings

Date/Time

Net

Shell / Command Line


Magic Variables

The $_ variable.

ruby -e 'puts $_ while STDIN.gets'
ruby -e 'STDIN.each_line { |l| puts l.upcase }'
ruby -ne 'puts $_.upcase'
ruby -pe '$_.upcase!'

Arrays

Enumeration, Streams, and Maps

Security

Objects

Introspection

Profiling

Process

File Formats

Safe Navigation Operator

# rails
obj.try!(:foo)

# ruby 2.3.0
obj&.foo

Array#dig and Hash#dig

numbers = [2, [3, [6]]]
six = numbers.fetch(1).fetch(1).fetch(0)
six = numbers.dig(1, 1, 0)

person = {
  name: {
    first_name: "A",
    last_name: "Z"
  }
}

first_name = person.fetch(:name).fetch(:first_name)
first_name = person.dig(:name, :first_name)

Other

May Be Useful

Run

Library

How To Use

ruby [switches] [--] [programfile] [arguments]

  -rlibrary       require the library before executing your script
  -e 'command'    one line of script. Several -e's allowed.
ruby -run -e COMMAND [--] [OPTIONS]

Note: POSIX Utility Syntax Guideline 10:

Guideline 10:    The argument "--" should be accepted as a delimiter
                 indicating the end of options.  Any following
                 arguments should be treated as operands, even if they
                 begin with the '-' character.  The "--" argument
                 should not be used as an option or as an operand.

Examples

help

ruby -run -e help [COMMAND]
ruby -run -e help

httpd

ruby -run -e httpd -- [OPTION] DocumentRoot
ruby -run -e httpd -- .
ruby -run -e httpd -- --port 8000 .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment