mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-11-22 12:32:09 +03:00
[ruby/en] Reflow comments (#5180)
This commit is contained in:
parent
fa95b37b6f
commit
54fbf0a948
@ -91,7 +91,7 @@ false.class #=> FalseClass
|
|||||||
2 <= 2 #=> true
|
2 <= 2 #=> true
|
||||||
2 >= 2 #=> true
|
2 >= 2 #=> true
|
||||||
|
|
||||||
# Combined comparison operator (returns `1` when the first argument is greater,
|
# Combined comparison operator (returns `1` when the first argument is greater,
|
||||||
# `-1` when the second argument is greater, and `0` otherwise)
|
# `-1` when the second argument is greater, and `0` otherwise)
|
||||||
1 <=> 10 #=> -1 (1 < 10)
|
1 <=> 10 #=> -1 (1 < 10)
|
||||||
10 <=> 1 #=> 1 (10 > 1)
|
10 <=> 1 #=> 1 (10 > 1)
|
||||||
@ -259,8 +259,8 @@ else
|
|||||||
'else, also optional'
|
'else, also optional'
|
||||||
end
|
end
|
||||||
|
|
||||||
# If a condition controls invocation of a single statement rather than a block of code
|
# If a condition controls invocation of a single statement rather than a block
|
||||||
# you can use postfix-if notation
|
# of code you can use postfix-if notation
|
||||||
warnings = ['Patronimic is missing', 'Address too short']
|
warnings = ['Patronimic is missing', 'Address too short']
|
||||||
puts("Some warnings occurred:\n" + warnings.join("\n")) if !warnings.empty?
|
puts("Some warnings occurred:\n" + warnings.join("\n")) if !warnings.empty?
|
||||||
|
|
||||||
@ -268,7 +268,7 @@ puts("Some warnings occurred:\n" + warnings.join("\n")) if !warnings.empty?
|
|||||||
puts("Some warnings occurred:\n" + warnings.join("\n")) unless warnings.empty?
|
puts("Some warnings occurred:\n" + warnings.join("\n")) unless warnings.empty?
|
||||||
|
|
||||||
# Loops
|
# Loops
|
||||||
# In Ruby, traditional `for` loops aren't very common. Instead, these
|
# 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|
|
(1..5).each do |counter|
|
||||||
puts "iteration #{counter}"
|
puts "iteration #{counter}"
|
||||||
@ -279,9 +279,10 @@ for counter in 1..5
|
|||||||
puts "iteration #{counter}"
|
puts "iteration #{counter}"
|
||||||
end
|
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
|
||||||
# to lambdas, anonymous functions or closures in other programming languages. They can
|
# similar to lambdas, anonymous functions or closures in other programming
|
||||||
# be passed around as objects, called, or attached as methods.
|
# 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 'each' method of a range runs the block once for each element of the range.
|
||||||
# The block is passed a counter as a parameter.
|
# The block is passed a counter as a parameter.
|
||||||
@ -415,19 +416,20 @@ 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
|
||||||
# and allows it to be passed to another method, bound to a different scope,
|
# it to be passed to another method, bound to a different scope, or manipulated
|
||||||
# or manipulated otherwise. This is most common in method parameter lists,
|
# otherwise. This is most common in method parameter lists, where you frequently
|
||||||
# where you frequently see a trailing '&block' parameter that will accept
|
# see a trailing '&block' parameter that will accept the block, if one is given,
|
||||||
# the block, if one is given, and convert it to a 'Proc'. The naming here is
|
# and convert it to a 'Proc'. The naming here is convention; it would work just
|
||||||
# convention; it would work just as well with '&pineapple'.
|
# as well with '&pineapple'.
|
||||||
def guests(&block)
|
def guests(&block)
|
||||||
block.class #=> Proc
|
block.class #=> Proc
|
||||||
block.call(4)
|
block.call(4)
|
||||||
end
|
end
|
||||||
|
|
||||||
# The 'call' method on the Proc is similar to calling 'yield' when a block is
|
# 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 arguments.
|
# present. The arguments passed to 'call' will be forwarded to the block as
|
||||||
|
# arguments.
|
||||||
|
|
||||||
guests { |n| "You have #{n} guests." }
|
guests { |n| "You have #{n} guests." }
|
||||||
# => "You have 4 guests."
|
# => "You have 4 guests."
|
||||||
@ -443,7 +445,7 @@ end
|
|||||||
upcased = ['Watch', 'these', 'words', 'get', 'upcased'].map(&:upcase)
|
upcased = ['Watch', 'these', 'words', 'get', 'upcased'].map(&:upcase)
|
||||||
puts upcased
|
puts upcased
|
||||||
#=> ["WATCH", "THESE", "WORDS", "GET", "UPCASED"]
|
#=> ["WATCH", "THESE", "WORDS", "GET", "UPCASED"]
|
||||||
|
|
||||||
sum = [1, 2, 3, 4, 5].reduce(&:+)
|
sum = [1, 2, 3, 4, 5].reduce(&:+)
|
||||||
puts sum
|
puts sum
|
||||||
#=> 15
|
#=> 15
|
||||||
@ -472,7 +474,7 @@ def best(first, second, third, *others)
|
|||||||
puts "There were #{others.count} other participants."
|
puts "There were #{others.count} other participants."
|
||||||
end
|
end
|
||||||
|
|
||||||
best *ranked_competitors
|
best *ranked_competitors
|
||||||
#=> Winners are John, Sally, and Dingus.
|
#=> Winners are John, Sally, and Dingus.
|
||||||
#=> There were 2 other participants.
|
#=> There were 2 other participants.
|
||||||
|
|
||||||
@ -480,9 +482,9 @@ best *ranked_competitors
|
|||||||
5.even? #=> false
|
5.even? #=> false
|
||||||
5.odd? #=> true
|
5.odd? #=> true
|
||||||
|
|
||||||
# By convention, if a method name ends with an exclamation mark, it does something destructive
|
# By convention, if a method name ends with an exclamation mark, it does
|
||||||
# like mutate the receiver. Many methods have a ! version to make a change, and
|
# something destructive like mutate the receiver. Many methods have a ! version
|
||||||
# a non-! version to just return a new changed version.
|
# to make a change, and a non-! version to just return a new changed version.
|
||||||
company_name = "Dunder Mifflin"
|
company_name = "Dunder Mifflin"
|
||||||
company_name.upcase #=> "DUNDER MIFFLIN"
|
company_name.upcase #=> "DUNDER MIFFLIN"
|
||||||
company_name #=> "Dunder Mifflin"
|
company_name #=> "Dunder Mifflin"
|
||||||
@ -516,7 +518,8 @@ class Human
|
|||||||
@name
|
@name
|
||||||
end
|
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
|
attr_accessor :name
|
||||||
|
|
||||||
# Getter/setter methods can also be created individually like this.
|
# Getter/setter methods can also be created individually like this.
|
||||||
|
Loading…
Reference in New Issue
Block a user