From 8c89f3f6083c93fa7ca04148a0e7aa07ff0d4545 Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Mon, 29 Jul 2013 16:30:51 +0800 Subject: Explained syntactic sugar is really just method calls. Objects in ruby receive a message via the . (dot) notation. The arithmetic operators are just syntactic sugar of the . message notation. --- ruby.html.markdown | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 38d060a3..7729e0ff 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -312,4 +312,22 @@ dwight.name #=> "Dwight K. Schrute" # Call the class method Human.say("Hi") #=> "Hi" + + +=begin +Arithmetic is just syntactic sugar +for calling a method on an object: +=end +1.+ 1 #=> 2 +1.+(1) #=> 2 + +8.- 1 #=> 7 +8.-(1) #=> 7 + +10.* 2 #=> 20 +10.*(2) #=> 20 + +35./ 5 #=> 7 +35./(5) #=> 7 + ``` -- cgit v1.2.3 From 99e1c31b19547fbe117a77047bb23eff0edafaea Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Tue, 30 Jul 2013 15:26:51 +0800 Subject: Updated location, and reduced examples also added array [] access example. --- ruby.html.markdown | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 7729e0ff..9a59be90 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -4,6 +4,7 @@ filename: learnruby.rb contributors: - ["David Underwood", "http://theflyingdeveloper.com"] - ["Joel Walden", "http://joelwalden.net"] + - ["Luke Holder", "http://twitter.com/lukeholder"] --- ```ruby @@ -30,6 +31,11 @@ You shouldn't either 10 * 2 #=> 20 35 / 5 #=> 7 +## Arithmetic is just syntactic sugar +## for calling a method on an object +1.+(3) #=> 4 +10.* 5 #=> 50 + # Special values are objects nil # Nothing to see here true # truth @@ -121,6 +127,12 @@ array = [1, "hello", false] #=> => [1, "hello", false] array[0] #=> 1 array[12] #=> nil +# Like arithmetic, [var] access +# is just syntactic sugar +# for calling a method [] on an object +array.[] 0 #=> 1 +array.[] 12 #=> nil + # From the end array[-1] #=> 5 @@ -313,21 +325,4 @@ dwight.name #=> "Dwight K. Schrute" # Call the class method Human.say("Hi") #=> "Hi" - -=begin -Arithmetic is just syntactic sugar -for calling a method on an object: -=end -1.+ 1 #=> 2 -1.+(1) #=> 2 - -8.- 1 #=> 7 -8.-(1) #=> 7 - -10.* 2 #=> 20 -10.*(2) #=> 20 - -35./ 5 #=> 7 -35./(5) #=> 7 - ``` -- cgit v1.2.3 From df90e4957246c422c00b07f788d825d327304d11 Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Tue, 30 Jul 2013 15:28:40 +0800 Subject: Fixed comment style --- ruby.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 9a59be90..a3bcbbd5 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -31,8 +31,8 @@ You shouldn't either 10 * 2 #=> 20 35 / 5 #=> 7 -## Arithmetic is just syntactic sugar -## for calling a method on an object +# Arithmetic is just syntactic sugar +# for calling a method on an object 1.+(3) #=> 4 10.* 5 #=> 50 -- cgit v1.2.3 From 80c71ef1c849d4294e13c3dbfce6fad66a79e99a Mon Sep 17 00:00:00 2001 From: greybird Date: Wed, 7 Aug 2013 11:20:20 +0400 Subject: Ruby. Difference between class instance variables and class variables --- ruby.html.markdown | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index a3bcbbd5..99817982 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -325,4 +325,50 @@ dwight.name #=> "Dwight K. Schrute" # Call the class method Human.say("Hi") #=> "Hi" +# Class also is object in ruby. So class can have instance variables. +# Class variable is shared among the class and all of its descendants. + +# base class +class Human + @@foo = 0 + + def self.foo + @@foo + end + + def self.foo=(value) + @@foo = value + end +end + +# derived class +class Worker < Human +end + +Human.foo # 0 +Worker.foo # 0 + +Human.foo = 2 # 2 +Worker.foo # 2 + +# Class instance variable is not shared by the class's descendants. + +class Human + @bar = 0 + + def self.bar + @bar + end + + def self.bar=(value) + @bar = value + end +end + +class Doctor < Human +end + +Human.bar # 0 +Doctor.bar # nil + ``` -- cgit v1.2.3 From 97a99b0ad55894631bcca03e086a2f23033c7c4b Mon Sep 17 00:00:00 2001 From: m5o Date: Thu, 8 Aug 2013 12:17:10 +0200 Subject: fix indentation by 2 spaces --- ruby.html.markdown | 60 +++++++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 99817982..861a94ad 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -275,36 +275,36 @@ surround { puts 'hello world' } # Define a class with the class keyword class Human - # A class variable. It is shared by all instances of this class. - @@species = "H. sapiens" - - # Basic initializer - def initialize(name, age=0) - # Assign the argument to the "name" instance variable for the instance - @name = name - # If no age given, we will fall back to the default in the arguments list. - @age = age - end - - # Basic setter method - def name=(name) - @name = name - end - - # Basic getter method - def name - @name - end - - # A class method uses self to distinguish from instance methods. - # It can only be called on the class, not an instance. - def self.say(msg) - puts "#{msg}" - end - - def species - @@species - end + # A class variable. It is shared by all instances of this class. + @@species = "H. sapiens" + + # Basic initializer + def initialize(name, age=0) + # Assign the argument to the "name" instance variable for the instance + @name = name + # If no age given, we will fall back to the default in the arguments list. + @age = age + end + + # Basic setter method + def name=(name) + @name = name + end + + # Basic getter method + def name + @name + end + + # A class method uses self to distinguish from instance methods. + # It can only be called on the class, not an instance. + def self.say(msg) + puts "#{msg}" + end + + def species + @@species + end end -- cgit v1.2.3 From 5424b31848e85e40ea78afb4582e8f33845e1b01 Mon Sep 17 00:00:00 2001 From: Tristan Hume Date: Mon, 12 Aug 2013 14:53:00 -0400 Subject: Explain blocks better --- ruby.html.markdown | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 861a94ad..52321ff6 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -5,6 +5,7 @@ contributors: - ["David Underwood", "http://theflyingdeveloper.com"] - ["Joel Walden", "http://joelwalden.net"] - ["Luke Holder", "http://twitter.com/lukeholder"] + - ["Tristan Hume", "http://thume.ca/"] --- ```ruby @@ -158,11 +159,6 @@ hash['number'] #=> 5 # Asking a hash for a key that doesn't exist returns nil: hash['nothing here'] #=> nil -# Iterate over hashes with the #each method: -hash.each do |k, v| - puts "#{k} is #{v}" -end - # Since Ruby 1.9, there's a special syntax when using symbols as keys: new_hash = { defcon: 3, action: true} @@ -193,7 +189,12 @@ end # HOWEVER # No-one uses for loops -# Use `each` instead, like this: +# Under the hood for loops use the each method which takes a "block". +# A block is a bunch of code that you can pass to a method like each. +# It is analogous to lambdas, anonymous functions or closures in other programming languages. + +# The each method runs the block multiple times passing a counter. +# You can iterate over a range like this: (1..5).each do |counter| puts "iteration #{counter}" @@ -204,6 +205,17 @@ end #=> iteration 4 #=> iteration 5 +# You can also surround blocks in curly brackets: +(1..5).each {|counter| puts "iteration #{counter}"} + +# You can also iterate over the contents of data structures using each. +array.each do |element| + puts "#{element} is part of the array" +end +hash.each do |key, value| + puts "#{key} is #{value}" +end + counter = 1 while counter <= 5 do puts "iteration #{counter}" -- cgit v1.2.3 From 5c5c8d3c4a0aea7c1c7a8150f428e354d0a12a6c Mon Sep 17 00:00:00 2001 From: Tristan Hume Date: Mon, 12 Aug 2013 15:05:00 -0400 Subject: Tidy up wording --- ruby.html.markdown | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 52321ff6..68c5b524 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -187,14 +187,14 @@ end #=> iteration 4 #=> iteration 5 -# HOWEVER -# No-one uses for loops -# Under the hood for loops use the each method which takes a "block". -# A block is a bunch of code that you can pass to a method like each. +# HOWEVER, No-one uses for loops. +# Instead you should use the "each" method and pass it a block. +# A block is a bunch of code that you can pass to a method like "each". # It is analogous to lambdas, anonymous functions or closures in other programming languages. - -# The each method runs the block multiple times passing a counter. -# You can iterate over a range like this: +# +# The "each" method of a range runs the block once for each element of the range. +# The block is passed a counter as a parameter. +# Calling the "each" method with a block looks like this: (1..5).each do |counter| puts "iteration #{counter}" @@ -208,7 +208,7 @@ end # You can also surround blocks in curly brackets: (1..5).each {|counter| puts "iteration #{counter}"} -# You can also iterate over the contents of data structures using each. +# The contents of data structures can also be iterated using each. array.each do |element| puts "#{element} is part of the array" end -- cgit v1.2.3 From ea853dfa674a1be2c6d5e54b1307bf80693cf01d Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Tue, 13 Aug 2013 17:42:03 -0500 Subject: Fixes array typos --- ruby.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 68c5b524..46bfbb7c 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -117,11 +117,11 @@ status == :approved #=> false # Arrays # This is an array -[1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] +array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] # Arrays can contain different types of items -array = [1, "hello", false] #=> => [1, "hello", false] +[1, "hello", false] #=> [1, "hello", false] # Arrays can be indexed # From the front -- cgit v1.2.3 From 7af884a9407c0b79aaede1edd3539e13a55552c8 Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Tue, 13 Aug 2013 17:46:33 -0500 Subject: Fixes spacing issues --- ruby.html.markdown | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 46bfbb7c..963d1fc1 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -173,9 +173,9 @@ new_hash.keys #=> [:defcon, :action] if true "if statement" elsif false - "else if, optional" + "else if, optional" else - "else, also optional" + "else, also optional" end for counter in 1..5 @@ -190,7 +190,8 @@ end # HOWEVER, No-one uses for loops. # Instead you should use the "each" method and pass it a block. # A block is a bunch of code that you can pass to a method like "each". -# It is analogous to lambdas, anonymous functions or closures in other programming languages. +# It is analogous to lambdas, anonymous functions or closures in other +# programming languages. # # The "each" method of a range runs the block once for each element of the range. # The block is passed a counter as a parameter. -- cgit v1.2.3 From 9fd70ffbb12e92a4bd129cfbe28d91e07138c398 Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Tue, 13 Aug 2013 17:47:52 -0500 Subject: Giving myself some credit... I guess --- ruby.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 963d1fc1..19f2ec86 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -6,6 +6,7 @@ contributors: - ["Joel Walden", "http://joelwalden.net"] - ["Luke Holder", "http://twitter.com/lukeholder"] - ["Tristan Hume", "http://thume.ca/"] + - ["Nick LaMuro", "https://github.com/NickLaMuro"] --- ```ruby -- cgit v1.2.3 From 6062618d358839e22f4bbe6348658253ad2e0209 Mon Sep 17 00:00:00 2001 From: Geoffrey Roguelon Date: Wed, 14 Aug 2013 19:11:13 +0200 Subject: Fix a missing character in the comment. remove useless whitespace at the end of the lines. --- ruby.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 19f2ec86..3a233d98 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -36,7 +36,7 @@ You shouldn't either # Arithmetic is just syntactic sugar # for calling a method on an object 1.+(3) #=> 4 -10.* 5 #=> 50 +10.* 5 #=> 50 # Special values are objects nil # Nothing to see here @@ -242,7 +242,7 @@ when 'D' puts "Scraping through" when 'F' puts "You failed!" -else +else puts "Alternative grading system, eh?" end @@ -252,7 +252,7 @@ def double(x) x * 2 end -# Functions (and all blocks) implcitly return the value of the last statement +# Functions (and all blocks) implicitly return the value of the last statement double(2) #=> 4 # Parentheses are optional where the result is unambiguous -- cgit v1.2.3 From e13b5111d569903c6620d448f0703707ed1dc8e8 Mon Sep 17 00:00:00 2001 From: Marcos Brizeno Date: Fri, 30 Aug 2013 21:58:23 -0300 Subject: Adding explanation about variables scope and naming --- ruby.html.markdown | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 3a233d98..80682682 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -7,6 +7,7 @@ contributors: - ["Luke Holder", "http://twitter.com/lukeholder"] - ["Tristan Hume", "http://thume.ca/"] - ["Nick LaMuro", "https://github.com/NickLaMuro"] + - ["Marcos Brizeno", "http://www.about.me/marcosbrizeno"] --- ```ruby @@ -339,6 +340,23 @@ dwight.name #=> "Dwight K. Schrute" # Call the class method Human.say("Hi") #=> "Hi" +# Variable's scopes are defined by the way we name them. +# Variables that start with $ have global scope +$var = "I'm a global var" +defined? $var #=> "global-variable" + +# Variables that start with @ have instance scope +@var = "I'm an instance var" +defined? @var #=> "instance-variable" + +# Variables that start with @@ have class scope +@@var = "I'm a class var" +defined? @@var #=> "class variable" + +# Variables that start with a capital letter are constants +Var = "I'm a constant" +defined? Var #=> "constant" + # Class also is object in ruby. So class can have instance variables. # Class variable is shared among the class and all of its descendants. -- cgit v1.2.3 From 9cc1982c484e99adfb922733cb9c0fb5768b6a77 Mon Sep 17 00:00:00 2001 From: Matthew Johnston Date: Sun, 8 Sep 2013 11:46:08 -0500 Subject: Added module extension and inclusion examples Also added examples for callbacks made when modules are included and extended. --- ruby.html.markdown | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 80682682..b9ba83cb 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -403,4 +403,55 @@ end Human.bar # 0 Doctor.bar # nil +module ModuleExample + def foo + 'foo' + end +end + +# Including modules binds the methods to the object instance +# Extending modules binds the methods to the class instance + +class Person + include ModuleExample +end + +class Book + extend ModuleExample +end + +Person.foo # => NoMethodError: undefined method `foo' for Person:Class +Person.new.foo # => 'foo' +Book.foo # => 'foo' +Book.new.foo # => NoMethodError: undefined method `foo' + +# Callbacks when including and extending a module are executed + +module ConcernExample + def self.included(base) + base.extend(ClassMethods) + base.send(:include, InstanceMethods) + end + + module ClassMethods + def bar + 'bar' + end + end + + module InstanceMethods + def qux + 'qux' + end + end +end + +class Something + include ConcernExample +end + +Something.bar # => 'bar' +Something.qux # => NoMethodError: undefined method `qux' +Something.new.bar # => NoMethodError: undefined method `bar' +Something.new.qux # => 'qux' ``` -- cgit v1.2.3 From 239595fc5929806b2f8c4d476d9bb383f8b0418e Mon Sep 17 00:00:00 2001 From: Marcin Wawrzyniak Date: Sun, 29 Sep 2013 18:15:16 +0100 Subject: ADD: "&" and "*" use cases in function parameters --- ruby.html.markdown | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index b9ba83cb..8723e18f 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -287,6 +287,18 @@ surround { puts 'hello world' } # } +# You can pass a block to a function +# "&" marks a reference to a passed block +def guests(&block) + block.call "some_argument" +end + +# You can pass a list of arguments, which will be converted into an array +# That's what splat operator ("*") is for +def guests(*array) + array.each { |guest| puts "#{guest}" } +end + # Define a class with the class keyword class Human -- cgit v1.2.3 From 9a60d0a6e4fa2aba9d1a1cf2a834ae82b2a18aea Mon Sep 17 00:00:00 2001 From: Jake Romer Date: Wed, 20 Nov 2013 05:38:46 -0500 Subject: Corrects Array#[,] entry The #[i,l] method returns a subarray starting at index i and continuing for length l. [source](http://ruby-doc.org/core-1.9.3/Array.html#method-i-5B-5D) --- ruby.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 8723e18f..bf4cb229 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -139,8 +139,8 @@ array.[] 12 #=> nil # From the end array[-1] #=> 5 -# With a start and end index -array[2, 4] #=> [3, 4, 5] +# With a start index and length +array[2, 3] #=> [3, 4, 5] # Or with a range array[1..3] #=> [2, 3, 4] -- cgit v1.2.3 From 02044c1eab7d7d8a670c1f63b004e91df83d22c4 Mon Sep 17 00:00:00 2001 From: David Underwood Date: Sun, 1 Dec 2013 21:42:51 -0500 Subject: Ruby: Adds attr_accessor, attr_reader, and attr_writer to class docs --- ruby.html.markdown | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index bf4cb229..a4c74a4f 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -323,6 +323,13 @@ class Human @name end + # The above functionality can be encapsulated using the attr_accessor method as follows + attr_accessor :name + + # Getter/setter methods can also be created individually like this + attr_reader :name + attr_writer :name + # A class method uses self to distinguish from instance methods. # It can only be called on the class, not an instance. def self.say(msg) -- cgit v1.2.3 From d25c10969d2f21c2dde1c89b491f4a7ef15476c4 Mon Sep 17 00:00:00 2001 From: Ariel Date: Mon, 10 Mar 2014 17:05:49 -0400 Subject: Added some example code and resources. --- ruby.html.markdown | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index a4c74a4f..0a28a000 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -8,6 +8,8 @@ contributors: - ["Tristan Hume", "http://thume.ca/"] - ["Nick LaMuro", "https://github.com/NickLaMuro"] - ["Marcos Brizeno", "http://www.about.me/marcosbrizeno"] + - ["Ariel Krakowski", "http://www.learneroo.com"] + --- ```ruby @@ -33,6 +35,7 @@ You shouldn't either 8 - 1 #=> 7 10 * 2 #=> 20 35 / 5 #=> 7 +2 ** 5 #=> 32 # Arithmetic is just syntactic sugar # for calling a method on an object @@ -79,6 +82,10 @@ placeholder = "use string interpolation" "I can #{placeholder} when using double quoted strings" #=> "I can use string interpolation when using double quoted strings" +#Combine strings but not with numbers +"hello " + "world" #=> "hello world" +"hello " + 3 #=> TypeError: can't convert Fixnum into String +"hello " +3.to_s #=> "hello 3" # print to the output puts "I'm printing!" @@ -247,6 +254,22 @@ else puts "Alternative grading system, eh?" end +#=> "Better luck next time" + +# cases can also use ranges +grade = 82 +case grade + when 90..100 + puts "Hooray!" + when 80...90 + puts "OK job" + else + puts "You failed!" +end + +#=> "OK job" + + # Functions def double(x) @@ -474,3 +497,12 @@ Something.qux # => NoMethodError: undefined method `qux' Something.new.bar # => NoMethodError: undefined method `bar' 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/) +- [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 addition](http://ruby-doc.com/docs/ProgrammingRuby/) is available online. + + -- cgit v1.2.3 From 933cf6f10d3e894edef4efe1c0d2a4e7aafd3192 Mon Sep 17 00:00:00 2001 From: Ariel Date: Mon, 10 Mar 2014 18:06:44 -0400 Subject: fixed space --- ruby.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 0a28a000..962853a2 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -82,10 +82,10 @@ placeholder = "use string interpolation" "I can #{placeholder} when using double quoted strings" #=> "I can use string interpolation when using double quoted strings" -#Combine strings but not with numbers +# Combine strings, but not with numbers "hello " + "world" #=> "hello world" "hello " + 3 #=> TypeError: can't convert Fixnum into String -"hello " +3.to_s #=> "hello 3" +"hello " + 3.to_s #=> "hello 3" # print to the output puts "I'm printing!" -- cgit v1.2.3 From b249363a998d08aff750d2722370e648c59cf70f Mon Sep 17 00:00:00 2001 From: Dzianis Dashkevich Date: Tue, 19 Aug 2014 12:06:52 +0300 Subject: Make style fixes to conform to ruby style guide Made style fixes to conform to Ruby style guide. Added a reference to a community-driven Ruby coding style guide. --- ruby.html.markdown | 102 ++++++++++++++++++++++++++--------------------------- 1 file changed, 51 insertions(+), 51 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 962853a2..3c67de2e 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -9,6 +9,7 @@ contributors: - ["Nick LaMuro", "https://github.com/NickLaMuro"] - ["Marcos Brizeno", "http://www.about.me/marcosbrizeno"] - ["Ariel Krakowski", "http://www.learneroo.com"] + - ["Dzianis Dashkevich", "https://github.com/dskecse"] --- @@ -35,7 +36,7 @@ You shouldn't either 8 - 1 #=> 7 10 * 2 #=> 20 35 / 5 #=> 7 -2 ** 5 #=> 32 +2**5 #=> 32 # Arithmetic is just syntactic sugar # for calling a method on an object @@ -78,14 +79,17 @@ false.class #=> FalseClass 'I am a string'.class #=> String "I am a string too".class #=> String -placeholder = "use string interpolation" +placeholder = 'use string interpolation' "I can #{placeholder} when using double quoted strings" #=> "I can use string interpolation when using double quoted strings" +# Prefer single quoted strings to double quoted ones where possible +# Double quoted strings perform additional inner calculations + # Combine strings, but not with numbers -"hello " + "world" #=> "hello world" -"hello " + 3 #=> TypeError: can't convert Fixnum into String -"hello " + 3.to_s #=> "hello 3" +'hello ' + 'world' #=> "hello world" +'hello ' + 3 #=> TypeError: can't convert Fixnum into String +'hello ' + 3.to_s #=> "hello 3" # print to the output puts "I'm printing!" @@ -130,7 +134,7 @@ array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] # Arrays can contain different types of items -[1, "hello", false] #=> [1, "hello", false] +[1, 'hello', false] #=> [1, "hello", false] # Arrays can be indexed # From the front @@ -157,7 +161,7 @@ array << 6 #=> [1, 2, 3, 4, 5, 6] # Hashes are Ruby's primary dictionary with keys/value pairs. # Hashes are denoted with curly braces: -hash = {'color' => 'green', 'number' => 5} +hash = { 'color' => 'green', 'number' => 5 } hash.keys #=> ['color', 'number'] @@ -170,7 +174,7 @@ hash['nothing here'] #=> nil # Since Ruby 1.9, there's a special syntax when using symbols as keys: -new_hash = { defcon: 3, action: true} +new_hash = { defcon: 3, action: true } new_hash.keys #=> [:defcon, :action] @@ -180,11 +184,11 @@ new_hash.keys #=> [:defcon, :action] # Control structures if true - "if statement" + 'if statement' elsif false - "else if, optional" + 'else if, optional' else - "else, also optional" + 'else, also optional' end for counter in 1..5 @@ -216,7 +220,7 @@ end #=> iteration 5 # You can also surround blocks in curly brackets: -(1..5).each {|counter| puts "iteration #{counter}"} +(1..5).each { |counter| puts "iteration #{counter}" } # The contents of data structures can also be iterated using each. array.each do |element| @@ -241,32 +245,30 @@ grade = 'B' case grade when 'A' - puts "Way to go kiddo" + puts 'Way to go kiddo' when 'B' - puts "Better luck next time" + puts 'Better luck next time' when 'C' - puts "You can do better" + puts 'You can do better' when 'D' - puts "Scraping through" + puts 'Scraping through' when 'F' - puts "You failed!" + puts 'You failed!' else - puts "Alternative grading system, eh?" + puts 'Alternative grading system, eh?' end - #=> "Better luck next time" # cases can also use ranges grade = 82 case grade - when 90..100 - puts "Hooray!" - when 80...90 - puts "OK job" - else - puts "You failed!" +when 90..100 + puts 'Hooray!' +when 80...90 + puts 'OK job' +else + puts 'You failed!' end - #=> "OK job" @@ -284,23 +286,23 @@ double 3 #=> 6 double double 3 #=> 12 -def sum(x,y) +def sum(x, y) x + y end # Method arguments are separated by a comma sum 3, 4 #=> 7 -sum sum(3,4), 5 #=> 12 +sum sum(3, 4), 5 #=> 12 # yield # All methods have an implicit, optional block parameter # it can be called with the 'yield' keyword def surround - puts "{" + puts '{' yield - puts "}" + puts '}' end surround { puts 'hello world' } @@ -311,25 +313,25 @@ surround { puts 'hello world' } # You can pass a block to a function -# "&" marks a reference to a passed block +# "&" marks a reference to a passed block def guests(&block) - block.call "some_argument" + block.call 'some_argument' end - + # You can pass a list of arguments, which will be converted into an array -# That's what splat operator ("*") is for +# That's what splat operator ("*") is for def guests(*array) - array.each { |guest| puts "#{guest}" } + array.each { |guest| puts guest } end # Define a class with the class keyword class Human # A class variable. It is shared by all instances of this class. - @@species = "H. sapiens" + @@species = 'H. sapiens' # Basic initializer - def initialize(name, age=0) + def initialize(name, age = 0) # Assign the argument to the "name" instance variable for the instance @name = name # If no age given, we will fall back to the default in the arguments list. @@ -356,20 +358,19 @@ class Human # A class method uses self to distinguish from instance methods. # It can only be called on the class, not an instance. def self.say(msg) - puts "#{msg}" + puts msg end def species @@species end - end # Instantiate a class -jim = Human.new("Jim Halpert") +jim = Human.new('Jim Halpert') -dwight = Human.new("Dwight K. Schrute") +dwight = Human.new('Dwight K. Schrute') # Let's call a couple of methods jim.species #=> "H. sapiens" @@ -380,7 +381,7 @@ dwight.species #=> "H. sapiens" dwight.name #=> "Dwight K. Schrute" # Call the class method -Human.say("Hi") #=> "Hi" +Human.say('Hi') #=> "Hi" # Variable's scopes are defined by the way we name them. # Variables that start with $ have global scope @@ -399,7 +400,7 @@ defined? @@var #=> "class variable" Var = "I'm a constant" defined? Var #=> "constant" -# Class also is object in ruby. So class can have instance variables. +# Class is also an object in ruby. So class can have instance variables. # Class variable is shared among the class and all of its descendants. # base class @@ -415,7 +416,7 @@ class Human end end -# derived class +# derived class class Worker < Human end @@ -451,8 +452,8 @@ module ModuleExample end end -# Including modules binds the methods to the object instance -# Extending modules binds the methods to the class instance +# Including modules binds their methods to the class instances +# Extending modules binds their methods to the class itself class Person include ModuleExample @@ -467,7 +468,7 @@ Person.new.foo # => 'foo' Book.foo # => 'foo' Book.new.foo # => NoMethodError: undefined method `foo' -# Callbacks when including and extending a module are executed +# Callbacks are executed when including and extending a module module ConcernExample def self.included(base) @@ -500,9 +501,8 @@ 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. +- [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/) - [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 addition](http://ruby-doc.com/docs/ProgrammingRuby/) is available online. - - +- [Programming Ruby](http://www.amazon.com/Programming-Ruby-1-9-2-0-Programmers/dp/1937785491/) - An older [free addition](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. -- cgit v1.2.3 From 9ddc2d0a5b92974ac1614024b4586354a395482e Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Mon, 10 Nov 2014 19:45:17 -0600 Subject: Add Ruby exceptions. --- ruby.html.markdown | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 3c67de2e..a1a2c77b 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -10,6 +10,7 @@ contributors: - ["Marcos Brizeno", "http://www.about.me/marcosbrizeno"] - ["Ariel Krakowski", "http://www.learneroo.com"] - ["Dzianis Dashkevich", "https://github.com/dskecse"] + - ["Levi Bostian", "https://github.com/levibostian"] --- @@ -271,6 +272,19 @@ else end #=> "OK job" +# exception handling +begin + # code here that might raise an exception + raise NoMemoryError, 'You ran out of memory.' +rescue NoMemoryError => exception_variable + puts 'NoMemoryError was raised', exception_variable +rescue RuntimeError => other_exception_variable + puts 'RuntimeError was raised now' +else + puts 'This runs if no exceptions were thrown at all' +ensure + puts 'This code always runs no matter what' +end # Functions -- cgit v1.2.3 From 1c4cbd279e740f4782c62e75d1f4659447dc464a Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Tue, 11 Nov 2014 13:06:57 -0600 Subject: Add colon to introduce the exception handling section --- ruby.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index a1a2c77b..e58c513d 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -272,7 +272,7 @@ else end #=> "OK job" -# exception handling +# exception handling: begin # code here that might raise an exception raise NoMemoryError, 'You ran out of memory.' -- cgit v1.2.3 From f25d54b06bd8ae17cf8e9fc9d966fc728cb593e0 Mon Sep 17 00:00:00 2001 From: Kevin Busby Date: Thu, 20 Nov 2014 16:26:56 +0000 Subject: Typo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Typo: “addition” corrected to “edition”. --- ruby.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index e58c513d..7cf5bdc7 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -518,5 +518,5 @@ Something.new.qux # => 'qux' - [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/) - [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 addition](http://ruby-doc.com/docs/ProgrammingRuby/) is available online. +- [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. -- cgit v1.2.3 From 081deac4b189f9fc4beced619653a2d602fd3b7a Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Wed, 4 Mar 2015 16:56:23 -0700 Subject: Add logical operators to Ruby tutorial --- ruby.html.markdown | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 7cf5bdc7..1883d1ad 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -60,8 +60,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 +73,17 @@ false.class #=> FalseClass 2 <= 2 #=> true 2 >= 2 #=> true +# Logical operators +true && false #=> false +true || false #=> true +!true #=> false + +# Alternate spellings of logical operators +true and false #=> false +true or false #=> true +not true #=> false + + # Strings are objects 'I am a string'.class #=> String @@ -280,9 +289,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 -- cgit v1.2.3 From 1b1d10e2d9b7493039382f0f6020e82d317d0c52 Mon Sep 17 00:00:00 2001 From: Rahil Momin Date: Thu, 19 Mar 2015 15:37:42 +0530 Subject: add include? method on arrays --- ruby.html.markdown | 3 +++ 1 file changed, 3 insertions(+) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 1883d1ad..800f0445 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -169,6 +169,9 @@ array[1..3] #=> [2, 3, 4] # Add to an array like this array << 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: hash = { 'color' => 'green', 'number' => 5 } -- cgit v1.2.3 From 37c00223d013da83d847767fbab6a3c4f960ad1a Mon Sep 17 00:00:00 2001 From: Rahil Momin Date: Thu, 19 Mar 2015 15:38:16 +0530 Subject: add has_key? and has_value? methods on hashes --- ruby.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 800f0445..8c9d8fc5 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -191,6 +191,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 -- cgit v1.2.3 From 1246edea3f83063b24c97240a10246025831843d Mon Sep 17 00:00:00 2001 From: Rahil Momin Date: Thu, 19 Mar 2015 15:38:27 +0530 Subject: add to ruby contributors --- ruby.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 8c9d8fc5..792c9c95 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"] --- -- cgit v1.2.3 From 7070304d60715405bb5f2432f4c84868a6418376 Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Tue, 16 Jun 2015 12:54:54 -0600 Subject: Fix explanation of "and" and "or" in ruby. --- ruby.html.markdown | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 792c9c95..66a0774d 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -79,10 +79,14 @@ true && false #=> false true || false #=> true !true #=> false -# Alternate spellings of logical operators -true and false #=> false -true or false #=> true -not 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 -- cgit v1.2.3 From c00ac0de6612feb54bf0b6d1040c953e2de5df81 Mon Sep 17 00:00:00 2001 From: Arthur Vieira Date: Wed, 26 Aug 2015 03:15:36 -0300 Subject: Add #push to Array besides shovel operator #push is commonly used imho and it is also used in other languages (e.g. Javascript). --- ruby.html.markdown | 2 ++ 1 file changed, 2 insertions(+) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 66a0774d..7bd28d86 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -173,6 +173,8 @@ 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 -- cgit v1.2.3 From cae1960ca3a5c25d8db458234ac0a6e97ab87ed5 Mon Sep 17 00:00:00 2001 From: Chashmeet Singh Date: Mon, 5 Oct 2015 17:51:23 +0530 Subject: Reverse an array --- ruby.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 7bd28d86..4e8dcc8f 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -168,6 +168,10 @@ array[-1] #=> 5 # With a start index and length array[2, 3] #=> [3, 4, 5] +# Reverse an Array +# array = [1,2,3] +array.reverse #=> [3,2,1] + # Or with a range array[1..3] #=> [2, 3, 4] -- cgit v1.2.3 From 51e3bd21e29ac19884eca57c6f4f1defbd78ea3a Mon Sep 17 00:00:00 2001 From: Chashmeet Singh Date: Mon, 5 Oct 2015 20:22:07 +0530 Subject: Reverse a list --- ruby.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 4e8dcc8f..b6d0e44d 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -169,8 +169,8 @@ array[-1] #=> 5 array[2, 3] #=> [3, 4, 5] # Reverse an Array -# array = [1,2,3] -array.reverse #=> [3,2,1] +a=[1,2,3] +a[::-1] #=> [3,2,1] # Or with a range array[1..3] #=> [2, 3, 4] -- cgit v1.2.3 From 0beb78ac436d232ce73986848f354fc4e7210dfb Mon Sep 17 00:00:00 2001 From: Chashmeet Singh Date: Mon, 5 Oct 2015 20:32:00 +0530 Subject: Reverse an array --- ruby.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index b6d0e44d..8f23b2e6 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -170,7 +170,7 @@ array[2, 3] #=> [3, 4, 5] # Reverse an Array a=[1,2,3] -a[::-1] #=> [3,2,1] +a.reverse! #=> [3,2,1] # Or with a range array[1..3] #=> [2, 3, 4] -- cgit v1.2.3