메쏘드를 반환 할 때 가끔씩 탭을 사용하기를 좋아합니다.재귀 함수의 탭 동작
def render
"Name: #{name}\n".tap do |res|
children.each do |child|
res += " - " + child.render + "\n"
end
end
end
:
class Node
attr_accessor :name, :children
def initialize(name); self.name, self.children = name, []; end
def render
res = "Name: #{name}\n"
children.each do |child|
res += " - " + child.render + "\n"
end
res
end
end
parent = Node.new('Parent')
parent.children = [Node.new('Child')]
puts parent.render
반환
Name: Parent
- Name: Child
을 내가 탭을 사용하여 렌더링 기능을 변경하는 경우 : 재귀 함수와 탭을 사용할 때 내가 무엇을 기대하지만, 그것은 행동이 다른가요
돌아 가기
Name: Parent
이 동작은 첫 번째 렌더링 함수와 동일하다고 가정합니다. 문서는 "x를 블록에 출력하고 x를 반환합니다"라고 표시합니다 ... 함수가 반복적으로 함수 스택을 오염시키는 것이므로?
Ruby의'String's은 매우 가변적입니다. '''','''','''',''연결'',''preby'',''setbyte'',''force_encoding'',''insert'',''replace' 등 많은 돌연변이 방법이 있습니다. chop!','delete!','downcase!','upcase!','encode!','lstrip!'과 같은 것들을 포함하고 있습니다. ''! ',''!'''''''''''''''''''''! ,'tr_s! '. –
문자열은 불변하지 않습니다. 그러나 할당 연산자를 사용할 때마다 실제로'='의 왼쪽에있는 객체를 변경하여 객체의 값을 변경하지 않습니다. –