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