#################### # Ruby Cheat Sheet # # by Tom Veatch # #################### '%' introduces a shell command, to do things like start/run ruby itself. % ruby -v # print version % ruby -e CODE # run CODE % ruby -i.bak -pe CODE *.[ch] # run CODE on .c, .h files, making bak backups. # Below here: the Ruby language itself, for CODE, ruby scripts or console # One line comment =begin multiline comment =end STATEMENT ; STATEMENT \n STATEMENT # semicolon & \n are equivalent load "file" # copy it in (e.g., at console, to run scripts therein) require "file" # load it only once, and only if needed. ### METHODS/FUNCTIONS ### definition syntax: (returns last evaluated line) def fn(args) .. end # a method can be used before defined, but must be defined before executed. # def main; calls ; end; def calls ; ... ; end; main; # makes sure " " " ### REGULAR EXPRESSIONS ### similar to perl. ### ARRAY ### + for concatenation, index with negative #'s counting from end. ### STRING ### is character array, use puts for output, *N to multiply puts "A" + ("BC" * 3), "#{5*3}" # ABCBCBC15\n # '..' literal string; ".." interpreted string. ### CONTROL STRUCTURES #### if S ; BLOCK ; else ; BLOCK ; end # no ()'s around test S. while S ; BLOCK ; end # next, break, redo go to bottom, top, out. case E [when [VAL,]*VAL BLOCK]* end # => switch(E){ case VAL: case VAL: BLOCK } for ELEMENT in COLLECTION BLOCK end # for loop. COLLECTION.each{|ELEMENT| BLOCK } # same as for loop STRING.each_byte{|c| BLOCK } STRING.each_line{|L| BLOCK } Iterable.map{|x| BLOCK} # run code on each item, return a list of results Iterable.map{&:methodname} # run named method on each.. def ITFUNC(ARG) ... yield ... end # call as ITeratorFUNCtion(..) { use ARG repeatedly } list=[a,1,"c"]<<"d" # list of diverse types. '<<' appends. list[rand(4)] # pick one out randomly. puts list.join(":") # concatenate with :'s. hasht = { k1=>v1, k2=>v2 }.delete k2 # build/cut a hashtable with keys/values j = STDIN.gets.chop.to_i # scanf("%i\n",j) s1.chop! # modify s1 in place. s2=s1.chop # leave s1 unmodified. b1=s2? # X? returns boolean T or F OBJECT.method(..) # dot notation for calling a method of an object. self.method a1, a2 # method call from within an object method(a1, a2) # same as self.method... # thus all functions are methods of the current class class Dog global, instance, or local var, or constant. trace_var :$x proc{puts "$x is now {#$x}"} # called upon change to global $x ${!_@&~=/\0*$?} # have special meanings like ARGV, PID, etc. @v = .. # add/set instance variable into current class lCaseInit=nil # local var within def proc loop class module or file, establish scope def box # this is a method not a class. contents=nil # establish scope as within def not proc get=proc{contents} # return local var's value set=proc{|n| contents=n} # store into local var return get,set # return two values end reader,writer = box # The two returned values get new names reader, writer reader.call => nil # contents not yet set writer.call(4) # contents set to 4 reader.call => 4 # contents available within the scope of the box method # Ruby also provides abbreviated syntax for object attribute readers/writers begin raise "E1" # got here? raise a named error. rescue # if an error was raised, come here. puts "Error: #{$!}" # Should print "Error: E1" ensure # With or without error, do this too at the end file.close # for example close a file. end #### This was written using internet sources during a small Ruby project. #### #### May it serve as useful. ####