Last active
December 30, 2015 17:59
-
-
Save randalmaile/7864773 to your computer and use it in GitHub Desktop.
Arrays Checkpoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Returning Array elements | |
- fruits = ["apple", "banana", "orange"] | |
- fruits.first (**Cool - there's a plain english descriptions for the position!!**) | |
- #=> "apple" | |
- fruits.third | |
- #=> "orange" | |
- fruits.last | |
- #=> "orange" | |
- If we want to add an element to the end of an array we can use the "shovel" (<<) operator. | |
- numbers = [1] | |
- numbers << 2 | |
- numbers | |
- #=> [1,2] | |
##Negative Indexes | |
Indexes, like the the ones in the examples above, retrieve elements from an array going left to right. Ruby also allows us to define negative indexes, which retrieve elements from right to left. For example: | |
fruits = ["apple", "banana", "orange"] | |
- fruits[-1] | |
- #=> "orange" | |
- fruits[-2] | |
- #=> "banana" | |
- fruits[-3] | |
- #=> "apple" | |
Notice that when using negative indexes, counting starts at -1, rather than 0. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```Bloc output - Array Definition | |
``` | |
new_array creates an array of numbers | |
new_array creates an array of strings | |
first_and_last creates new array with numbers | |
first_and_last creates new array with strings | |
```Bloc output - Array Methods | |
``` | |
reverse_plus_one creates a new array for a short array | |
reverse_plus_one creates a new array for a longer array | |
pluses_everywhere returns a single plus for a short array | |
pluses_everywhere returns pluses for longer arrays | |
array_quantity_plus_one returns the number of items for a short array plus one | |
array_quantity_plus_one returns the number of items for a long array plus one | |
```Bloc output - Grocery List | |
``` | |
add_item adds an item to the end of the list | |
add_item doesn't add an item if the list already has it | |
remove_item removes an item if it's in the list | |
remove_item removes an item if it's in the list | |
remove_item doesn't remove an item if it's not in the list | |
full_list returns the list sorted | |
full_list returns the list sorted without duplicates | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Just want to make sure I got this right (QUESTIONS ARE COMMENTED ON THE LINE ITSELF) | |
# Grocery List section | |
def add_item(item, list) | |
if !(list.include?(item)) | |
list << item # **DOES THIS EXPRESSION RETURN THE APPENDED LIST??** | |
else | |
list # **IS THIS CORRECT - JUST RETURN THE LIST ITSELF?** | |
end | |
end | |
def remove_item(item, list) | |
list.delete(item) # **AM I CORRECT IN ASSUMING THAT list.delete(item) DOES NOT RETURN THE LIST??** | |
list # **...HENCE RETURNING IT HERE??** | |
end | |
def full_list(list) #the list argument is expected to be an array of grocery items | |
list.uniq.sort # **DOES THIS EXPRESSION RETURN THE UNIQUE/SORTED LIST??** | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ARRAY DEFINITION | |
def new_array(a,b,c,d) | |
#return an array consiting of the arguments here | |
$new_array = [a,b,c,d] | |
end | |
def first_and_last(a) | |
$first_and_last = [a.first, a.last] | |
end | |
# Array Methods | |
def reverse_plus_one(a) | |
a << a.first | |
a.reverse | |
end | |
def pluses_everywhere(a) | |
#convert the array argument to a string and insert a "+" between each element | |
a.join("+") | |
end | |
def array_quantity_plus_one(a) | |
# return the number of elements in the array argument, plus 1. | |
a.length + 1 | |
end | |
#Grocery List | |
# Create prog - manage grocery list | |
def add_item(item, list) | |
if !(list.include?(item)) | |
list << item | |
else | |
list | |
end | |
end | |
def remove_item(item, list) | |
list.delete(item) | |
list | |
end | |
def full_list(list) #the list argument is expected to be an array of grocery items | |
list.uniq.sort | |
end | |
mkwiatkowski
commented
Dec 9, 2013
[1] pry(main)> x = [1,2,3]
=> [1, 2, 3]
[2] pry(main)> x.third
NoMethodError: undefined method `third' for [1, 2, 3]:Array
from (pry):2:in `__pry__'
[3] pry(main)> require 'active_support/all'
=> true
[4] pry(main)> x.third
=> 3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment