From 836133b871e6a8b75a5aed2d69f7099b1bc8971e Mon Sep 17 00:00:00 2001 From: Wayne Walker Date: Tue, 29 Aug 2017 17:39:42 -0500 Subject: Ruby has "methods" not "functions" --- 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 a1532855..e0a6bb6e 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -359,13 +359,13 @@ ensure puts 'This code always runs no matter what' end -# Functions +# Methods def double(x) x * 2 end -# Functions (and all blocks) implicitly return the value of the last statement +# Methods (and all blocks) implicitly return the value of the last statement double(2) #=> 4 # Parentheses are optional where the result is unambiguous @@ -399,7 +399,7 @@ surround { puts 'hello world' } # } -# You can pass a block to a function +# You can pass a block to a method # "&" marks a reference to a passed block def guests(&block) block.call 'some_argument' -- cgit v1.2.3 From 74ed7c684f0f7a22e61e12aba22cbb9b873e35ef Mon Sep 17 00:00:00 2001 From: Corey Ward Date: Fri, 23 Mar 2018 10:28:30 -0500 Subject: Various improvements to Ruby language doc --- ruby.html.markdown | 145 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 83 insertions(+), 62 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index e0a6bb6e..bbc8c558 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -15,32 +15,28 @@ contributors: - ["Gabriel Halley", "https://github.com/ghalley"] - ["Persa Zula", "http://persazula.com"] - ["Jake Faris", "https://github.com/farisj"] + - ["Corey Ward", "https://github.com/coreyward"] --- ```ruby # This is a comment -=begin -This is a multiline comment -No-one uses them -You shouldn't either -=end +# In Ruby, (almost) everything is an object. +# This includes numbers… +3.class #=> Integer -# First and foremost: Everything is an object. - -# Numbers are objects - -3.class #=> Fixnum - -3.to_s #=> "3" +# …strings… +"Hello".class #=> String +# …even methods! +"Hello".method(:class).class #=> Method # Some basic arithmetic 1 + 1 #=> 2 8 - 1 #=> 7 10 * 2 #=> 20 35 / 5 #=> 7 -2**5 #=> 32 +2 ** 5 #=> 32 5 % 3 #=> 2 # Bitwise operators @@ -52,6 +48,7 @@ You shouldn't either # for calling a method on an object 1.+(3) #=> 4 10.* 5 #=> 50 +100.methods.include?(:/) #=> true # Special values are objects nil # equivalent to null in other languages @@ -72,9 +69,10 @@ false.class #=> FalseClass # apart from false itself, nil is the only other 'falsey' value -!nil #=> true -!false #=> true -!0 #=> false +!!nil #=> false +!!false #=> false +!!0 #=> true +!!"" #=> true # More comparisons 1 < 10 #=> true @@ -82,7 +80,8 @@ false.class #=> FalseClass 2 <= 2 #=> true 2 >= 2 #=> true -# Combined comparison operator +# Combined comparison operator (returns `1` when the first argument is greater, +# `-1` when the second argument is greater, and `0` otherwise) 1 <=> 10 #=> -1 10 <=> 1 #=> 1 1 <=> 1 #=> 0 @@ -90,7 +89,6 @@ false.class #=> FalseClass # 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 @@ -101,23 +99,17 @@ 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 -"I am a string too".class #=> String +# 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 +# You can combine strings using `+`, but not with other types 'hello ' + 'world' #=> "hello world" 'hello ' + 3 #=> TypeError: can't convert Fixnum into String 'hello ' + 3.to_s #=> "hello 3" +"hello #{3}" #=> "hello 3" # Combine strings and operators 'hello ' * 3 #=> "hello hello hello " @@ -150,9 +142,8 @@ snake_case = true # Use descriptive variable names path_to_project_root = '/good/name/' -path = '/bad/name/' +m = '/bad/name/' -# Symbols (are objects) # Symbols are immutable, reusable constants represented internally by an # integer value. They're often used instead of strings to efficiently convey # specific, meaningful values @@ -167,6 +158,11 @@ status == 'pending' #=> false status == :approved #=> false +Strings can be converted into symbols and vice versa: + +status.to_s #=> "pending" +"argon".to_sym #=> :argon + # Arrays # This is an array @@ -196,7 +192,7 @@ array.last #=> 5 array[2, 3] #=> [3, 4, 5] # Reverse an Array -a=[1,2,3] +a = [1,2,3] a.reverse! #=> [3,2,1] # Or with a range @@ -223,7 +219,7 @@ hash['number'] #=> 5 # Asking a hash for a key that doesn't exist returns nil: hash['nothing here'] #=> nil -# Since Ruby 1.9, there's a special syntax when using symbols as keys: +# When using symbols for keys in a hash, you can use this alternate syntax: new_hash = { defcon: 3, action: true } @@ -246,33 +242,26 @@ else 'else, also optional' end + +# In Ruby, traditional `for` loops aren't very common. Instead, these +# basic loops are implemented using enumerable, which hinges on `each`: + +(1..5).each do |counter| + puts "iteration #{counter}" +end + +# Which is roughly equivalent to this, which is unusual to see in Ruby: + for counter in 1..5 puts "iteration #{counter}" end -#=> iteration 1 -#=> iteration 2 -#=> iteration 3 -#=> iteration 4 -#=> iteration 5 -# 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 `do |variable| ... end` construct above is called a “block”. Blocks are similar +# to lambdas, anonymous functions or closures in other programming languages. They can +# be passed around as objects, called, or attached as methods. # # 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}" -end -#=> iteration 1 -#=> iteration 2 -#=> iteration 3 -#=> iteration 4 -#=> iteration 5 # You can also surround blocks in curly brackets: (1..5).each { |counter| puts "iteration #{counter}" } @@ -365,10 +354,10 @@ def double(x) x * 2 end -# Methods (and all blocks) implicitly return the value of the last statement +# Methods (and blocks) implicitly return the value of the last statement double(2) #=> 4 -# Parentheses are optional where the result is unambiguous +# Parentheses are optional where the interpretation is unambiguous double 3 #=> 6 double double 3 #=> 12 @@ -399,25 +388,57 @@ surround { puts 'hello world' } # } -# You can pass a block to a method -# "&" marks a reference to a passed block +# Blocks can be converted into a `proc` object, which wraps the block +# and allows it to be passed to another method, bound to a different scope, +# or manipulated otherwise. This is most common in method parameter lists, +# where you frequently see a trailing `&block` parameter that will accept +# the block, if one is given, and convert it to a `Proc`. The naming here is +# convention; it would work just as well with `&pineapple`: def guests(&block) - block.call 'some_argument' + block.class #=> Proc + block.call(4) end +# The `call` method on the Proc is similar to calling `yield` when a block is +# present. The arguments passed to `call` will be forwarded to the block as arugments: + +guests { |n| "You have #{n} guests." } +# => "You have 4 guests." + # 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 -# If a method returns an array, you can use destructuring assignment -def foods - ['pancake', 'sandwich', 'quesadilla'] +# Destructuring + +# Ruby will automatically destrucure arrays on assignment to multiple variables: +a, b, c = [1, 2, 3] +a #=> 1 +b #=> 2 +c #=> 3 + +# In some cases, you will want to use the splat operator: `*` to prompt destructuring +# of an array into a list: + +ranked_competitors = ["John", "Sally", "Dingus", "Moe", "Marcy"] + +def best(first, second, third) + puts "Winners are #{first}, #{second}, and #{third}." end -breakfast, lunch, dinner = foods -breakfast #=> 'pancake' -dinner #=> 'quesadilla' + +best *ranked_competitors.first(3) #=> Winners are John, Sally, and Dingus. + +# The splat operator can also be used in parameters: +def best(first, second, third, *others) + puts "Winners are #{first}, #{second}, and #{third}." + puts "There were #{others.count} other participants." +end + +best *ranked_competitors +#=> Winners are John, Sally, and Dingus. +#=> There were 2 other participants. # By convention, all methods that return booleans end with a question mark 5.even? # false -- cgit v1.2.3 From 1f4fea47fdf512f500d2ac01748ab691cf8c1cab Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Sat, 16 Jun 2018 21:10:19 +0600 Subject: Fixed a typo --- 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 bbc8c558..4bc872da 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -413,7 +413,7 @@ end # Destructuring -# Ruby will automatically destrucure arrays on assignment to multiple variables: +# Ruby will automatically destructure arrays on assignment to multiple variables: a, b, c = [1, 2, 3] a #=> 1 b #=> 2 -- cgit v1.2.3 From 4af377e970908c97d88ff8713e6df5d06be160e6 Mon Sep 17 00:00:00 2001 From: bonte Date: Fri, 3 Aug 2018 23:34:39 +0200 Subject: fix comment --- 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 4bc872da..51f220e3 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -158,7 +158,7 @@ status == 'pending' #=> false status == :approved #=> false -Strings can be converted into symbols and vice versa: +# Strings can be converted into symbols and vice versa: status.to_s #=> "pending" "argon".to_sym #=> :argon -- cgit v1.2.3 From bce44c5ed98750beb7bad31666582a046bc6f14f Mon Sep 17 00:00:00 2001 From: Jannik <6362150+janniks@users.noreply.github.com> Date: Fri, 10 Aug 2018 13:45:11 +0200 Subject: Update ruby.html.markdown - Remove special characters - Unify style and grammar - Fix typos, etc. --- ruby.html.markdown | 246 ++++++++++++++++++++++++++--------------------------- 1 file changed, 120 insertions(+), 126 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 51f220e3..2f4d0934 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -16,19 +16,20 @@ contributors: - ["Persa Zula", "http://persazula.com"] - ["Jake Faris", "https://github.com/farisj"] - ["Corey Ward", "https://github.com/coreyward"] + - ["Jannik Siebert", "https://github.com/janniks"] --- ```ruby # This is a comment # In Ruby, (almost) everything is an object. -# This includes numbers… +# This includes numbers... 3.class #=> Integer -# …strings… +# ...and strings... "Hello".class #=> String -# …even methods! +# ...and even methods! "Hello".method(:class).class #=> Method # Some basic arithmetic @@ -67,7 +68,7 @@ false.class #=> FalseClass 1 != 1 #=> false 2 != 1 #=> true -# apart from false itself, nil is the only other 'falsey' value +# Apart from false itself, nil is the only other 'falsey' value !!nil #=> false !!false #=> false @@ -111,33 +112,33 @@ placeholder = 'use string interpolation' 'hello ' + 3.to_s #=> "hello 3" "hello #{3}" #=> "hello 3" -# Combine strings and operators +# ...or combine strings and operators 'hello ' * 3 #=> "hello hello hello " -# Append to string +# ...or append to string 'hello' << ' world' #=> "hello world" -# print to the output with a newline at the end +# You can print to the output with a newline at the end puts "I'm printing!" #=> I'm printing! #=> nil -# print to the output without a newline +# ...or print to the output without a newline print "I'm printing!" -#=> I'm printing! => nil +#=> "I'm printing!" => nil # Variables x = 25 #=> 25 x #=> 25 -# Note that assignment returns the value assigned -# This means you can do multiple assignment: +# Note that assignment returns the value assigned. +# This means you can do multiple assignment. x = y = 10 #=> 10 x #=> 10 y #=> 10 -# By convention, use snake_case for variable names +# By convention, use snake_case for variable names. snake_case = true # Use descriptive variable names @@ -146,7 +147,7 @@ m = '/bad/name/' # Symbols are immutable, reusable constants represented internally by an # integer value. They're often used instead of strings to efficiently convey -# specific, meaningful values +# specific, meaningful values. :pending.class #=> Symbol @@ -158,82 +159,82 @@ status == 'pending' #=> false status == :approved #=> false -# Strings can be converted into symbols and vice versa: - +# Strings can be converted into symbols and vice versa. status.to_s #=> "pending" "argon".to_sym #=> :argon # Arrays -# This is an array +# This is an array. array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] -# Arrays can contain different types of items - +# Arrays can contain different types of items. [1, 'hello', false] #=> [1, "hello", false] -# Arrays can be indexed -# From the front +# Arrays can be indexed. +# From the front... array[0] #=> 1 array.first #=> 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 +# ...or from the back... array[-1] #=> 5 array.last #=> 5 -# With a start index and length +# ...or with a start index and length... array[2, 3] #=> [3, 4, 5] -# Reverse an Array +# ...or with a range... +array[1..3] #=> [2, 3, 4] + +# You can reverse an Array. a = [1,2,3] a.reverse! #=> [3,2,1] -# Or with a range -array[1..3] #=> [2, 3, 4] +# Like arithmetic, [var] access is just syntactic sugar +# for calling a method '[]' on an object. +array.[] 0 #=> 1 +array.[] 12 #=> nil -# Add to an array like this +# You can add to an array... 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 +# ...and check if an item exists in an array array.include?(1) #=> true # Hashes are Ruby's primary dictionary with key/value pairs. -# Hashes are denoted with curly braces: +# Hashes are denoted with curly braces. hash = { 'color' => 'green', 'number' => 5 } hash.keys #=> ['color', 'number'] -# Hashes can be quickly looked up by key: -hash['color'] #=> 'green' +# Hashes can be quickly looked up by key. +hash['color'] #=> "green" hash['number'] #=> 5 -# Asking a hash for a key that doesn't exist returns nil: +# Asking a hash for a key that doesn't exist returns nil. hash['nothing here'] #=> nil -# When using symbols for keys in a hash, you can use this alternate syntax: +# When using symbols for keys in a hash, you can use an alternate syntax. -new_hash = { defcon: 3, action: true } +hash = { :defcon => 3, :action => true } +hash.keys #=> [:defcon, :action] -new_hash.keys #=> [:defcon, :action] +hash = { defcon: 3, action: true } +hash.keys #=> [:defcon, :action] # Check existence of keys and values in hash -new_hash.key?(:defcon) #=> true -new_hash.value?(3) #=> true +hash.key?(:defcon) #=> true +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 +# Tip: Both Arrays and Hashes are Enumerable! +# They share a lot of useful methods such as each, map, count, and more. # Control structures +# Conditionals if true 'if statement' elsif false @@ -242,28 +243,26 @@ else 'else, also optional' end - +# Loops # In Ruby, traditional `for` loops aren't very common. Instead, these -# basic loops are implemented using enumerable, which hinges on `each`: - +# basic loops are implemented using enumerable, which hinges on `each`. (1..5).each do |counter| puts "iteration #{counter}" end -# Which is roughly equivalent to this, which is unusual to see in Ruby: - +# Which is roughly equivalent to the following, which is unusual to see in Ruby. for counter in 1..5 puts "iteration #{counter}" end -# The `do |variable| ... end` construct above is called a “block”. Blocks are similar +# The `do |variable| ... end` construct above is called a 'block'. Blocks are similar # to lambdas, anonymous functions or closures in other programming languages. They can -# be passed around as objects, called, or attached as methods. +# be passed around as objects, called, or attached as methods. # -# The "each" method of a range runs the block once for each element of the range. +# 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. -# You can also surround blocks in curly brackets: +# You can also surround blocks in curly brackets. (1..5).each { |counter| puts "iteration #{counter}" } # The contents of data structures can also be iterated using each. @@ -274,8 +273,8 @@ hash.each do |key, value| puts "#{key} is #{value}" end -# If you still need an index you can use "each_with_index" and define an index -# variable +# 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" end @@ -291,9 +290,9 @@ end #=> iteration 4 #=> iteration 5 -# There are a bunch of other helpful looping functions in Ruby, -# for example "map", "reduce", "inject", the list goes on. Map, -# for instance, takes the array it's looping over, does something +# There are a bunch of other helpful looping functions in Ruby. +# For example: 'map', 'reduce', 'inject', the list goes on. +# Map, for instance, takes the array it's looping over, does something # to it as defined in your block, and returns an entirely new array. array = [1,2,3,4,5] doubled = array.map do |element| @@ -304,6 +303,7 @@ puts doubled puts array #=> [1,2,3,4,5] +# Case construct grade = 'B' case grade @@ -322,7 +322,7 @@ else end #=> "Better luck next time" -# cases can also use ranges +# Cases can also use ranges grade = 82 case grade when 90..100 @@ -334,9 +334,9 @@ else end #=> "OK job" -# exception handling: +# Exception handling begin - # code here that might raise an exception + # Code here that might raise an exception raise NoMemoryError, 'You ran out of memory.' rescue NoMemoryError => exception_variable puts 'NoMemoryError was raised', exception_variable @@ -354,10 +354,10 @@ def double(x) x * 2 end -# Methods (and blocks) implicitly return the value of the last statement +# Methods (and blocks) implicitly return the value of the last statement. double(2) #=> 4 -# Parentheses are optional where the interpretation is unambiguous +# Parentheses are optional where the interpretation is unambiguous. double 3 #=> 6 double double 3 #=> 12 @@ -366,15 +366,14 @@ def sum(x, y) x + y end -# Method arguments are separated by a comma +# Method arguments are separated by a comma. sum 3, 4 #=> 7 sum sum(3, 4), 5 #=> 12 # yield -# All methods have an implicit, optional block parameter -# it can be called with the 'yield' keyword - +# All methods have an implicit, optional block parameter. +# Tt can be called with the 'yield' keyword. def surround puts '{' yield @@ -383,45 +382,43 @@ end surround { puts 'hello world' } -# { -# hello world -# } +#=> { +#=> hello world +#=> } - -# Blocks can be converted into a `proc` object, which wraps the block +# Blocks can be converted into a 'proc' object, which wraps the block # and allows it to be passed to another method, bound to a different scope, # or manipulated otherwise. This is most common in method parameter lists, -# where you frequently see a trailing `&block` parameter that will accept -# the block, if one is given, and convert it to a `Proc`. The naming here is -# convention; it would work just as well with `&pineapple`: +# where you frequently see a trailing '&block' parameter that will accept +# the block, if one is given, and convert it to a 'Proc'. The naming here is +# convention; it would work just as well with '&pineapple'. def guests(&block) block.class #=> Proc block.call(4) end -# The `call` method on the Proc is similar to calling `yield` when a block is -# present. The arguments passed to `call` will be forwarded to the block as arugments: +# The 'call' method on the Proc is similar to calling 'yield' when a block is +# present. The arguments passed to 'call' will be forwarded to the block as arugments. guests { |n| "You have #{n} guests." } # => "You have 4 guests." -# You can pass a list of arguments, which will be converted into an array -# That's what splat operator ("*") is for +# 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 # Destructuring -# Ruby will automatically destructure arrays on assignment to multiple variables: +# Ruby will automatically destructure arrays on assignment to multiple variables. a, b, c = [1, 2, 3] a #=> 1 b #=> 2 c #=> 3 # In some cases, you will want to use the splat operator: `*` to prompt destructuring -# of an array into a list: - +# of an array into a list. ranked_competitors = ["John", "Sally", "Dingus", "Moe", "Marcy"] def best(first, second, third) @@ -430,7 +427,7 @@ end best *ranked_competitors.first(3) #=> Winners are John, Sally, and Dingus. -# The splat operator can also be used in parameters: +# The splat operator can also be used in parameters. def best(first, second, third, *others) puts "Winners are #{first}, #{second}, and #{third}." puts "There were #{others.count} other participants." @@ -440,21 +437,23 @@ best *ranked_competitors #=> Winners are John, Sally, and Dingus. #=> There were 2 other participants. -# By convention, all methods that return booleans end with a question mark -5.even? # false -5.odd? # true +# 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 +# By convention, if a method name 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 +# 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! +# We're mutating company_name this time. +company_name.upcase! #=> "DUNDER MIFFLIN" company_name #=> "DUNDER MIFFLIN" +# Classes -# Define a class with the class keyword +# You can define a class with the 'class' keyword. class Human # A class variable. It is shared by all instances of this class. @@ -462,7 +461,7 @@ class Human # Basic initializer def initialize(name, age = 0) - # Assign the argument to the "name" instance variable for the instance + # 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 @@ -478,10 +477,10 @@ class Human @name end - # The above functionality can be encapsulated using the attr_accessor method as follows + # 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 + # Getter/setter methods can also be created individually like this. attr_reader :name attr_writer :name @@ -496,13 +495,11 @@ class Human end end - -# Instantiate a class +# Instantiating of a class jim = Human.new('Jim Halpert') - dwight = Human.new('Dwight K. Schrute') -# Let's call a couple of methods +# You can call the methods of the generated object. jim.species #=> "H. sapiens" jim.name #=> "Jim Halpert" jim.name = "Jim Halpert II" #=> "Jim Halpert II" @@ -510,30 +507,30 @@ jim.name #=> "Jim Halpert II" dwight.species #=> "H. sapiens" dwight.name #=> "Dwight K. Schrute" -# Call the class method +# Calling of a class method Human.say('Hi') #=> "Hi" # Variable's scopes are defined by the way we name them. -# Variables that start with $ have global scope +# Variables that start with $ have global scope. $var = "I'm a global var" defined? $var #=> "global-variable" -# Variables that start with @ have instance scope +# Variables that start with @ have instance scope. @var = "I'm an instance var" defined? @var #=> "instance-variable" -# Variables that start with @@ have class scope +# 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 +# Variables that start with a capital letter are constants. Var = "I'm a constant" defined? Var #=> "constant" -# 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. +# Class is also an object in ruby. So a class can have instance variables. +# A class variable is shared among the class and all of its descendants. -# base class +# Base class class Human @@foo = 0 @@ -546,18 +543,17 @@ class Human end end -# derived class +# Derived class class Worker < Human end -Human.foo # 0 -Worker.foo # 0 - -Human.foo = 2 # 2 -Worker.foo # 2 +Human.foo #=> 0 +Worker.foo #=> 0 -# Class instance variable is not shared by the class's descendants. +Human.foo = 2 +Worker.foo #=> 2 +# A class instance variable is not shared by the class's descendants. class Human @bar = 0 @@ -573,8 +569,8 @@ end class Doctor < Human end -Human.bar # 0 -Doctor.bar # nil +Human.bar #=> 0 +Doctor.bar #=> nil module ModuleExample def foo @@ -582,9 +578,8 @@ module ModuleExample end end -# Including modules binds their methods to the class instances -# Extending modules binds their methods to the class itself - +# Including modules binds their methods to the class instances. +# Extending modules binds their methods to the class itself. class Person include ModuleExample end @@ -593,13 +588,12 @@ 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' +Person.foo #=> NoMethodError: undefined method `foo' for Person:Class +Person.new.foo #=> "foo" +Book.foo #=> "foo" +Book.new.foo #=> NoMethodError: undefined method `foo' # Callbacks are executed when including and extending a module - module ConcernExample def self.included(base) base.extend(ClassMethods) @@ -623,10 +617,10 @@ 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' +Something.bar #=> "bar" +Something.qux #=> NoMethodError: undefined method `qux' +Something.new.bar #=> NoMethodError: undefined method `bar' +Something.new.qux #=> "qux" ``` ## Additional resources -- cgit v1.2.3 From c3c6e3b08d9129d52a7349d0656569022ffa2f9b Mon Sep 17 00:00:00 2001 From: Keith Miyake Date: Wed, 3 Oct 2018 17:19:01 -0700 Subject: [ruby/en] fixes 3402 (spring cleanup) --- 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 2f4d0934..2635309b 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -17,6 +17,7 @@ contributors: - ["Jake Faris", "https://github.com/farisj"] - ["Corey Ward", "https://github.com/coreyward"] - ["Jannik Siebert", "https://github.com/janniks"] + - ["Keith Miyake", "https://github.com/kaymmm"] --- ```ruby @@ -83,9 +84,9 @@ false.class #=> FalseClass # Combined comparison operator (returns `1` when the first argument is greater, # `-1` when the second argument is greater, and `0` otherwise) -1 <=> 10 #=> -1 -10 <=> 1 #=> 1 -1 <=> 1 #=> 0 +1 <=> 10 #=> -1 (1 < 10) +10 <=> 1 #=> 1 (10 > 1) +1 <=> 1 #=> 0 (1 == 1) # Logical operators true && false #=> false @@ -188,8 +189,11 @@ array[2, 3] #=> [3, 4, 5] array[1..3] #=> [2, 3, 4] # You can reverse an Array. +# Return a new array with reversed values +[1,2,3].reverse #=> [3,2,1] +# Reverse an array in place to update variable with reversed values a = [1,2,3] -a.reverse! #=> [3,2,1] +a.reverse! #=> a==[3,2,1] because of the bang ('!') call to reverse # Like arithmetic, [var] access is just syntactic sugar # for calling a method '[]' on an object. -- cgit v1.2.3 From bb7e218b828b0ebf980f0c1c4a293c423bbdd4b4 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Tue, 16 Oct 2018 16:58:55 +0200 Subject: Update ruby.html.markdown Fix typo ("Tt"-->"It") --- 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 2635309b..2595d1d5 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -377,7 +377,7 @@ sum sum(3, 4), 5 #=> 12 # yield # All methods have an implicit, optional block parameter. -# Tt can be called with the 'yield' keyword. +# It can be called with the 'yield' keyword. def surround puts '{' yield -- cgit v1.2.3 From 4a14d54eb520f9776710102bfec740467b549745 Mon Sep 17 00:00:00 2001 From: Ilya Vorontsov Date: Thu, 5 Sep 2019 11:06:51 +0300 Subject: [ruby/ru] [ruby/en] added notes about postfix-if and about --- ruby.html.markdown | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 2595d1d5..d77672ab 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -247,6 +247,14 @@ else 'else, also optional' end +# If a condition controls invokation of a single statement rather than a block of code +# you can use postfix-if notation +warnings = ['Patronimic is missing', 'Address too short'] +puts("Some warnings occurred:\n" + warnings.join("\n")) if !warnings.empty? + +# Rephrase condition if `unless` sounds better than `if` +puts("Some warnings occurred:\n" + warnings.join("\n")) unless warnings.empty? + # Loops # In Ruby, traditional `for` loops aren't very common. Instead, these # basic loops are implemented using enumerable, which hinges on `each`. -- cgit v1.2.3 From d0d0fe4995d5be0bc7d1bc749c959468dec2f35c Mon Sep 17 00:00:00 2001 From: Chris Zimmerman Date: Tue, 1 Oct 2019 16:45:47 -0400 Subject: en/ruby - Adds documentation for multiline comments --- ruby.html.markdown | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index d77672ab..b36d1526 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -23,6 +23,15 @@ contributors: ```ruby # This is a comment +=begin +This is a multi-line comment. +The beginning line must start with "=begin" +and the ending line must start with "=end". + +You can do this, or start each line in +a multi-line comment with the # character. +=end + # In Ruby, (almost) everything is an object. # This includes numbers... 3.class #=> Integer -- cgit v1.2.3 From 03c06ac1bc8dc2da43d6ff29cade0d235103033c Mon Sep 17 00:00:00 2001 From: Alex Mendes da Costa Date: Mon, 9 Dec 2019 09:19:36 -0800 Subject: Fix typo in ruby.html.markdown "arugments" => "arguments" --- 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 d77672ab..f437adcf 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -410,7 +410,7 @@ def guests(&block) end # The 'call' method on the Proc is similar to calling 'yield' when a block is -# present. The arguments passed to 'call' will be forwarded to the block as arugments. +# present. The arguments passed to 'call' will be forwarded to the block as arguments. guests { |n| "You have #{n} guests." } # => "You have 4 guests." -- cgit v1.2.3 From 2eb57cb02376df701e80d2692deab5809214171a Mon Sep 17 00:00:00 2001 From: Barton Stanley Date: Fri, 31 Jan 2020 13:04:15 -0600 Subject: Change "invokation" to "invocation" --- 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 3fc2ed2d..376f4a47 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -256,7 +256,7 @@ else 'else, also optional' end -# If a condition controls invokation of a single statement rather than a block of code +# If a condition controls invocation of a single statement rather than a block of code # you can use postfix-if notation warnings = ['Patronimic is missing', 'Address too short'] puts("Some warnings occurred:\n" + warnings.join("\n")) if !warnings.empty? -- cgit v1.2.3 From c6804101b9f60c9083f88b24a1dfa5b91443fc94 Mon Sep 17 00:00:00 2001 From: Jake Haber Date: Tue, 21 Apr 2020 07:33:51 -0500 Subject: Add Ruby shorthand block syntax examples --- ruby.html.markdown | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 376f4a47..d21c727f 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -430,6 +430,16 @@ def guests(*array) array.each { |guest| puts guest } end +# There is also the shorthand block syntax. It's most useful when you need +# to call a simple method on all array items. +upcased = ['Watch', 'these', 'words', 'get', 'upcased'].map(&:upcase) +puts upcased +#=> ["WATCH", "THESE", "WORDS", "GET", "UPCASED"] + +sum = [1, 2, 3, 4, 5].reduce(&:+) +puts sum +#=> 15 + # Destructuring # Ruby will automatically destructure arrays on assignment to multiple variables. -- cgit v1.2.3 From a67f4663eb1b477cb50da9986fc5da945864ebab Mon Sep 17 00:00:00 2001 From: Claudio Secco Date: Sat, 2 May 2020 13:03:36 -0300 Subject: include %w option --- 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 376f4a47..97f7a516 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -181,6 +181,9 @@ array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] # Arrays can contain different types of items. [1, 'hello', false] #=> [1, "hello", false] +# You might prefer %w instead of quotes +%w[foo bar baz] #=> ["foo", "bar", "baz"] + # Arrays can be indexed. # From the front... array[0] #=> 1 -- cgit v1.2.3 From d9ee9ce3903f078c68d796464c70c463c1bf133b Mon Sep 17 00:00:00 2001 From: Claudio Secco Date: Sat, 2 May 2020 16:09:41 -0300 Subject: [ruby/en] add .map($:method) sintax in blocks context --- ruby.html.markdown | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 97f7a516..3111d09b 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -327,6 +327,11 @@ puts doubled puts array #=> [1,2,3,4,5] +# another useful sintax is .map(&:method) +a = ["FOO", "BAR", "BAZ"] +a.map { |s| s.downcase } #=> ["foo", "bar", "baz"] +a.map(&:downcase) #=> ["foo", "bar", "baz"] + # Case construct grade = 'B' -- cgit v1.2.3 From 20a468579ac87bd56f1f9e95a7f065e3519522af Mon Sep 17 00:00:00 2001 From: Claudio Secco <41170119+claudiosecco@users.noreply.github.com> Date: Tue, 7 Jul 2020 20:27:34 -0300 Subject: [ruby/en] fix typo --- 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 3111d09b..0e35a7ec 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -327,7 +327,7 @@ puts doubled puts array #=> [1,2,3,4,5] -# another useful sintax is .map(&:method) +# another useful syntax is .map(&:method) a = ["FOO", "BAR", "BAZ"] a.map { |s| s.downcase } #=> ["foo", "bar", "baz"] a.map(&:downcase) #=> ["foo", "bar", "baz"] -- cgit v1.2.3