-
-
Save meaganewaller/762e6b4042f7d836ebde to your computer and use it in GitHub Desktop.
Ruby arrays
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
>> vowels = [ "a", "e", "i", "o", "u"] | |
>> vowels[0] | |
=> "a" | |
>> vowels.size | |
=> 5 |
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
>> [1, 2] << 3 | |
=> [1, 2, 3] | |
>> [1, 2, 3].push(4) | |
=> [1, 2, 3, 4] | |
>> [1, 2, 3] << [3, 2, 1] | |
=> [1, 2, 3, [3, 2, 1]] |
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 = [1, 2, 3] | |
>> array.pop | |
=> 3 | |
>> array | |
=> [1, 2] | |
>> array = [1, 2, 3, 4] | |
>> array.shift | |
=> 4 | |
>> array | |
=> [1, 2, 3] | |
>> array = [ 1, 2, 3, 4] | |
>> array.delete(1) | |
=> 1 | |
>> array | |
=> [2, 3, 4] | |
>> array.delete_at(2) | |
=> 4 | |
>> array | |
=> [2, 3] |
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
>> [1, 2, 3].reverse | |
=> [3, 2, 1] | |
>> [1, 2, 3].rotate | |
=> [2, 3, 1] | |
>> [1, 2, 3].rotate(-2) | |
=> [2, 3, 1] |
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
>> ["a", "a", "b", "c", "b"].uniq | |
=> ["a", "b", "c"] |
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
>> [1, 2, 3].include?(2) | |
=> true | |
>> [1, 2, 3].include?("a") | |
=> false |
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
>> [1, 2, 3, 4, 5, 6].each { |num| puts num } | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 |
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
>> [1, 2, 3, 4, 5, 6].reverse_each { |num| puts num } | |
6 | |
5 | |
4 | |
3 | |
2 | |
1 | |
>> ["a", "b", "c", "d"].each_with_index do |letter, index| | |
>> puts "#{letter}: #{index}" | |
a: 0 | |
b: 1 | |
c: 2 | |
d: 3 |
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
>> our_array = [ "a", 1, 1.4567, false] |
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
>> odds = Array.new([1, 3, 5, 7]) | |
=> [1, 3, 5, 7] |
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 = Array.new(5) | |
=> [ nil, nil, nil, nil, nil] |
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.new(5, "a") | |
=> ["a", "a", "a", "a", "a"] | |
>> Array.new(3, 0) | |
=> [0, 0, 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
>> %W{1 2 3 4} | |
=> [1, 2, 3, 4] | |
>> %w{This is an array} | |
=> ["This", "is", "an", "array"] |
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
>> name = "meagan" | |
>> %W{My name is #{name}} | |
=> ["My", "name", "is", "meagan"] | |
>> %w{My name is #{name}} | |
=> ["My", "name", "is", "\#{name}"] |
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
>> months = [] | |
>> months[5] | |
=> nil | |
>> months[5] = "june" | |
=> [nil, nil, nil, nil, nil, "june"] |
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
>> [1, 2, 3, 4][-1] | |
=> 4 | |
>> ["a", "e", "i", "o", "u"][-2] | |
=> "o" | |
>> [1, 2, 3][-4] | |
=> nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment