GitHub Repos

Sadly by necessity some of my repos are private. Those that are private are clearly marked. For those that are, please don't ask me to share the code, because I can't. They're listed here purely for my reference.

Tutorials

Miscellaenous

  • Ruby cheatsheet
  • Symbols do hold strings of characters, they are just immutable

    • This means that when you reference a symbol in string interpolation, what is printed out will be the name of the symbol
    • So #{:node} will give node as the output
  • A variable that has a string as its value will be mutable, but a symbol is immutable, and stored in a single place in memory

  • Return values in Ruby functions are the last thing that was assigned

    • the return statement is often not used
  • IRB is the standard Ruby repl (run irb on command line, then type exit to leave)

  • The puts statement is how you can output to console - useful for quick-and-dirty debug logging.

    • If you want to get the output from something in a Ruby script onto the command line and into a pipe: Use the puts keyword in the Ruby script
    • If Chef, you can use knife exec and then pipe the output to other commands
    • Otherwise just run the script with the script name, and pipe straight to something else
  • Boolean methods should be suffixed with a question mark. More here. However this is a convention and is not enforced. It's possible to write a function suffixed with a question mark that doesn't return a bool. In fact it's really used to indicate that the function is asking a question - the answer might not be a bool in practice. More here.

  • Methods that end in ! indicate that the method will modify the object it's called on. Ruby calls these "dangerous methods" because they change state that someone else might have a reference to.

  • Require, gems, loading file: https://medium.com/@connorstack/understanding-ruby-load-require-gems-bundler-and-rails-autoloading-from-the-bottom-up-3b422902ca0

    • If you get the error “cannot load such file” you need to check your load path by running irb on command line and then typing $LOAD_PATH
  • If you get “cannot load such file -- bundler/setup (LoadError)” then you might need to run bundle install or vendor install or both

    • If you get “command not found: bundle” then you might need to install Bundler: sudo gem install bundler

      • !! The password it wants is your laptop password
  • Calling javascript code from Ruby: https://github.com/sstephenson/execjs

  • Static methods and the self keyword

  • Debugging Ruby with breakpoints in VS Code

Hashes

Frozen values

Arrays

  • Negative indices:

    • To get last array element, use negative index: [-1]
    • The second-to-last element has index -2, and so on.

Strings

  • gsub: Find and replace

String interpolation

  • Do it like this: "search/#{type}”, where type is a variable

    • !! It only works in double quotes!
    • In fact it's not recommended to use ' in ruby. Use " instead.
  • It doesn’t seem to work in a Ruby script run via knife exec - instead, you can do this:

    • This: query="fqdn:"+ARGV[2]
  • See below for how to do string interpolation with symbols

Testing

Rspec

Symbols

  • Symbols are things that look like this :symbol

    • The closest thing in C# is an enum
    • The symbol itself is the value - they are not variables and you don’t assign to them
    • Symbols do hold strings of characters, they are just immutable
    • This means that when you reference a symbol in string interpolation, what is printed out will be the name of the symbol
    • So #{:node} will give node as the output
  • A variable that has a string as its value will be mutable, but a symbol is immutable, and stored in a single place in memory