레일스의 *args
배열에 조건부로 해시를 추가하려면 어떻게해야합니까? 나는 원래 가치가 있다면 그것을 밟기를 원하지 않는다.* args에 해시를 추가하려면 어떻게해야합니까?
def foo(*args)
# I want to insert {style: 'bar'} into args but only if !style.present?
bar(*args) # do some other stuff
end
내가 레일에 의해 제공되는 extract_options 및 reverse_merge 방법을 사용하기 시작했습니다 :
def foo(*args)
options = args.extract_options! # remove the option hashes
options.reverse_merge! {style: 'bar'} # modify them
args << options # put them back into the array
bar(*args) # do some other stuff
end
그것은 작동을하지만 자세한 보인다 예를 들어
, 나는 배열을받는 방법이 별로 루비가 아닙니다. 나는 뭔가를 놓친 것 같은 기분이 든다.
'args' 목록에 다시 넣어야하는 이유가 있습니까? 처음 두 개는 Rails 소스에서 흔히 볼 수 있습니다. –
좋은 질문입니다. 예, 함수'foo'는 args를 다른 함수로 넘깁니다. 나는 그것을 보여주기 위해 질문을 편집 할 것이다. 저는 FormBuilder 클래스를 작성하고 있습니다. 다른 헬퍼를 호출하는 많은 헬퍼가 있습니다. – IAmNaN