0

im new with ruby and i tried to put an array into initialize method but its not work like that, so how to put an array with this argument ? thanks

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")
4
  • 1
    There's a cyclic problem with your approach: you can't pass a user before creating it. For example, you pass alice as a friend to jane before creating alice – that doesn't work. You have to create the users first and once they are created, you can define the friendships. Commented Apr 7, 2020 at 15:34
  • 1
    ... BTW, does bob.isFriendWith("Jane") imply jane.isFriendWith("Bob")? In other words, is friendship something mutual or can it be one-sided? Commented Apr 7, 2020 at 15:35
  • 1
    "its not work like that" is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? Commented Apr 7, 2020 at 15:56
  • Thank you for your answers, first sorry because i didn't describe my problem / error, yes the problem comes from the variable was not assigned, I am so confused because im coding with an DB/SQL approch, that's why it didn't work, thanks Commented Apr 9, 2020 at 8:47

1 Answer 1

2

You have multiple ways of solving your problem :

  • First friends could be array of names (string)
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")
  • Other way is to pass object as you did but in this case you can only pass object already instanciated. In this second choice you can add a method addFriend and first create User and then add them friends.
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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.