Merge pull request #723 from dskecse/ruby-en-style-changes

[ruby/en] Make style fixes to conform to Ruby style guide
This commit is contained in:
Levi Bostian 2014-09-05 21:06:05 -05:00
commit 6f32b5bfd9

View File

@ -9,6 +9,7 @@ contributors:
- ["Nick LaMuro", "https://github.com/NickLaMuro"] - ["Nick LaMuro", "https://github.com/NickLaMuro"]
- ["Marcos Brizeno", "http://www.about.me/marcosbrizeno"] - ["Marcos Brizeno", "http://www.about.me/marcosbrizeno"]
- ["Ariel Krakowski", "http://www.learneroo.com"] - ["Ariel Krakowski", "http://www.learneroo.com"]
- ["Dzianis Dashkevich", "https://github.com/dskecse"]
--- ---
@ -35,7 +36,7 @@ You shouldn't either
8 - 1 #=> 7 8 - 1 #=> 7
10 * 2 #=> 20 10 * 2 #=> 20
35 / 5 #=> 7 35 / 5 #=> 7
2 ** 5 #=> 32 2**5 #=> 32
# Arithmetic is just syntactic sugar # Arithmetic is just syntactic sugar
# for calling a method on an object # for calling a method on an object
@ -78,14 +79,17 @@ false.class #=> FalseClass
'I am a string'.class #=> String 'I am a string'.class #=> String
"I am a string too".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 #{placeholder} when using double quoted strings"
#=> "I can use string interpolation 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 # Combine strings, but not with numbers
"hello " + "world" #=> "hello world" 'hello ' + 'world' #=> "hello world"
"hello " + 3 #=> TypeError: can't convert Fixnum into String '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 # print to the output
puts "I'm printing!" 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 # Arrays can contain different types of items
[1, "hello", false] #=> [1, "hello", false] [1, 'hello', false] #=> [1, "hello", false]
# Arrays can be indexed # Arrays can be indexed
# From the front # 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 Ruby's primary dictionary with keys/value pairs.
# Hashes are denoted with curly braces: # Hashes are denoted with curly braces:
hash = {'color' => 'green', 'number' => 5} hash = { 'color' => 'green', 'number' => 5 }
hash.keys #=> ['color', 'number'] 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: # 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] new_hash.keys #=> [:defcon, :action]
@ -180,11 +184,11 @@ new_hash.keys #=> [:defcon, :action]
# Control structures # Control structures
if true if true
"if statement" 'if statement'
elsif false elsif false
"else if, optional" 'else if, optional'
else else
"else, also optional" 'else, also optional'
end end
for counter in 1..5 for counter in 1..5
@ -216,7 +220,7 @@ end
#=> iteration 5 #=> iteration 5
# You can also surround blocks in curly brackets: # 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. # The contents of data structures can also be iterated using each.
array.each do |element| array.each do |element|
@ -241,32 +245,30 @@ grade = 'B'
case grade case grade
when 'A' when 'A'
puts "Way to go kiddo" puts 'Way to go kiddo'
when 'B' when 'B'
puts "Better luck next time" puts 'Better luck next time'
when 'C' when 'C'
puts "You can do better" puts 'You can do better'
when 'D' when 'D'
puts "Scraping through" puts 'Scraping through'
when 'F' when 'F'
puts "You failed!" puts 'You failed!'
else else
puts "Alternative grading system, eh?" puts 'Alternative grading system, eh?'
end end
#=> "Better luck next time" #=> "Better luck next time"
# cases can also use ranges # cases can also use ranges
grade = 82 grade = 82
case grade case grade
when 90..100 when 90..100
puts "Hooray!" puts 'Hooray!'
when 80...90 when 80...90
puts "OK job" puts 'OK job'
else else
puts "You failed!" puts 'You failed!'
end end
#=> "OK job" #=> "OK job"
@ -284,23 +286,23 @@ double 3 #=> 6
double double 3 #=> 12 double double 3 #=> 12
def sum(x,y) def sum(x, y)
x + y x + y
end end
# Method arguments are separated by a comma # Method arguments are separated by a comma
sum 3, 4 #=> 7 sum 3, 4 #=> 7
sum sum(3,4), 5 #=> 12 sum sum(3, 4), 5 #=> 12
# yield # yield
# All methods have an implicit, optional block parameter # All methods have an implicit, optional block parameter
# it can be called with the 'yield' keyword # it can be called with the 'yield' keyword
def surround def surround
puts "{" puts '{'
yield yield
puts "}" puts '}'
end end
surround { puts 'hello world' } surround { puts 'hello world' }
@ -313,23 +315,23 @@ surround { puts 'hello world' }
# You can pass a block to a function # 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) def guests(&block)
block.call "some_argument" block.call 'some_argument'
end end
# You can pass a list of arguments, which will be converted into an array # 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) def guests(*array)
array.each { |guest| puts "#{guest}" } array.each { |guest| puts guest }
end end
# Define a class with the class keyword # Define a class with the class keyword
class Human class Human
# A class variable. It is shared by all instances of this class. # A class variable. It is shared by all instances of this class.
@@species = "H. sapiens" @@species = 'H. sapiens'
# Basic initializer # Basic initializer
def initialize(name, age=0) 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 @name = name
# If no age given, we will fall back to the default in the arguments list. # 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. # A class method uses self to distinguish from instance methods.
# It can only be called on the class, not an instance. # It can only be called on the class, not an instance.
def self.say(msg) def self.say(msg)
puts "#{msg}" puts msg
end end
def species def species
@@species @@species
end end
end end
# Instantiate a class # 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 # Let's call a couple of methods
jim.species #=> "H. sapiens" jim.species #=> "H. sapiens"
@ -380,7 +381,7 @@ dwight.species #=> "H. sapiens"
dwight.name #=> "Dwight K. Schrute" dwight.name #=> "Dwight K. Schrute"
# Call the class method # Call the class method
Human.say("Hi") #=> "Hi" Human.say('Hi') #=> "Hi"
# Variable's scopes are defined by the way we name them. # 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
@ -399,7 +400,7 @@ defined? @@var #=> "class variable"
Var = "I'm a constant" Var = "I'm a constant"
defined? Var #=> "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. # Class variable is shared among the class and all of its descendants.
# base class # base class
@ -415,7 +416,7 @@ class Human
end end
end end
# derived class # derived class
class Worker < Human class Worker < Human
end end
@ -451,8 +452,8 @@ module ModuleExample
end end
end end
# Including modules binds the methods to the object instance # Including modules binds their methods to the class instances
# Extending modules binds the methods to the class instance # Extending modules binds their methods to the class itself
class Person class Person
include ModuleExample include ModuleExample
@ -467,7 +468,7 @@ Person.new.foo # => 'foo'
Book.foo # => 'foo' Book.foo # => 'foo'
Book.new.foo # => NoMethodError: undefined method `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 module ConcernExample
def self.included(base) def self.included(base)
@ -504,5 +505,4 @@ Something.new.qux # => 'qux'
- [Official Documentation](http://www.ruby-doc.org/core-2.1.1/) - [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/) - [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.