mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-06 23:06:49 +02:00
Merge pull request #1011 from iamrahil/ruby
[ruby] Array and Hash specific functions
This commit is contained in:
@@ -11,6 +11,7 @@ contributors:
|
|||||||
- ["Ariel Krakowski", "http://www.learneroo.com"]
|
- ["Ariel Krakowski", "http://www.learneroo.com"]
|
||||||
- ["Dzianis Dashkevich", "https://github.com/dskecse"]
|
- ["Dzianis Dashkevich", "https://github.com/dskecse"]
|
||||||
- ["Levi Bostian", "https://github.com/levibostian"]
|
- ["Levi Bostian", "https://github.com/levibostian"]
|
||||||
|
- ["Rahil Momin", "https://github.com/iamrahil"]
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -169,6 +170,9 @@ array[1..3] #=> [2, 3, 4]
|
|||||||
# Add to an array like this
|
# Add to an array like this
|
||||||
array << 6 #=> [1, 2, 3, 4, 5, 6]
|
array << 6 #=> [1, 2, 3, 4, 5, 6]
|
||||||
|
|
||||||
|
# Check if an item exists in an array
|
||||||
|
array.include?(1) #=> true
|
||||||
|
|
||||||
# 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 }
|
||||||
@@ -188,6 +192,10 @@ new_hash = { defcon: 3, action: true }
|
|||||||
|
|
||||||
new_hash.keys #=> [:defcon, :action]
|
new_hash.keys #=> [:defcon, :action]
|
||||||
|
|
||||||
|
# Check existence of keys and values in hash
|
||||||
|
new_hash.has_key?(:defcon) #=> true
|
||||||
|
new_hash.has_value?(3) #=> true
|
||||||
|
|
||||||
# Tip: Both Arrays and Hashes are Enumerable
|
# Tip: Both Arrays and Hashes are Enumerable
|
||||||
# They share a lot of useful methods such as each, map, count, and more
|
# They share a lot of useful methods such as each, map, count, and more
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user