|
|
RubyCheatSheetFrom $1Table of contents[MISSING]<UL>
</UL> <H2><A NAME="1">Language</A></H2> <H3><A NAME="2">General Syntax Rules</A></H3> <UL>
</UL> <H3><A NAME="3">Reserved words</A></H3> <PRE>alias and BEGIN begin break case class def defined do else elsif END end ensure false for if in module next nil not or redo rescue retry return self super then true undef unless until when while yield </PRE> <H3><A NAME="4">Types</A></H3> <P>Basic types are numbers, strings, ranges, regexen, symbols, arrays, and hashes. Also included are files because they are used so often.</P> <H4><A NAME="5">Numbers</A></H4> <PRE>123 1_234 123.45 1.2e-3 0xffff (hex) 0b01011 (binary) 0377 (octal) ?a ASCII character ?\C-a Control-a ?\M-a Meta-a ?\M-\C-a Meta-Control-a :symbol Integer corresponding to identifiers, variables, and operators.</PRE> <H4><A NAME="6">Strings</A></H4> <PRE>'no interpolation' "#{interpolation}, #{$interpolation}, #{@interpolation}, and backslashes\n" %q!no interpolation! %!interpolation and backslashes! %Q(interpolation and backslashes) <H5><A NAME="7">Backslashes</A></H5> <PRE>\t (tab), \n (newline), \r (carriage return), \f (form feed), \b (backspace), \a (bell), \e (escape), \s (whitespace), \nnn (octal), \xnn (hexadecimal), \cx (control x), \C-x (control x), \M-x (meta x), \M-\C-x (meta control x)</PRE> <H5><A NAME="8">Here Docs</A></H5> <PRE><<identifier, <<"identifier", <<-identifier</PRE> <H4><A NAME="9">Ranges</A></H4> <PRE>1..10 'a'..'z' (1..10) === 5 => true (1..10) === 15 => false while gets # prints lines starting at 'start' and ending at 'end'
end</PRE> <PRE>class RangeThingy
end range = RangeThingy.new(lower_bound)..RangeThingy.new(upper_bound)</PRE> <H4><A NAME="10">Regexen</A></H4> <PRE>/normal regex/i %r|alternate form|</PRE> <PRE>. any character except newline [ ] any single character of set [^ ] any single character NOT of set * 0 or more previous regular expression *? 0 or more previous regular expression(non greedy) + 1 or more previous regular expression +? 1 or more previous regular expression(non greedy) ? 0 or 1 previous regular expression | alternation ( ) grouping regular expressions ^ beginning of a line or string $ end of a line or string #{m,n} at least m but most n previous regular expression #{m,n}? at least m but most n previous regular expression(non greedy) \A beginning of a string \b backspace(0x08)(inside[]only) \B non-word boundary \b word boundary(outside[]only) \d digit, same as[0-9] \D non-digit \S non-whitespace character \s whitespace character[ \t\n\r\f] \W non-word character \w word character[0-9A-Za-z_] \z end of a string \Z end of a string, or before newline at the end (?# ) comment (?: ) grouping without backreferences (?= ) zero-width positive look-ahead assertion (?! ) zero-width negative look-ahead assertion (?ix-ix) turns on/off i/x options, localized in group if any. (?ix-ix: ) turns on/off i/x options, localized in non-capturing group.</PRE> <H4><A NAME="11">Arrays</A></H4> <PRE>[1, 2, 3] %w(foo bar baz)</PRE> <P>Indexes may be negative, and they index backwards (eg -1 is last element).</P> <H4><A NAME="12">Hashes</A></H4> <PRE>{1=>2, 2=>4, 3=>6} { expr => expr...}</PRE> <H4><A NAME="13">Files</A></H4> <P>Common methods include:</P> <UL>
</UL> <H5><A NAME="14">Mode Strings</A></H5> <DL>
</DL> <H3><A NAME="15">Variables</A></H3> <PRE>$global_variable @instance_variable [OtherClass::]CONSTANT local_variable</PRE> <H3><A NAME="16">Pseudo variables</A></H3> <PRE>self the receiver of the current method nil the sole instance of the Class NilClass(represents false) true the sole instance of the Class TrueClass(typical true value) false the sole instance of the Class FalseClass(represents false) FILE the current source file name. LINE the current line number in the source file.</PRE> <H3><A NAME="17">Pre-defined variables</A></H3> <PRE>$! The exception information message set by 'raise'. $@ Array of backtrace of the last exception thrown. $& The string matched by the last successful pattern match in this scope. $` The string to the left of the last successful match. $' The string to the right of the last successful match. $+ The last bracket matched by the last successful match. $1 The Nth group of the last successful match. May be > 1. $~ The information about the last match in the current scope. $= The flag for case insensitive, nil by default. $/ The input record separator, newline by default. $\ The output record separator for the print and IO#write. Default is nil. $, The output field separator for the print and Array#join. $; The default separator for String#split. $. The current input line number of the last file that was read. $< The virtual concatenation file of the files given on command line. $> The default output for print, printf. $stdout by default. $_ The last input line of string by gets or readline. $0 Contains the name of the script being executed. May be assignable. $* Command line arguments given for the script sans args. $$ The process number of the Ruby running this script. $? The status of the last executed child process. $: Load path for scripts and binary modules by load or require. $" The array contains the module names loaded by require. $DEBUG The status of the -d switch. $FILENAME Current input file from $<. Same as $<.filename. $LOAD_PATH The alias to the $:. $stderr The current standard error output. $stdin The current standard input. $stdout The current standard output. $VERBOSE The verbose flag, which is set by the -v switch. $-0 The alias to $/. $-a True if option -a is set. Read-only variable. $-d The alias to $DEBUG. $-F The alias to $;. $-i In in-place-edit mode, this variable holds the extention, otherwise nil. $-I The alias to $:. $-l True if option -l is set. Read-only variable. $-p True if option -p is set. Read-only variable. $-v The alias to $VERBOSE.</PRE> <H3><A NAME="18">Pre-defined global constants</A></H3> <PRE>TRUE The typical true value. FALSE The false itself. NIL The nil itself. STDIN The standard input. The default value for $stdin. STDOUT The standard output. The default value for $stdout. STDERR The standard error output. The default value for $stderr. ENV The hash contains current environment variables. ARGF The alias to the $<. ARGV The alias to the $*. DATA The file object of the script, pointing just after END. VERSION The Ruby version string. RUBY_RELEASE_DATE The relase date string. RUBY_PLATFORM The platform identifier.</PRE> <H3><A NAME="19">Expressions</A></H3> <H4><A NAME="20">Terms</A></H4> <P>Terms are expressions that may be a basic type (listed above), a shell command, variable reference, constant reference, or method invocation.</P> <H4><A NAME="21">Operators and Precedence</A></H4> <PRE>(Top to bottom) :: [] ** -(unary) +(unary) ! ~ * / % + - << >> & | ^ > >= < <= <=> == === != =~ !~ && || .. ... =(+=, -=...) not and or</PRE> <P>All of the above are just methods except these:</P> <PRE>=, .., ..., !, not, &&, and, ||, or, !=, !~</PRE> <P>In addition, assignment operators(+= etc.) are not user-definable.</P> <H4><A NAME="22">Control Expressions</A></H4> <PRE>if bool-expr [then]
elsif bool-expr [then]
else
end</PRE> <PRE>unless bool-expr [then]
else
end</PRE> <PRE>expr if bool-expr expr unless bool-expr</PRE> <PRE>case target-expr
[else
end</PRE> <P>(comparisons may be regexen)</P> <PRE>while bool-expr [do]
end</PRE> <PRE>until bool-expr [do]
end</PRE> <PRE>begin
end while bool-expr</PRE> <PRE>begin
end until bool-expr</PRE> <PRE>for name[, name]... in expr [do]
end</PRE> <PRE>expr.each do | name[, name]... |
end</PRE> <PRE>expr while bool-expr expr until bool-expr</PRE> <UL>
</UL> <H3><A NAME="23">Invoking a Method</A></H3> <P>Nearly everything available in a method invocation is optional, consequently the syntax is very difficult to follow. Here are some examples:</P> <UL>
</UL> <PRE>invocation := [receiver ('::' | '.')] name [ parameters ] [ block ] parameters := ( [param]* [, hashlist] [*array] [&aProc] ) block := { blockbody } | do blockbody end </PRE> <H3><A NAME="24">Defining a Class</A></H3> <P>Classnames begin w/ capital character.</P> <PRE>class Identifier [< superclass ]
end</PRE> <PRE># singleton classes, add methods to a single instance class << obj
end</PRE> <H3><A NAME="25">Defining a Module</A></H3> <PRE>module Identifier
end</PRE> <H3><A NAME="26">Defining a Method</A></H3> <PRE>def method_name(arg_list, *list_expr, &block_expr)
end</PRE> <PRE># singleton method def expr.identifier(arg_list, *list_expr, &block_expr)
end</PRE> <UL>
</UL> <H4><A NAME="27">Access Restriction</A></H4> <UL>
</UL> <UL>
</UL> <PRE>class A
end class B < A
end b = B.new.test_protected</PRE> <H4><A NAME="28">Accessors</A></H4> <P>Class Module provides the following utility methods:</P> <DL>
</DL> <H3><A NAME="29">Aliasing</A></H3> <PRE>alias <old> <new></PRE> <P>Creates a new reference to whatever old referred to. old can be any existing method, operator, global. It may not be a local, instance, constant, or class variable.</P> <H3><A NAME="30">Blocks, Closures, and Procs</A></H3> <H4><A NAME="31">Blocks/Closures</A></H4> <UL>
</UL> <PRE>invocation do ... end invocation { ... }</PRE> <UL>
</UL> <H4><A NAME="32">Proc Objects</A></H4> <P>Created via:</P> <UL>
</UL> <P>See class Proc for more information.</P> <H3><A NAME="33">Exceptions, Catch, and Throw</A></H3> <UL>
</UL> <PRE>begin
[rescue [error_type [=> var],..]
[else
[ensure
end</PRE> <P>The default error_type for resuce is StandardError, not Exception.</P> <H2><A NAME="34">Standard Library</A></H2> <P>Ruby comes with an extensive library of classes and modules. Some are built-in, and some are part of the standard library. You can distinguish the two by the fact that the built-in classes are in fact, built-in. There are no dot-rb files for them.</P> <H3><A NAME="35">Built-in Library</A></H3> <H4><A NAME="36">Class Hierarchy</A></H4> <UL>
</UL> <H4><A NAME="37">Modules</A></H4> <UL>
</UL> <H3><A NAME="38">Standard Library</A></H3> <UL>
</UL> <H4><A NAME="39">Classes</A></H4> <H4><A NAME="40">Modules</A></H4> <H4><A NAME="41">Mixins</A></H4> <H4><A NAME="42">Socket</A></H4> <P>BasicSocket IPSocket TCPSocket SOCKSSocket TCPServer UDPSocket UNIXSocket Socket</P> <H4><A NAME="43">Net:</A></H4> <P>FTP HTTP HTTPResponse POP APOP POPMail SMTP Telnet</P> <H4><A NAME="44">CGI:</A></H4> <P>CGI CGI::Session</P> <H4><A NAME="45">MS</A></H4> <P>WIN32OLE WIN32OLE_EVENT Win32API</P> <H2><A NAME="46">Tools</A></H2> <H3><A NAME="47">ruby</A></H3> <H4><A NAME="48">Command Line Options</A></H4> <PRE>-0[octal] specify record separator (\0, if no argument). -a autosplit mode with -n or -p (splits $_ into $F). -c check syntax only. -Cdirectory cd to directory, before executing your script. --copyright print the copyright and exit. -d set debugging flags (set $DEBUG to true). -e 'command' one line of script. Several -e's allowed. -F regexp split() pattern for autosplit (-a). -h prints summary of the options. -i[extension] edit ARGV files in place (make backup if extension supplied). -Idirectory specify $LOAD_PATH directory (may be used more than once). -Kkcode specifies KANJI (Japanese) code-set. -l enable line ending processing. -n assume 'while gets(); ... end' loop around your script. -p assume loop like -n but print line also like sed. -rlibrary require the library, before executing your script. -s enable some switch parsing for switches after script name. -S look for the script using PATH environment variable. -T[level] turn on tainting checks. -v print version number, then turn on verbose mode. --version print the version and exit. -w turn warnings on for your script. -x[directory] strip off text before #! line and perhaps cd to directory. -X directory causes Ruby to switch to the directory. -y turns on compiler debug mode.</PRE> <H4><A NAME="49">Environment Variables</A></H4> <PRE>DLN_LIBRARY_PATH Search path for dynamically loaded modules. RUBYLIB Additional search paths. RUBYLIB_PREFIX Add this prefix to each item in RUBYLIB. Windows only. RUBYOPT Additional command line options. RUBYPATH With -S, searches PATH, or this value for ruby programs. RUBYSHELL Shell to use when spawning.</PRE> <H3><A NAME="50">irb</A></H3> <PRE>irb [options] [script [args]]</PRE> <P>The options are:</P> <DL>
</DL> <P>Besides arbitrary ruby commands, the special commands are:</P> <DL>
</DL> <H3><A NAME="51">rtags</A></H3> <P>TODO: write content</P> <H3><A NAME="52">xmp</A></H3> <PRE>require "irb/xmp" xmp "something to eval" # or: x = XMP.new x.puts "something to eval"</PRE> <H3><A NAME="53">ruby-mode</A></H3> <P>TODO: I don't have a freakin clue how to use the inferior ruby thing... I always fire up a shell in emacs... DOH!</P> <H3><A NAME="54">Debugger</A></H3> <P>To invoke the debugger:</P> <PRE>ruby -r debug ...</PRE> Note: With 1.8.2 it appears you have to use: <PRE>ruby -rubygems -r debug ...</PRE> <P>To use the debugger:</P> <DL>
</DL> <H3><A NAME="55">old: Embedded Documentation</A></H3> <PRE>=begin the everything between a line beginning with `=begin' and that with `=end' will be skipped by the interpreter. =end</PRE> <P>FIX: there is a lot more to rdtool / rdoc.</P> <P>FIX: rdtool is deprecated for rdoc.</P> <H2><A NAME="56">Mindshare, Idiom and Patterns</A></H2> <H3><A NAME="57">Object Design</A></H3> <UL>
</UL> <H3><A NAME="58">Other Third-party Libraries</A></H3> <H4><A NAME="59">Amstd</A></H4> <UL>
</UL> <H4><A NAME="60">Racc</A></H4> <UL>
</UL> <H4><A NAME="61">Optparse</A></H4> <UL>
</UL> <H4><A NAME="62">Test::Unit</A></H4> <UL>
</UL> Kevin's notes: <UL> <li>You find executable gem/rubygem code in /usr/local/lib/ruby/gems/1.8/gems/[gem-name]/lib/[code-file].rb <li>You also find gem/rubygem code in /usr/lib/ruby/gems/1.8/gems/[gem-name]/lib/[code-file].rb, but this code does not appear to get executed at runtime. </UL>
Tags:
none |