Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 37 ยท Getting Started

Ruby You Need for Chef

Learn the small part of Ruby that Chef recipes use every day: strings and interpolation, arrays, hashes, blocks, conditionals and loops, with runnable examples.

Chef recipes are Ruby files, but you do not need to be a Ruby expert to write them. A handful of features cover almost everything: strings with interpolation, arrays and hashes, blocks, conditionals and loops. This lesson teaches exactly those, with examples in the shape you will meet in cookbooks. For the full language, see the Ruby tutorial.

Strings and interpolation

Single quotes give you the text exactly as written. Double quotes allow #{...} interpolation, which inserts the value of any Ruby expression. Chef style guides prefer single quotes unless you interpolate.

Ruby Strings and interpolation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

You will use interpolation for file paths, command lines and messages, for example "/etc/#{node['app']['name']}.conf".

Arrays

An array is an ordered list. Chef uses arrays for actions (action [:enable, :start]), run lists and lists of packages.

Ruby Arrays and each

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

In a recipe, the very same loop creates one resource per package:

Ruby
%w(git curl vim).each do |pkg|
  package pkg
end

Hashes and symbols

A hash maps keys to values. Chef attributes are hashes, and you read them with square brackets. A symbol such as :install is a lightweight name that never changes, used for actions and many property values.

Ruby Hashes, nested hashes and symbols

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

In real Chef, node['nginx']['port'] reads an attribute exactly like this. Symbols and strings are different keys: options[:owner] and options['owner'] are not the same entry in plain Ruby (Chef attributes are more forgiving, as you will see later).

Blocks

A block is a chunk of code passed to a method, written with do ... end or braces. Every resource in a recipe is a method call with a block:

Ruby
package 'nginx' do
  action :install
end

You have already seen blocks with each. They are also how resources get their properties. The next example shows a method that receives a block and calls it.

Ruby Writing a method that takes a block

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

yield hands control to the block. That is the whole mechanism behind the resource DSL, as the first recipe lesson showed.

Conditionals

Recipes often act depending on the platform or an attribute. Ruby gives you if, elsif, else, unless and case.

Ruby if, unless and case

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

Real recipes read the platform from node['platform_family'] (for example 'debian' or 'rhel') rather than a variable you set yourself, and the Ohai lesson explains where that value comes from.

Putting it together

This stand-in mixes everything: a hash of attributes, an array, a loop and a conditional produce a list of "resources". It is plain Ruby, not Chef.

Ruby A recipe-shaped Ruby program

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

Use fetch instead of [] when a missing key is a bug. fetch raises an error with the key name, while [] silently returns nil and the failure shows up far away.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which string interpolates the variable name?

    Only double-quoted strings evaluate #{...}. Single quotes keep the text literally.
  2. 2. What does %w(a b c) create?

    %w builds an array of words, which is handy for lists of packages.
  3. 3. How does Chef's resource syntax relate to Ruby blocks?

    package 'nginx' do ... end is a method call that receives a block holding the properties.
  4. 4. What is :install in Ruby?

    Symbols are immutable names, used for actions and options.

Summary

  • Double quotes interpolate with #{}; single quotes are literal.
  • Arrays hold lists, hashes hold key-value pairs (attributes are hashes), symbols name actions.
  • Resources are method calls with blocks.
  • if, unless, case and each cover most recipe logic.

Next: Chef Architecture