在类

时间:2015-06-15 22:02:28

标签: ruby proc

我正在努力调试我的问题;我试图在一个方法中使用Proc,所有都在一个类中。当我没有输入"类"细节,我的程序编译好;但是,当我指定类名时,我会遇到各种不同的错误。

注意:我在CodeReview.StackExchange.com上发布了这个问题,但它在那里是偏离主题的; mods(?)推荐我在这里问这个问题。

背景。以下是我给出的提示,直接来自pine.fm

  

祖父时钟。编写一个方法,它接受一个块,并在今天过去的每个小时调用一次。那样的话,如果我要通过这个区块,那就去吧!' DING!'结束时,它会像一个祖父钟钟声(有点像)。用几个不同的块(包括我刚刚给你的块)测试你的方法。提示:您可以使用Time.now.hour来获取当前小时。但是,这会返回0到23之间的数字,因此您必须更改这些数字才能获得普通的时钟面数(1到12)。

有效的代码:

def hourlyRing ring
    time = Time.now.hour%12;
    hour_set =[12, 1, 2, 3, 4, 5, 6, 7, 8 , 9 , 10, 11];
    (hour_set[time].to_i).times do 
        ring.call
    end
end

ring = Proc.new do 
    puts 'DING!'
end 

hourlyRing ring

它产生以下内容(正确,因为它在这里下午4点)输出:

~$ ruby GrandfatherClock.rb
DING!
DING!
DING!
DING!

无效的代码:

class GrandfatherClock  

    def hourlyRing ring
        time = Time.now.hour%12;
        hour_set =[12, 1, 2, 3, 4, 5, 6, 7, 8 , 9 , 10, 11];
        (hour_set[time].to_i).times do 
            ring.call
        end
    end

    ring = Proc.new do 
        puts 'DING!'
    end 
end

# Here's where I try to call it:
ringtime = GrandfatherClock.new
ringtime.hourlyRing ring

任何人都可以在我的代码中看到错误吗?在我的拙见中,这是一个足够短的代码,但我无法理解它为什么不能编译。也许我从根本上误解了有关Procs的东西,但我无法理解究竟是什么。提前谢谢。

2 个答案:

答案 0 :(得分:2)

这是关于背景的 在第二个示例中,您将ring proc放在类中,因此当您实际调用ringtime.hourlyRing ring时,未在该范围内定义ring。

您可以将代码更改为:

class GrandfatherClock  

    def hourlyRing ring
        time = Time.now.hour%12;
        hour_set =[12, 1, 2, 3, 4, 5, 6, 7, 8 , 9 , 10, 11];
        (hour_set[time].to_i).times do 
            ring.call
        end
    end
end

ring = Proc.new do 
   puts 'DING!'
end 

# Here's where I try to call it:
ringtime = GrandfatherClock.new
ringtime.hourlyRing ring

此外,问题陈述明确询问了一个块, 所以hourlyRing应该是这样的:

def hourlyRing
    time = Time.now.hour%12;
    hour_set =[12, 1, 2, 3, 4, 5, 6, 7, 8 , 9 , 10, 11];
    (hour_set[time].to_i).times do 
        yield
    end
end

你会这样称呼:

hourlyRing { puts 'RING'}

 hourlyRing do
    puts 'RING'
 end

答案 1 :(得分:1)

在第一个示例中,您已将proc分配给类外的变量ring,因此当您引用变量ring时,变量ring在范围内(即可见)。

在第二个示例中,尽管您仍然将proc分配给名为GrandfatherClock的变量,但该变量现在在class GrandfatherClock def hourlyRing ring time = Time.now.hour % 12 hour_set = [12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] (hour_set[time].to_i).times do ring.call end end def ring Proc.new do puts 'DING!' end end end # Here's where I try to call it: ringtime = GrandfatherClock.new ringtime.hourlyRing ringtime.ring 类中声明,因此在该类之外不再可见。< / p>

不是将proc分配给变量,而是可以创建一个返回proc的函数:

ring

请注意,您现在必须通过班级的ringtime实例引用yield

然而,这并不是问题描述所要求的(我只是用它来说明发生了什么)。为了更好地与问题描述保持一致,您需要在hourlyRing函数中使用ring.call代替ringtime.hourlyRing do puts 'DING!' end ,然后在方法调用中传递一个块:

    .modalDialog {
        position: fixed;
        font-family: Arial, Helvetica, sans-serif;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        background: rgba(0, 0, 0, 0.8);
        z-index: 99999;
        opacity:0;
        -webkit-transition: opacity 400ms ease-in;
        -moz-transition: opacity 400ms ease-in;
        transition: opacity 400ms ease-in;
        pointer-events: none;
    }

    .modalDialog:target {
        opacity:1;
        pointer-events: auto;
    }

    .modalDialog > div {
        width: 800px;
        position: relative;
        margin: 10% auto;
        padding: 5px 20px 13px 20px;
        /*border-radius: 10px;*/
        background: #fff;
        /*background: -moz-linear-gradient(#fff, #999);
        background: -webkit-linear-gradient(#fff, #999);
        background: -o-linear-gradient(#fff, #999);*/
    }

    .close {
        background: #606061;
        color: #FFFFFF;
        line-height: 25px;
        position: absolute;
        right: -12px;
        text-align: center;
        top: -10px;
        width: 24px;
        text-decoration: none;
        font-weight: bold;
        -webkit-border-radius: 12px;
        -moz-border-radius: 12px;
        border-radius: 12px;
        -moz-box-shadow: 1px 1px 3px #000;
        -webkit-box-shadow: 1px 1px 3px #000;
        box-shadow: 1px 1px 3px #000;
    }

    .close:hover {
        background: #00d9ff;
    }