Array & Hash
Array
array1=[1,2,3]
array2=[true,2,"a",[1,2,3,4]]
๋ฐฐ์ด์๋ ๋ค์ํ ํํ์ ๋ฐ์ดํฐ ํ์ (boolean, number, string, array)์ ๊ฐ์ด ์ธ ์ ์๋ค.
- ๋ฐฐ์ด์ ์ ๊ทผ๋ฐฉ๋ฒ
index๋ฅผ ํตํด์ ์ ๊ทผ ํ ์ ์๋ค! ๋ฐฐ์ด์ index๋ 0๋ถํฐ ์์ํด ๋ฐฐ์ด์ ํฌ๊ธฐ-1 ๊น์ง๊ฐ ๋ฒ์์ด๋ค.
arr=[1,2,3,4,5]
1
2
3
4
5
arr[0]
arr[1]
arr[2]
arr[3]
arr[4]
arr = [1,"a",3,"b",true]
#=> [1, "a", 3, "b", true]
arr
#=> [1, "a", 3, "b", true]
arr[0]
#=> 1
arr[4]
#=> true
Hashes
: a collection of key-value pairs
hash ์์ฑ๋ฐฉ๋ฒ
hash = {
key1 => value1,
key2 => value2,
key3 => value3
}
=>
hash rocket๋ฅผ ์ด์ฉํด์ key๋ฅผ ์ง์ ํด์ค๋ค.
my_hash = Hash.new
my_hash["key"] = "value"
# Hash๋ ๋ฐ๋์ H๊ฐ ๋๋ฌธ์์ฌ์ผํ๋ค.
# default value ์ง์
h=Hash.new(โdefault valueโ)
hash value์ ์ ๊ทผํ๋ ๋ฐฉ๋ฒ
hash = "key"
Symbol(:)
"string" == :string # false
arenโt strings
thereโs only one copy of any particular symbol at a given time
์ฃผ๋ก hash์ ํค๋ก ๋ง์ด ์ฌ์ฉ๋๋ค.
sounds = { :cat => "meow", :dog => "woof", :computer => 10010110, }
๋ง๋ค์ด์ง๋ฉด ๋ฐ๊ฟ ์ ์๋ค.
Only one copy of any symbol exists at a given time, so they save memory;
Symbol-as-keys are faster than strings-as-keys because of the above two reasons.
hash rocket(=>)์์ ๋ฌธ๋ฒ์ด ๋ฐ๋
new_hash = {
key1: value1,
key2: value2,
key3: value3
}
Last updated
Was this helpful?