Ruby Cheat Sheet

HTTP & CURL

make a Curl request with authentication

STRING

string with variable

puts "This is a variable #{myVar}"

JSON

require 'json'

jsonfile = File.read(ARGV[0])

data = JSON.parse(jsonfile)

parse/iterate json string (embedded values)

data['issues'][0]['fields']['issuelinks'].each do |link|

puts link['outwardIssue']['key']

end

GEM

install gem thru proxy with no SSL

http_proxy="http://my.proxy:8500" gem install vagrant-vsphere --source http://rubygems.org

remove gem

gem uninstall -i /usr/local/share/gems rspec-core -v 3.7.0

install specific version of gem

gem install rake -v 2.3

download Gem locally

gem fetch gem-name

install local Gem

gem install --local /path/to/gem

OS

change to a different dir

print working dir

check if file exists

STRINGS

convert to string

567.to_s

reverse string

"hello there".reverse

567.to_s.reverse // 765

convert to integer

"abcd".to_i

convert to array

"abc".to_a

remove last 3 characters

mystring = "beers for everyone"

mystring = mystring[0..mystring.size-4]

// beers for every

remove specific characters

'blue moon'.chomp('blue')

moon

capitalize, upcase, downcase, etc

myString.capitalize # Dog Is Hungry

myString.upcase # DOG IS HUNGRY

myString.downcase # dog is hungry

INPUT

get input from cmd line

get variables into script from user input

FILE HANDLING

open file for reading

open file for reading & writing

check if file exists

save incoming Json into file

VARIABLES

Variables & Constants

$global_variable

@instance_variable

[OtherClass::]CONSTANT # eg. Math::PI

local_variable

Array

create new Array

myarray = []

add to array

myarray << 'value1'

myarray << 34

myarray << 'value2'

can also use 'push' to add to end of array,

myarray.push('carrot')

add to front of array

myarray.unshift('apple')

add to a specific place wihtin the array

myarray.insert(3, 'pencil') #inserts word pencil into 4th place from beginning

count # of values in array

myarray.count

Print out each element

myarray.each do |arg|

puts arg

end

Array Uniq

[1,1,1,2,3,4,3,3].uniq

=> [1,2,3,4]

Hashes

define new hash

fruits = Hash.new() or fruits = {}

CONDITIONALS

If, Else If

if a > b # body elsif b > a # body else # body end

Unless

unless a > b # 'unless a == b' is same as 'if a != b' # body else # body end

Case

case grade when "A" puts 'Well done!' when "B" puts 'Try harder!' when "C" puts 'You need help!!!' else puts "You just making it up!" end

While Loop

while conditional [do] code end code while condition begin code end while conditional

Until Loop

until conditional [do] code end code until conditional begin code end until conditional

Each Loop

(expression).each do |variable[, variable...]| code end

For Loop

for i in 0..5 if i > 2 then break # use break to exit the loop end puts "Value of local variable is #{i}" end

Range

1..10 'a'..'z' (1..10) # inclusive (1...10) # exclusive

FUNCTIONS

convert to integer, string or float

Iteration (using Next function)

Print args multiple args

Generator Functions

Range

MATH

Return

get min, max, sum

generate random number