Declaring methods
def functionName(parameters)
# content
end
remark: void methods will not have brackets, or nothing inside the brackets for parameters.
------------------------------------------------------------------------------------------------
Splat arguments
def what_up(greeting, *bros)
bros.each { |bro| puts "#{greeting}, #{bro}!" }
end
what_up("What up", "Justin", "Ben", "Kevin Sorbo")
Splat arguments are arguments preceded by a *, which signals to Ruby: "Hey Ruby, I don't know how many arguments there are about to be, but it could be more than one."
-------------------------------------------------------------------------------------
Blocks
Blocks can be defined with either the keywords do and end or with curly braces ({}).
# You can provide parameters to the call to yield:
# these will be passed to the block
def call_block
yield('hello', 99)
end
call_block {|str, num| puts str + ' ' + num.to_s}
or, you could do something like:
=begin
Ruby Code blocks are chunks of code between braces or
between do..end that you can associate with method invocations
=end
def call_block
puts 'Start of method'
# you can call the block using the yield keyword
yield
yield
puts 'End of method'
end
# Code blocks may appear only in the source adjacent to a method call
call_block {puts 'In the block'}
---------------------------------------------------------------------------------------------
method vs blocks
# they basically do the same thing here. Just different in how they are written.
# method that capitalizes a word
def capitalize(string)
puts "#{string[0].upcase}#{string[1..-1]}"
end
capitalize("ryan") # prints "Ryan"
capitalize("jane") # prints "Jane"
# block that capitalizes each string in the array
["ryan", "jane"].each {|string| puts "#{string[0].upcase}#{string[1..-1]}"} # prints "Ryan", then "Jane"
---------------------------------------------------------------------------------------------
sort
.sort! will sort the array itself.
the combined comparison operator: <=>
It returns 0 if the first operand (item to be compared) equals the second,
1 if first operand is greater than the second, and
-1 if the first operand is less than the second.
example:
books = ["Charlie and the Chocolate Factory", "War and Peace", "Utopia", "A Brief History of Time", "A Wrinkle in Time"]
# To sort our books in ascending order, in-place
books.sort! { |firstBook, secondBook| firstBook <=> secondBook }
# Sort your books in descending order, in-place below
books.sort! { |firstBook, secondBook| secondBook <=> firstBook }
def functionName(parameters)
# content
end
remark: void methods will not have brackets, or nothing inside the brackets for parameters.
------------------------------------------------------------------------------------------------
Splat arguments
def what_up(greeting, *bros)
bros.each { |bro| puts "#{greeting}, #{bro}!" }
end
what_up("What up", "Justin", "Ben", "Kevin Sorbo")
Splat arguments are arguments preceded by a *, which signals to Ruby: "Hey Ruby, I don't know how many arguments there are about to be, but it could be more than one."
-------------------------------------------------------------------------------------
Blocks
Blocks can be defined with either the keywords do and end or with curly braces ({}).
# You can provide parameters to the call to yield:
# these will be passed to the block
def call_block
yield('hello', 99)
end
call_block {|str, num| puts str + ' ' + num.to_s}
or, you could do something like:
=begin
Ruby Code blocks are chunks of code between braces or
between do..end that you can associate with method invocations
=end
def call_block
puts 'Start of method'
# you can call the block using the yield keyword
yield
yield
puts 'End of method'
end
# Code blocks may appear only in the source adjacent to a method call
call_block {puts 'In the block'}
---------------------------------------------------------------------------------------------
method vs blocks
# they basically do the same thing here. Just different in how they are written.
# method that capitalizes a word
def capitalize(string)
puts "#{string[0].upcase}#{string[1..-1]}"
end
capitalize("ryan") # prints "Ryan"
capitalize("jane") # prints "Jane"
# block that capitalizes each string in the array
["ryan", "jane"].each {|string| puts "#{string[0].upcase}#{string[1..-1]}"} # prints "Ryan", then "Jane"
---------------------------------------------------------------------------------------------
sort
.sort! will sort the array itself.
the combined comparison operator: <=>
It returns 0 if the first operand (item to be compared) equals the second,
1 if first operand is greater than the second, and
-1 if the first operand is less than the second.
example:
books = ["Charlie and the Chocolate Factory", "War and Peace", "Utopia", "A Brief History of Time", "A Wrinkle in Time"]
# To sort our books in ascending order, in-place
books.sort! { |firstBook, secondBook| firstBook <=> secondBook }
# Sort your books in descending order, in-place below
books.sort! { |firstBook, secondBook| secondBook <=> firstBook }