Saturday, May 7, 2011

Ruby Cheat Sheets

1.1 Ruby Shebang
#!/usr/bin/env ruby

2.1 String Literals (Double quotes and Single Quotes)
puts "Year #{Time.now.year}"  # => "Year 2011"
puts 'Year #{Time.now.year}'  # => "Year #{Time.now.year}"
puts "First line.\nSecond line." 
  # => "First line.
  #     Second line."
puts 'First line.\nSecond line.' 
  # => "First line.\nSecond line."
puts "Mary's Lamb"  # => "Mary's Lamb"
puts 'Mary\'s Lamb' # => "Mary's Lamb"   -- single quote escape sequence
puts "C:\\Program Files\\" # => "C:\Program Files\"
puts 'C:\\Program Files\\' # => "C:\Program Files\" -- backslash escape sequence

2.2. String Literals (using Custom Delimiter, "Here document")
t = <<MY_DELIMITER   # EOS, EOF, EOC are often used.
First line.
Second line.
MY_DELIMITER
puts t
  # => "First line.
  #     Second line."

2.3 String Concatenation
puts "ABC"+"DEF"  # => "ABCDEF" 
puts "ABC" "DEF"  # => "ABCDEF"  (same as above)
puts "*" * 20 # => "********************"


2.4 String Length and Byte Size
"the world".encoding.to_s # => "UTF-8" (in Ruby 1.9.x or later)
"the world".encoding # => NoMethodError (in Ruby 1.8.x)
"the world".length # => 9 (number of characters, not of bytes, in Ruby 1.9.x or later)
"あ".length # => 1 (one character long in Ruby 1.9.x)
"あ".length # => 3 (three bytes long in Ruby 1.8.x)
"あ".bytesize # => 3 (bytes in Ruby 1.9.x)
"First line\nSecond line".length # => 22 ("\n" is one character)
'First line\nSecond line'.length # => 23 ('\n' consists of two characters)


2.5 Uppercase and Lowercase
"the world".capitalize # => "The world"
"THE WORLD".capitalize # => "The world"
"the world".upcase # => "THE WORLD"
"THE WORLD".downcase # => "the world"


2.6 Substring
"abcdefg"[0] # => "a" (in Ruby 1.9.x; You get '97' Ascii code in Ruby 1.8.x)
"abcdefg"[1] # => "b" (in Ruby 1.9.x; You get '98' Ascii code in Ruby 1.8.x)
"abcdefg"[1,3] # => "bcd"  (string of 3 characters starting from position 1) 
"abcabc".include? "abc" # => true (the string contains the substring "abc")
"abcabc".index "abc" # => 0 (position of the first occurrence of the substring)
"abcabc".index "abc", 2 # => 3 (position of the first occurrence from the position 2)
"abcabc".rindex "abc" # => 3 (position of the first occurrence from the tail)

2.7 String Alignment
s = "Ruby String"
s.center(20)     # => "    Ruby String     " 
s.ljust(20)      # => "Ruby String         " 
s.rjust(20)      # => "         Ruby String" 
s.center(20,'*') # => "****Ruby String*****" 

2.8 Removing Last Character or End-Of-Line with chop or chomp
"Sentence\n".chop       # => "Sentence" (remove any last char)
"Sentence\n".chop.chop  # => "Sentenc"  (remove any last char)
"Sentence\n".chomp      # => "Sentence" (remove only last end-of-line code if any)
"Sentence\n".chmp.chmp  # => "Sentence" (remove only last end-of-line code if any)
"Sentence\r".chop       # => "Sentence" (remove any last char)
"Sentence\r".chop.chop   # => "Sentenc"  (remove any last char)
"Sentence\r".chomp       # => "Sentence" (remove only last end-of-line code if any)
"Sentence\r".chomp.chomp # => "Sentence" (remove only last end-of-line code if any)
# '\r\n' is considered as a single character.
"Sentence\r\n".chop        # => "Sentence" (remove any last char)
"Sentence\r\n".chop.chop   # => "Sentenc"  (remove any last char)
"Sentence\r\n".chomp       # => "Sentence" (remove only last end-of-line code if any)
"Sentence\r\n".chomp.chomp # => "Sentence" (remove only last end-of-line code if any)
# Multi-byte codes in Ruby 1.9.x (UTF-8)
"第二章".chop.chop            # => "第" (removed two characters)
# Multi-byte codes in Ruby 1.8.x (ASCII)
"第二章".chop.chop.chop.chop  # => "第" (removed four bytes: two two-byte characters)
# Destructive method!
s = "Sentence\n"
s.chomp  # => "Sentence"   (remove end-of-line code if any, but...)
s        # => "Sentence\n" (original string not affected)
s.chomp! # => "Sentence"   (remove end-of-line code if any, and ...)
s        # => "Sentence"   (original string changed)

No comments:

Post a Comment