0% found this document useful (0 votes)
4 views4 pages

Learn Ruby_ Blocks and Sorting Cheatsheet _ Codecademy

The document provides an overview of Ruby programming concepts, including methods, parameters, blocks, and sorting. It explains how to define methods, use parameters and arguments, implement splat operators for variable arguments, and utilize blocks for code execution. Additionally, it covers the Ruby sort method and the combined comparison operator for comparing objects.

Uploaded by

Chia Wei Ng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Learn Ruby_ Blocks and Sorting Cheatsheet _ Codecademy

The document provides an overview of Ruby programming concepts, including methods, parameters, blocks, and sorting. It explains how to define methods, use parameters and arguments, implement splat operators for variable arguments, and utilize blocks for code execution. Additionally, it covers the Ruby sort method and the combined comparison operator for comparing objects.

Uploaded by

Chia Wei Ng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Cheatsheets / Learn Ruby

Blocks and Sor ting


Ruby method
A Ruby method is a reusable section of code written
to execute a certain task. It is defined with the def def greeting
keyword, followed by a method name, a method body, puts "Hello world!"
and ends with the end keyword: end

#In this example, the first line or


header contains the keyword "def" and
the method name. puts "Hello world!" is
within the body of the method, which
describes the certain task that the
method carries out. It is also indented
two spaces by convention. Following the
body, the method ends with the end
keyword.

Ruby Method Parameters & Arguments


In Ruby, parameters are placeholders for real values or
arguments passed into a method when it is called. def square(num) # num is the parameter
When calling a method that requires parameters, puts num ** 2
arguments (ie. real values) must be passed in for those end
parameters.

square(5) #5 is the argument


#Output => 25
Ruby Method Splat
In a Ruby method, a splat ( * ) operator is used to
indicate that a parameter can have an unknown #The * preceding the parameter "clubs"
number of arguments. allows for multiple arguments to be
passed into the method when you actually
call it.
def extra_curriculars(*clubs)
clubs.each { |club| puts "After
school, I'm involved with #{club}" }
end

extra_curriculars("chess club",
"gymnastics", "anime club", "library
services")

#Output
#After school, I'm involved with chess
club
#After school, I'm involved with
gymnastics
#After school, I'm involved with anime
club
#After school, I'm involved with library
services

Ruby Return
In Ruby, the return keyword is used to pass back
a value from a method. def generous_tip(bill)
return bill * (0.25)
end

generous_tip(100) # 25

#In this example, the generous_tip


method is returning the product of bill
and 0.25. In order to see that value, a
"puts" or "print" can be added before
the method call.
Ruby Block
In Ruby, a block is a section of code defined within the
keywords do and end or with curly braces {} . 2.times do
This is usually preceded by an integer followed by puts "I'm a code block!"
.times to indicate how many times the code is to end
be executed.
#Output
#I'm a code block!
#I'm a code block!

3.times { puts "So am I!" }

#Output
#"So am I!"
#"So am I!"
#"So am I!"

Ruby Block Parameter


In Ruby, a method can take a block as a parameter.
Passing a block to a method is a great way of # The block, {|i| puts i}, is passed the
abstracting certain tasks from the method and current array item each time it is
defining those tasks when we call the method. evaluated. This block prints the item.
[1, 2, 3, 4, 5].each { |i| puts i }

Ruby Sort Method


In Ruby, the .sort array method is used to sort
items in an array in ascending order (least to greatest). my_array = [3, 4, 8, 7, 1, 6, 5, 9, 2]
my_array.sort!
#Attaching an ! to the end of .sort or
any other Ruby method modifies the
original array.
print my_array
# => [1, 2, 3, 4, 5, 6, 7, 8, 9]
#If you didn't use !, print my_array
returns the original array.
Ruby Combined Comparison Operator
In Ruby, the combined comparison operator, <=> ,
also known as the spaceship operator is used to puts "Keanu" <=> "Adrianna" # The first
compare two objects. It returns 0 if the first operand letters of each word are compared in
equals the second, 1 if the first operand is greater
ASCII order and since "K" comes after
"A", 1 is printed.
than the second, and -1 if the first operand is less
than the second.
puts 1 <=> 2 # -1

puts 3 <=> 3 # 0

#<=> can also be used inside of a block


and to sort values in descending order:
my_array = [3, 0, 8, 7, 1, 6, 5, 9, 4]
my_array.sort! { |first_num, second_num|
second_num <=> first_num }
print my_array
#Output => [9, 8, 7, 6, 5, 4, 3, 1, 0]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy