Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 缩进行(意外行为)_Ruby_Indentation - Fatal编程技术网

Ruby 缩进行(意外行为)

Ruby 缩进行(意外行为),ruby,indentation,Ruby,Indentation,我想写一行递归缩进的代码。我只是在研究一种使用全局tab变量的方法。因此,我使用了以下代码: @@tab = ' ' @@tab_add = ' ' @@nl = "\n" def helper left = @@tab right = @@tab @@tab = @@tab + @@tab_add str = '<hello>' + @@nl + left + helper2 + right + @@nl + '</hello>'

我想写一行递归缩进的代码。我只是在研究一种使用全局tab变量的方法。因此,我使用了以下代码:

@@tab = '  '
@@tab_add = '  '
@@nl = "\n"

def helper
    left = @@tab 
    right = @@tab
    @@tab = @@tab + @@tab_add
    str = '<hello>' + @@nl + left + helper2 + right + @@nl + '</hello>'
end 

def helper2
    left = @@tab 
    right = @@tab
    @@tab = @@tab + @@tab_add
    str = '<goodbye>' + @@nl + left + helper3 + right + @@nl + '</goodbye>' 
end 

def helper3
    str =  'dawg'
end
我明白了

<hello>
   <goodbye>
     dawg
</goodbye>
</hello>

老兄

但是为什么结束再见标签没有缩进呢?我将其保存为“helper”函数中的“right”局部变量。谢谢你的帮助

helper2方法输出一个字符串,其中包含:

@@nl + '</goodbye>'
@@nl+'

换行符和结束标记之间不可能有任何缩进。您需要一个递归方法

@@tab  = '  '
@@nl   = "\n"
indent = 0

def helper3
    str =  'dawg'
end

def indented(p_indent, p_string='')
    @@tab * p_indent + p_string
end

def write_indented(p_indent, p_array)
    return indented(p_indent, helper3) if p_array.empty?
    head, *tail = p_array
    indent = indented(p_indent)
    indent + '<' + head + '>' + @@nl + 
            # recursive call :
        write_indented(p_indent + 1, tail) +
        @@nl + indent + '</' + head + '>'
end

tags = %w[hello goodbye]
puts write_indented(indent, tags)
@@tab=''
@@nl=“\n”
缩进=0
def助手3
str='dawg'
结束
def缩进(p_缩进,p_字符串=“”)
@@制表符*p_缩进+p_字符串
结束
def写入缩进(p_缩进,p_数组)
如果p_array.empty,是否返回缩进(p_缩进,helper3)?
头,*tail=p_数组
缩进=缩进(p_缩进)
缩进+“”+@@nl+
#递归调用:
写入缩进(p\u缩进+1,尾部)+
@@nl+缩进+“”
结束
标签=%w[你好,再见]
使写入缩进(缩进、标记)
执行(ruby 1.8.6):

$ruby-w t.rb
老兄
不幸的是,递归在大多数情况下是用阶乘或斐波那契函数来解释的,这使得它更难理解。
如果您通过一系列调用来表示递归,那么它实际上非常简单:

write_indented(0, ['hello', 'goodbye'])
    ...
    call write_indented(1, ['goodbye'])
       |
       |
       +--> ...
            call write_indented(2, [])
               |
               |
               +--> return if stop condition
                        |
               +<-------+
       +<------+ return   
       |
  <----+ returned value
end
write_缩进(0,['hello','bye'])
...
调用write_缩进(1,['拜拜])
|
|
+--> ...
调用写入缩进(2,[])
|
|
+-->在停止条件下返回
|

+你知道
@
变量不是“全局变量”,它们是“类变量”,对吗?全局变量是
$
变量。是的,“类变量”和“全局”变量一样邪恶
$ ruby -w t.rb 
<hello>
  <goodbye>
    dawg
  </goodbye>
</hello>
write_indented(0, ['hello', 'goodbye'])
    ...
    call write_indented(1, ['goodbye'])
       |
       |
       +--> ...
            call write_indented(2, [])
               |
               |
               +--> return if stop condition
                        |
               +<-------+
       +<------+ return   
       |
  <----+ returned value
end