diff options
Diffstat (limited to 'ruby.html.markdown')
-rw-r--r-- | ruby.html.markdown | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/ruby.html.markdown b/ruby.html.markdown index 7cf5bdc7..7bd28d86 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -11,6 +11,7 @@ contributors: - ["Ariel Krakowski", "http://www.learneroo.com"] - ["Dzianis Dashkevich", "https://github.com/dskecse"] - ["Levi Bostian", "https://github.com/levibostian"] + - ["Rahil Momin", "https://github.com/iamrahil"] --- @@ -60,8 +61,6 @@ false.class #=> FalseClass # Inequality 1 != 1 #=> false 2 != 1 #=> true -!true #=> false -!false #=> true # apart from false itself, nil is the only other 'falsey' value @@ -75,6 +74,21 @@ false.class #=> FalseClass 2 <= 2 #=> true 2 >= 2 #=> true +# Logical operators +true && false #=> false +true || false #=> true +!true #=> false + +# There are alternate versions of the logical operators with much lower +# precedence. These are meant to be used as flow-control constructs to chain +# statements together until one of them returns true or false. + +# `do_something_else` only called if `do_something` succeeds. +do_something() and do_something_else() +# `log_error` only called if `do_something` fails. +do_something() or log_error() + + # Strings are objects 'I am a string'.class #=> String @@ -159,6 +173,11 @@ array[1..3] #=> [2, 3, 4] # Add to an array like this array << 6 #=> [1, 2, 3, 4, 5, 6] +# Or like this +array.push(6) #=> [1, 2, 3, 4, 5, 6] + +# Check if an item exists in an array +array.include?(1) #=> true # Hashes are Ruby's primary dictionary with keys/value pairs. # Hashes are denoted with curly braces: @@ -179,6 +198,10 @@ new_hash = { defcon: 3, action: true } new_hash.keys #=> [:defcon, :action] +# Check existence of keys and values in hash +new_hash.has_key?(:defcon) #=> true +new_hash.has_value?(3) #=> true + # Tip: Both Arrays and Hashes are Enumerable # They share a lot of useful methods such as each, map, count, and more @@ -280,9 +303,9 @@ rescue NoMemoryError => exception_variable puts 'NoMemoryError was raised', exception_variable rescue RuntimeError => other_exception_variable puts 'RuntimeError was raised now' -else +else puts 'This runs if no exceptions were thrown at all' -ensure +ensure puts 'This code always runs no matter what' end |