diff options
Diffstat (limited to 'ruby.html.markdown')
| -rw-r--r-- | ruby.html.markdown | 50 | 
1 files changed, 42 insertions, 8 deletions
| diff --git a/ruby.html.markdown b/ruby.html.markdown index 998b4bf7..a1532855 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -14,6 +14,7 @@ contributors:    - ["Rahil Momin", "https://github.com/iamrahil"]    - ["Gabriel Halley", "https://github.com/ghalley"]    - ["Persa Zula", "http://persazula.com"] +  - ["Jake Faris", "https://github.com/farisj"]  ---  ```ruby @@ -41,7 +42,11 @@ You shouldn't either  35 / 5 #=> 7  2**5 #=> 32  5 % 3 #=> 2 -5 ^ 6 #=> 3 + +# Bitwise operators +3 & 5 #=> 1 +3 | 5 #=> 7 +3 ^ 5 #=> 6  # Arithmetic is just syntactic sugar  # for calling a method on an object @@ -49,7 +54,7 @@ You shouldn't either  10.* 5 #=> 50  # Special values are objects -nil # Nothing to see here +nil # equivalent to null in other languages  true # truth  false # falsehood @@ -77,6 +82,11 @@ false.class #=> FalseClass  2 <= 2 #=> true  2 >= 2 #=> true +# Combined comparison operator +1 <=> 10 #=> -1 +10 <=> 1 #=> 1 +1 <=> 1 #=> 0 +  # Logical operators  true && false #=> false  true || false #=> true @@ -122,7 +132,7 @@ puts "I'm printing!"  # print to the output without a newline  print "I'm printing!" -#=> I'm printing! => nill +#=> I'm printing! => nil  # Variables  x = 25 #=> 25 @@ -200,7 +210,7 @@ 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 Ruby's primary dictionary with key/value pairs.  # Hashes are denoted with curly braces:  hash = { 'color' => 'green', 'number' => 5 } @@ -220,8 +230,8 @@ 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 +new_hash.key?(:defcon) #=> true +new_hash.value?(3) #=> true  # Tip: Both Arrays and Hashes are Enumerable  # They share a lot of useful methods such as each, map, count, and more @@ -275,7 +285,7 @@ hash.each do |key, value|    puts "#{key} is #{value}"  end -# If you still need and index you can use "each_with_index" and define an index +# If you still need an index you can use "each_with_index" and define an index  # variable  array.each_with_index do |element, index|    puts "#{element} is number #{index} in the array" @@ -401,6 +411,28 @@ def guests(*array)    array.each { |guest| puts guest }  end +# If a method returns an array, you can use destructuring assignment +def foods +    ['pancake', 'sandwich', 'quesadilla'] +end +breakfast, lunch, dinner = foods +breakfast #=> 'pancake' +dinner #=> 'quesadilla' + +# By convention, all methods that return booleans end with a question mark +5.even? # false +5.odd? # true + +# And if a method ends with an exclamation mark, it does something destructive +# like mutate the receiver. Many methods have a ! version to make a change, and +# a non-! version to just return a new changed version +company_name = "Dunder Mifflin" +company_name.upcase #=> "DUNDER MIFFLIN" +company_name #=> "Dunder Mifflin" +company_name.upcase! # we're mutating company_name this time! +company_name #=> "DUNDER MIFFLIN" + +  # Define a class with the class keyword  class Human @@ -579,7 +611,9 @@ Something.new.qux # => 'qux'  ## Additional resources  - [Learn Ruby by Example with Challenges](http://www.learneroo.com/modules/61/nodes/338) - A variant of this reference with in-browser challenges. -- [Official Documentation](http://www.ruby-doc.org/core-2.1.1/) +- [An Interactive Tutorial for Ruby](https://rubymonk.com/) - Learn Ruby through a series of interactive tutorials. +- [Official Documentation](http://ruby-doc.org/core)  - [Ruby from other languages](https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/)  - [Programming Ruby](http://www.amazon.com/Programming-Ruby-1-9-2-0-Programmers/dp/1937785491/) - An older [free edition](http://ruby-doc.com/docs/ProgrammingRuby/) is available online.  - [Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide) - A community-driven Ruby coding style guide. +- [Try Ruby](http://tryruby.org) - Learn the basic of Ruby programming language, interactive in the browser. | 
