Commits

Steven! Ragnarök committed 1f23696

A draft of another literate-style article.

  • Participants
  • Parent commits f7e8410

Comments (0)

Files changed (1)

File lightnight_ruby.rb

+# Using What Ruby Gives You
+
+# ## If-Else Expression
+if the_price.right?
+  @guess = :accurate
+else
+  @guess = :inaccurate
+end
+
+# Why are you defining `@guess` twice?
+@guess = if the_price.right? then :accurate else :inaccurate end
+
+# Yeah, sure you could use the ternary operator
+@guess = the_price.right? ? :accurate : :inaccurate
+# But nothing is gained.
+# How do you even read that?
+
+# ## Case-When Expressions
+# The Procedural Way
+conditional_variable = :some_default_value
+case our_condition
+when :first
+  conditional_variable = :one_thing
+when :then
+  conditional_variable = :another
+end
+
+# A Better Way
+conditional_variable = case our_condition
+                       when :first
+                         :one_thing
+                       when :then
+                         :another
+                       else
+                         :some_default_value
+                       end
+
+# ## Equivalence and Case Equivalence
+# `(== vs. ===)`
+# Case equivalence is a little weird, it's best explained by _why.
+
+# > The double equals gives the appearance of a short link of ropes, right
+# > along the sides of a red carpet where only `true` can be admitted.
+if approaching_guy == true
+  puts "That necklace is classic"
+end
+# > The __triple equals__ is a length of velvet rope, checking values much like
+# > the __double equals__. It's just: the triple equals is a longer rope and it
+# > sags a bit in the middle. It's not as strict, it's a bit more flexible.
+# > Take the Ranges above. `(1895..1913)` isn't at all equal to `1905`.
+case year
+when 1894
+  "Born."
+when (1895..1913)
+  "Childhood in Lousville, Winston Co., Mississippi."
+else
+  "No information about this year."
+end
+
+# - Object#tap
+# Who has seen `returning some_object do |some_object| ... end`?
+
+# Here's the object oriented version of that, built into Ruby 1.9
+[].tap do |new_kittens|
+  end
+end
+# - Super-Do
+# - Enumerable#inject is cool but remember #any?, #all?
+# - wordy and/or vs &&/||