From 354e98325fe94f4834d360086a67d0032b83fa20 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Sun, 24 Jan 2010 05:56:09 -0500 Subject: syntax fixes --- software/ruby.page | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'software/ruby.page') diff --git a/software/ruby.page b/software/ruby.page index c044a48..662d934 100644 --- a/software/ruby.page +++ b/software/ruby.page @@ -7,7 +7,7 @@ toc: no Ruby ================== -.. note:: This information is very rough, it's mostly my notes about what is +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. @@ -24,17 +24,17 @@ 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"] +:: + 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 + 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). @@ -45,28 +45,29 @@ 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 + !!(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 ->>> $~ # => # ->>> $~.pre_hash # => "There are " + re1 = /\d+/ + "There are 5 kilos of chunky bacon on the table!" =~ re1 # => 10, the index + $~ # => # + $~.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 + 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 + -- cgit v1.2.3