Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.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
Arrays 在Ruby中用数组初始化方法_Arrays_Ruby_Methods_Initialization_Arguments - Fatal编程技术网

Arrays 在Ruby中用数组初始化方法

Arrays 在Ruby中用数组初始化方法,arrays,ruby,methods,initialization,arguments,Arrays,Ruby,Methods,Initialization,Arguments,我是ruby新手,我尝试将数组放入initialize方法中,但它不是这样工作的,所以如何使用此参数放入数组?谢谢 class User attr_accessor :name, :friends def initialize(name, friends) @name = name @friends = friends end def friendNbr return friends.count end

我是ruby新手,我尝试将数组放入initialize方法中,但它不是这样工作的,所以如何使用此参数放入数组?谢谢

class User
    attr_accessor :name, :friends

    def initialize(name, friends)
        @name = name
        @friends = friends
    end

    def friendNbr
        return friends.count
    end

    def isFriendWith(value)
        friends.each do |user|
            if (user.name == value)
                return "Yes, #{name} is friend with #{user.name}"
            end
        end

        return "No, #{name} is not friend with #{value}"
    end
end

jane = User.new("Jane", [boris, francois, carlos, alice])

bob = User.new("Bob", [jane, boris, missy])

alice = User.new("Alice", [bob, jane])


# bob.isFriendWith("Jane")
# jane.isFriendWith("Alice")
# alice.isFriendWith("Carlos")

您有多种解决问题的方法:

  • 第一个
    friends
    可以是名称数组(字符串)
  • 另一种方法是像传递对象一样传递对象,但在这种情况下,只能传递已实例化的对象。在第二个选项中,您可以添加一个方法
    addFriend
    ,首先创建
    User
    ,然后添加他们
    friends

您的方法存在一个循环问题:您不能在创建用户之前传递它。例如,在创建
alice
之前,将
alice
作为朋友传递给
jane
——这不起作用。你必须首先创建用户,一旦用户被创建,你就可以定义友谊。。。。顺便问一下,
bob.isFriendWith(“Jane”)
是否意味着
Jane.isFriendWith(“bob”)
?换句话说,友谊是相互的还是片面的?“它不是那样工作的”不是一个足够精确的错误描述,我们无法帮助你。什么不起作用?它怎么不起作用?你的代码有什么问题?你收到错误信息了吗?错误消息是什么?你得到的结果不是你期望的结果吗?你期望得到什么样的结果?为什么?你会得到什么样的结果?两者有什么不同?你观察到的行为是否不是期望的行为?期望的行为是什么,为什么,观察到的行为是什么,它们有什么不同?感谢您的回答,首先很抱歉,因为我没有描述我的问题/错误,是的,问题来自未分配的变量,我很困惑,因为我用DB/SQL方法编码,这就是为什么它不起作用,谢谢
class User
    attr_accessor :name, :friends

    def initialize(name, friends)
        @name = name
        @friends = friends
    end

    def friendNbr
        return friends.count
    end

    def isFriendWith(value)
        friends.each do |friend_name|
            if (friend_name == value)
                return "Yes, #{name} is friend with #{friend_name}"
            end
        end

        return "No, #{name} is not friend with #{friend_name}"
    end
end

jane = User.new("Jane", ["Boris", "Francois", "Carlos", "Alice"])

bob = User.new("Bob", ['Jane', 'Boris', 'Missy'])

alice = User.new("Alice", ['Bob', 'Jane'])


bob.isFriendWith("Jane")
jane.isFriendWith("Alice")
alice.isFriendWith("Carlos")
class User
    attr_accessor :name, :friends

    def initialize(name, friends)
        @name = name
        @friends = friends
    end

    def friendNbr
        return friends.count
    end

    def isFriendWith(value)
        friends.each do |user|
            if (user.name == value)
                return "Yes, #{name} is friend with #{user.name}"
            end
        end

        return "No, #{name} is not friend with #{value}"
    end

    def addFriend(...)
      ...
    end

end

jane = User.new("Jane", [])

bob = User.new("Bob", [jane])

alice = User.new("Alice", [bob, jane])


bob.isFriendWith("Jane")
jane.isFriendWith("Alice")
alice.isFriendWith("Carlos")