diff options
author | bryan newbold <bnewbold@snark.mit.edu> | 2009-01-22 23:04:17 -0500 |
---|---|---|
committer | bryan newbold <bnewbold@snark.mit.edu> | 2009-01-22 23:04:17 -0500 |
commit | 7218768aba3a43f1a62867f35f05cb85a30d7ed2 (patch) | |
tree | acfc09cc29bcb6c543a760ede6a399375a8c92ab /software/ruby | |
parent | 0f238f885ff64a94c2bfb2906b5f1e8edb93dc0c (diff) | |
parent | f18ba63a8b3d39bc9f2ca8dbe6d766d0dd5f77ec (diff) | |
download | knowledge-7218768aba3a43f1a62867f35f05cb85a30d7ed2.tar.gz knowledge-7218768aba3a43f1a62867f35f05cb85a30d7ed2.zip |
Merge branch 'master' of ssh://animus.robocracy.org/srv/git/knowledge
Diffstat (limited to 'software/ruby')
-rw-r--r-- | software/ruby | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/software/ruby b/software/ruby new file mode 100644 index 0000000..e64f73e --- /dev/null +++ b/software/ruby @@ -0,0 +1,67 @@ +================== +Ruby +================== + +.. note:: This information is very rough, it's mostly my notes about what is + different about Ruby syntax compared to similar modern interpreted + pan-paradigm languages like Python. + +A unique intro to ruby is `"Why's Poignant Guide to Ruby"`__, a web-comic-y +short free online book by why the luck stiff. The more serious reference is +the "pickax" book. + +__ http://poignantguide.net/ + +Blocks +--------- +Blocks of code can be passed to functions, making ruby code more of a first +order data type. + +Ranges +---------- + +>>> 2..7 # => 2..7 +>>> (2..7).to_a # => [2, 3, 4, 5, 6, 7] +>>> (2...7).to_a # => [2, 3, 4, 5, 6] +>>> ('e'..'h').to_a # => ["e", "f", "g", "h"] + +Control Structures +-------------------- +Can use ``if`` after a statement:: + +>>> a = c if c > b + +Along with the usual ``break`` and ``next``, there is ``redo`` which redoes +the current loop (initial conditions may have been changed). + + +Boolean Operators +-------------------- +Anything that is not ``nill`` or ``false`` is true. To force interpretation +as boolean, use ``!!`` (not not):: + +>>> !!(nil) # => false +>>> !!(true) # => true +>>> !!('') # => true +>>> !!(0) # => true +>>> !!({}) # => true + + +Misc +---------------- +Can use nasty Perl style regular expression stuff:: + +>>> re1 = /\d+/ +>>> "There are 5 kilos of chunky bacon on the table!" =~ re1 # => 10, the index +>>> $~ # => #<MatchData:0xb7c36754> +>>> $~.pre_hash # => "There are " + +Also $1, $2, etc. + +The "splat operator", '*', either collects or expands extra arguments depending +on syntax (I think this is kind of icky):: + +>>> a, b = 1, 2, 3, 4 # a=1, b=2 +>>> a, *b = 1, 2, 3, 4 # a=1, b=[2,3,4] +>>> c, d = 5, [6, 7, 8] # c=5, d=[6,7,8] +>>> c, d = 5, *[6, 7, 8] # c=5, b=6 |