Show Menu

Ruby Essentials for Kids Cheat Sheet by

Essentials for getting started with Ruby
programming     ruby     essentials

Command Line

From the command prompt run Ruby, providing the name of a file with source code:
$ ruby my_sou­rce­-co­de.rb

Intera­ctive Ruby

The Intera­ctive Ruby Shell can be used for intera­ctive progra­mming:
$ irb
>>

Loading files

In IRB, files can be loaded using the load command:
>> load 'scrip­t_n­ame.rb'

Input

gets gets a string e.g.:
direction = gets.chomp

Indent­ation

Useful but not necessary.

If / Else Loops

if my_variable_is_true && my_other_variable_is_true
  dosomething
elseif !my_next_variable_is_false
  dosomethingelse
else
  dothis
end

for loop - next

next skips to the next element

Unless

unless my_variable_is_true
  # dosomething
end

Console output

Use puts to display output.

Object methods

object_id
The object ID

Data Types

Boolean
true or false
numbers
strings
' or " quoted
 

Number methods

odd
even

String Methods

*
Repeat string
length
Number of characters in the string
reverse
A string with characters listed in reverse
to_sym
Convert to symbol

String Interp­olation

Can be used to insert variable values in a double quoted string:
“String with interp­ola­tion: #{vari­abl­e_n­ame}”

Operators

+
2+2
-
5-3
*
5*3
/
8/2
next
The next number
pred
The predec­essor

Logical Operators

&&
and
||
or
!
not

Comparison operators

<
less than
>
greater than
<=
less than or equal to
>=
greater than or equal to
!=
not equal to

Ranges

.. from first to last
... from first to predec­essor before last

Symbols

Created using :
eg string­_test = :test_­symbol
More efficient than strings - only stored once

Symbol methods

to_s
To string
 

Arrays

Described in [] with items delimited by commas:
meal = ['brea­kfast', 'lunch', 'dinner']

Array methods

new
Create an array
<<
Append to the end of an array
empty?
Is array empty
shift
Remove front element from array
unshift
Add new element at front of array
pop
Remove element from end of array
push
Add element to end of array

Array loop

for my_placeholder in my_array
  next if something_is_true
  do_something
end

Array loop example 2

my_array.each do |my_placeholder|
  next if something_is_true
  do_something
end

Array loop example 3

my_array.each { |my_placeholder| next if something_is_true do_something }

Hashes

Contain key value pairs. Can use symbols for keys.

Hash methods

has_key?
Has the key
has_value?
Has the value
my_has­h[:­my_key] = my_value
Set has value

Defining Methods

def my_method(argument_1,
      argument_with_default = 1,
      *splat_argument)
  # method body
  # use return keyword to return a value
  # better practice to omit return keyword
  puts "argument_1: #{argument_1}"
  puts "argument_with_default: #{argument_with_default}"
  return splat.length
end

Yield in Blocks

The yield keyword is used to invoke a calling block from within a method. Control is passed back to the method after the block has been invoked.

References

Ruby Wizardry by Eric Weinstein

Download the Ruby Essentials for Kids Cheat Sheet

2 Pages
//media.cheatography.com/storage/thumb/simon-fermor_ruby-essentials-for-kids.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          vim-rails Cheat Sheet

          More Cheat Sheets by Simon Fermor

          TiddlyWiki Cheat Sheet
          Markdown Cheat Sheet