在Ruby中分配类变量时可以调用实例方法吗?

时间:2016-09-29 15:23:35

标签: ruby-on-rails ruby methods

我有一些方法包含许多相同逻辑的不同组合。为了清理它,我想只定义一次测试。

WITH RECURSIVE recCTE() AS
(
    /*Recursive Seed - The start of the recursive lookup*/
    SELECT
        master_id as parent,
        slave_id as child,
        /*You can use "depth" to check how deep we are in the master/slave hierarchy*/
        1 as Depth,
        /*You can use a "path" to see which people/nodes are involved in the hierarchy as it's built through the iterations of the recursive CTE*/
        CAST(master_id || '>' || child as VARCHAR(50)) as path
    FROM
        Connections
    WHERE
        /* here we determine who we are starting with for the lookup. You could start with everyone by omitting this*/
        /* We'll start with Susie */
        master_id = 3


    /*
        Recursive Term - The part of the query that refers to itself and iterates until
        the inner join fails
    */
    SELECT
        recCTE.child as parent,
        connections.slave_id as child,
        recCTE.depth + 1 as depth,
        recCTE.path || '>' || connections.slave_id as path
    FROM
        recCTE /*referred to itself here*/
        INNER JOIN connections ON
            recCTE.child = connections.master_id /*Join child to master for next lookup of slave/child */
    WHERE
        /*safe guard in case of endless cycling (A reporting to B reporting to C reporting back to A)*/
        recCTE.Depth < 15 

        /*besides checking for depth, you could also insure that the slave doesn't exist in the path already*/
        recCTE.path NOT LIKE '%' || slave_id || '%'


)

/*Now select from it and see what you get*/
SELECT * FROM recCTE;

这样可以正常工作,但我试图将其从aux方法中拉出来用于其他方法。

class Sentence < ApplicationRecord

#Gathers options hash for sentence
def options
    { 
        pronoun: subject.pronoun,
        ...
        }
end

#gives auxiliary verb based on sentence options 
def aux
    third_person = ["he", "she", "it"].include?(options[:pronoun])

    aux = "does" if third_person #just an example 
    ...
end
...

有谁知道,我缺少什么?

1 个答案:

答案 0 :(得分:1)

您没有在类Sentence的实例上调用实例方法options

你必须这样称呼它:

sentence = Sentence.new
['he', 'she', 'it'].include?(sentence.options[:pronoun])