nil is false, anything else is true. false is false 0 is not false --- http://www.ruby-doc.org % ri ClassName (Ruby Doc) % ri method Everything is an object: number=Math.abs(number) // Java number = number.abs # Ruby Character indentation preferred with two spaces vs tabs def say_goodnight(name) result = "Good night, #{name.capitalize}" return result end $global - Global variables @instance - instance variable @@class - class variables * Local Variables, Method Parameters, and Method names must start with Uppercase * Constants are CONSTANTS - note that they hold a reference to an object * No semicolons at end of statement * Arrays a = [1, 'cat', 3.14] a[2] = nil; # Nil is Ruby's NULL a = %w{ ant bee cat dog elk } # array shortcut * Hashes inst_hash = { 'red' => "envelope", 'green' => "money", } inst_has['red'] ---> "envelope" * Control Structures (if) if count > 10 puts "Try again" elsif tries == 3 puts "You lose" else puts "Enter a number" end puts "Danger, dude" if aliens > 500 (No braces, end is terminator) * Control Structures (for) for ... in ... [do | : ] ... end * Control Structures (while) while line = gets puts line.downcase end square = 2 square = square*square while square < 1000 * Control Structures (case) case when ... when ... else ... end * Code Blocks and Callbacks { puts "Whatever" } do club.enroll(person) person.add end {} braces bind more tightly then do .. end * Iterators animals = %w (ant bee cat dog elk ) animals.each {|animal| puts animal } ['cat', 'dog', 'horse' ].each {|name| print name, " " } * Class class Song def initialize(name, artist, duration) @name = name @artist = artist @duration = duration end # end initialize end # end class * Builtin functions song.inspect (dump) song.to_s (serialize to string) ----------------------------------------------------------------------------------- Variables * All variables are references to an object. person1="Tim" person2=person1 person1[0] = 'K' person1 -> "Kim" person2 -> "Kim" * So you can prevent objects from being changed by freeze person1="Tim" person2=person1 person1.freeze person1[0] = 'K' person1 -> "Jim" person2 -> "Kim" ----------------------------------------------------------------------------------- Classes * Single Inheritance and Parents (note Mixins differentiator) class KaraokeSong < Song def initialize(name,artist,duration,lyrics) super (name, artist, duration) @lyrics = lyrics end # initialize def to_s super + " [#@lyrics]" end #to_s end # class * Class Methods are not tied to specific object - def ClassName.method_name class MyLogger private_class_method :new @@logger = nil def MyLogger.create @@logger = new unless @@logger @@logger end end * Public, Private, Protected class MyClass def method_public end protected def method_protected end private def method_private end end class MyClass def method1 end public: method1, :method4 protected: method5 private:method6 end * Scope :: OUTER_CONST = 99 class Const def get_const CONST end CONST=OUTER_CONST+1 end Const.new.get_const -> 100 Const::CONST -> 100 ::OUTER_CONST -> 99 * Yield class Creator def create_block puts "123" puts yield end end block = Creator.new.create_block {"original"} Outputs: 123 original ----------------------------------------------------------------------------------- Modules * Defines a namespace module Trig PI = xxx def Trig.sin(x) end end #module * Now you can really screw up stuff with mixins module DebugModule def what_the_fuck end end class OneClass include Debug end class SecondClass end one = OneClass.new() two = SecondClass.new() one.what_the_fuck() two.what_the_fuck()