ruby -e ' puts "hello world" ' # ruby filename.rb (non interactive ruby)
irb # interactive ruby prompt
MYCONSTANT = "hello" # begins with a capital, like Myconstant, constants can be modified! a, b, c, d = 10, 20, 30, 40 # multiple assignment a = 2 a.class # Fixnum a.kind_of? Integer # True # similar to type( a ) or class( a ) b = a.to_f # convert to float b.class # Float b.to_s( 2 ) # ArgumentError 3.to_s( 2 ) # 11 # convert to string, maybe binary? a.to_i() # 2 # convert to int
numbers = [1, 2, 3, 4, 5] puts numbers.inspect
numbers.each do |n| # for each loop puts n end
letters = %w{ a b c d e f g h} # weird ruby array puts letters.first # first element puts lettersts.last # last element puts letters.at( 3 ) # element at index 3 sould be d
Chef::Log()
[1, 2, 3].map { |n| n * n }
[1, 4, 9] # for each item in list, do the ruby block
if a > 2 puts "a is greater than 2" elsif a <= 2 and a != 0 puts "a is 1" else puts "a is less than 0" end
print "a is: " + (a == 10 ? "10" : "Not 10") # ternary print "debug\n" if $debug # prints debug if debug is true (or greater than 0)
case $age: when 0 .. 2 puts "baby" when 3 .. 13 puts "child" when 13 .. 17 puts "teenager" else puts "adult" end
TODO: unless
TODO: how to install ruby (yikes versions 1.8.7, 1.9, and more!)
begin result = exec("/usr/bin/python /home/ubuntu/myscript.py") rescue Exception => e puts e.message puts e.backtrace.inspect puts "rescue myscript" end