-1
\$\begingroup\$

I am trying to write a Person class in Ruby which has some methods and properties. The below is how I have implemented it now.

class Person
  attr_accessor :name, :gender, :mother, :spouse

  def initialize(name, gender, mother = nil)
    @name = name
    @gender = gender
    @mother = mother
    @spouse = nil
  end

  def add_spouse(spouse)
    if spouse
      @spouse = spouse
      spouse.spouse = self
    end
  end
  
  def father(self)
    return [self.mother.spouse]
  end
end

I want to access the methods like this:

m = Person.new('mom', 'Female')
d = Person.new('dad', 'Male')

d.add_spouse(m)

p = Person.new('Jack', 'Male', m)

If I want to get the father of p, then I want to get it like this: p.father.

So is the above implementation correct?

In python if I want a class method as a property then @property is used. In Ruby is there anything like that or is the above method for father property correct? Also I have seen examples with self.some_variable and @some_variable in Ruby. Here name and gender variables are defined at the object creation and never changed but other variables like mother, children can be nil initially and can be added by some other external method. For eg: By a method add_member in class Family. So for that is the above definition correct?

\$\endgroup\$
4
  • \$\begingroup\$ This question if off-topic on Code Review, as per the help center. Code Review is for review of working code, so the question "is this code correct" is off-topic. Besides that, the code has a trivial SyntaxError, it isn't even legal Ruby. You might get better help on Stack Overflow, but please make sure to produce a minimal reproducible example. In particular, the SyntaxError can be trivially reproduced in one line, it doesn't require 22 lines of code to show. \$\endgroup\$ Aug 16, 2020 at 10:05
  • \$\begingroup\$ "So is the above implementation correct?" Did you test it? Please see our help center. \$\endgroup\$
    – Mast
    Aug 17, 2020 at 14:11
  • \$\begingroup\$ I had put self in def father initially. Then I changed it in my code but while typing the question I mistyped it. I have changed it now. \$\endgroup\$
    – Lax_Sam
    Aug 17, 2020 at 15:11
  • 1
    \$\begingroup\$ Please do not update the code in your question after receving answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. Next time, make sure your first revision is the right one. \$\endgroup\$
    – Mast
    Aug 17, 2020 at 15:30

1 Answer 1

3
\$\begingroup\$

Your code has an error in the definition of father. In ruby, there is no need to add self as a parameter. In addition you return an array with the mother. This may be correct under some legal systems, but I think you didn't want to implement this..

It is fine, if you use:

def father()
  return self.mother.spouse
end

Then you can use it as son.father.


Ruby defines instance variables as @varname, you can use them inside the class.

If you want to have access from outside, you must define accessors (setter, getter or both). From outside you don't see a difference between accessors and "normal" methods. You call a method and get a result.

Inside a class I recommend to use the accessor if it is defined. If you ever replace the standard accessor with a programm logic, then you don't need to adapt the code.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Most coding styles would just write def father, without the parentheses, instead of def father() and just mother.spouse instead of return self.mother.spouse, neither the return nor the self are necessary here \$\endgroup\$ Aug 16, 2020 at 18:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.