리액트 첫 걸음
Biweekly Lecture
2015-10-20
Ruby on Rails Korea
- ROR Lab. Season 4 -

ReactJS Beginner
리액트 자바스크립트를 처음 사용해봅니다.

해시(또는 연관배열)은 배열의 요소를 참조하듯 키에 해당하는 값을
가져오거나 대입할 수 있으나 중첩해서 사용할 수 없음
해시(또는 연관배열)은 배열의 요소를 참조하듯 키에 해당하는 값을
가져오거나 대입할 수 있으나 중첩해서 사용할 수 없음
plain_hash = {}
plain_hash[:key1] = :value1 # 가능
plain_hash[:parent][:child] = :value2
# => NoMethodError: undefined method `[]=' for nil:NilClasshash_with_default = Hash.new({}) # nil 대신 비어 있는 해시를 기본값
hash_with_default[:parent][:child] = :value2 # 가능
hash_with_default[:parent] # => { :child => :value2 }
hash_with_default[:parent][:second_child] = :value3
hash_with_default[:parent]
# => { :child => :value2, :second_child => :value3 }
# 그러나 hash_with_default 자체는 비어있음?
hash_with_default # => {}
hash_with_default.keys # => []
# 그런데 중첩하지 않고 쓰면 중첩하지 않은 키에 대해서만 보인다
hash_with_default[:key1] = :value1속임수처럼 보이지만 :parent 키로 빈 해시{}를 초기화했기 때문
재귀함수를 선언하듯이, 해시 기본값을 블록으로 넘겨준다
# 블록에는 파라미터가 2개이고 첫번째 파라미터로 해시 자체를 넘겨받을 수 있음
recursive_hash = Hash.new do |hash, key|
hash[key] = Hash.new
end
recursive_hash[:parent][:child] = :value2
recursive_hash
=> {:parent=>{:child=>:value2}}
# 그런데 한번 더 키를 중첩하면 또 nil의 [] 메소드가 없다고 에러발생
recursive_hash[:parent][:child][:grandchild] = :value3
# => NoMethodError: undefined method `[]=' for :value2:Symbol# 그러나 hash_with_default 자체는 비어있음?
hash_with_default # => {}
hash_with_default.keys # => []
# 그런데 중첩하지 않고 쓰면 중첩하지 않은 키에 대해서만 보인다
hash_with_default[:key1] = :value1해시 기본값에 대한 블록을 중첩해서 정의할 수도 있고 아래처럼 재귀함수처럼 해시자체를 블록에서 사용하듯이 기본값을 만드는 블록 자체도 다시 사용할 수 있음
default_proc = lambda do |hash, key|
hash[key] = Hash.new(default_proc)
end
nested_hash = Hash.new(&default_proc)
nested_hash[:parent][:child] = :value2
nested_hash
=> {:parent=>{:child=>:value2}}
# 그러나 좀 더 키를 중첩하면 또 에러 발생
nested_hash[:parent][:child][:grandchild] = :value3
# => NoMethodError: undefined method `[]=' for :value2:Symbol# 그런데 한번 더 키를 중첩하면 또 nil의 [] 메소드가 없다고 에러발생
recursive_hash[:parent][:child][:grandchild] = :value3
# => NoMethodError: undefined method `[]=' for :value2:Symbol
# 그렇다고 해시 기본값에 대한 블록안에서 중첩을 한번 더 만들어야 할까?
recursive_hash = Hash.new do |hash0, key0|
hash0[key0] = Hash.new do |hash1, key1|
hash1[key1] = Hash.new
end
end
recursive_hash[:parent][:child][:grandchild] = :value3 # 가능
recursive_hash[:parent][:child][:grandchild][:another_deapth] = :value4
# 에러발생, 중첩 횟수가 늘어날때마다 기본값을 만드는 코드의 끝은 없다
중첩하는 키에 대해 빈 해시를 만들어서 해시를 처리할 때 empty? 로
참,거짓을 구분해야 한다. 루비에서 nil은 거짓이지만 빈 해시는 참이라는 점을 주의
default_proc = lambda do |hash, key|
hash[key] = Hash.new(default_proc)
end
# 해시 자체의 default_proc을 다시 사용
nested_hash2 = Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) }
nested_hash2[:parent][:child][:grandchild][:another_depth] = :value4
nested_hash2
=> {:parent=>{:child=>{:grandchild=>{:another_depth=>:value4}}}}
nested_hash2[:parent][:child][:grandchild][:theother_depth]
nested_hash2
=> {:parent=>{:child=>{:grandchild=>{:another_depth=>:value4, :theother_depth=>{}}}}}https://rubygems.org/gems/bottomless_hash
https://gist.github.com/firedev/9de91e245f70c2e963e4
http://firedev.com/posts/2015/bottomless-ruby-hash/
리액트 입문
By wagurano
리액트 입문
리액트 자바스크립트 프레임워크를 처음 배웁니다.
- 1,239