#!/usr/bin/ruby "hi" # double quoted string literal 'hi' # single quoted string literal %q{hi} # %q-quoted string literal # String interpolation x = 'something' interp = "say #{x}, say anything" -1 # prefix unary send 'hello,' + ' world' # infix send 'hello, world'.slice(0, 5) # keyword send: returns 'hello' 3.to_s # no-argument keyword send: returns string '3' [3, 4, 17, -2] # literal array x = 'hi' # assignment; returns left-hand-side a = b = c = x # chained assignments (assignments are expresions) # conditional expression; syntactic sugar for block version i = if x == 'hi' 3 else 7 end # a cased conditional expression print (case i when 0..2 then "low" when 3..6 then "medium" when 7..9 then "high" end \ + "\n") # control structures 3.times { print "hi!\n" } # the upto message of integers takes a block parameter 1.upto(10) { |x| print (x.to_s + "\n") } # the foreach message on arrays ['hi','hola','bonjour'].each { |x| print (x + ", world\n") } # I/O aFile = File.new("testfile") aFile.each_byte {|ch| putc ch; putc ?. } File.open("testfile", "r") { |aFile| # process file. aFile.each_line {|line| puts "Got #{line.dump}" } }