0

I am learning ruby and I am specifically playing with OOPS in it. I am trying to write equivalent of this PHP code in ruby

    class Abc {
        $a = 1;
        $b = 4;
        $c = 0;

        function __constructor($cc) {
            $this->c = $cc
        }

        function setA($v) {
            $this->a = $v
        }

        function getSum() {
            return ($this->a + $this->b + $this->c);
        }
    }

    $m = new Abc(7);
    $m->getSum(); // 12

    $m->setA(10);
    $m->getSum(); // 21

I am trying to write equivalent of above PHP to ruby. Please note my goal is to have default values of soem of the class variable and if I want to override it, then I can do it by calling getter/setter method.

    class Abc 
        attr_accessor :a
        def initialize cc
            @c = cc
        end

        def getSum 
            #???
        end

    end

I don't like to do

Abc.new(..and pass value of a, b and c)

My goal is to have default values, but they can be modified by instance, if required.

2
  • 1
    The Ruby code you've written isn't valid Ruby. Commented Nov 20, 2012 at 2:44
  • There are also no class variables in your code. Commented Nov 20, 2012 at 3:53

1 Answer 1

3
class Abc
  attr_accessor :a, :b, :c 

  def initialize a = 1, b = 4, c = 0 
    @a = a
    @b = b
    @c = c
  end

end

This will accept 1, 4, and 0 respectively as default values, but they can be overridden by passing in parameters.

So if you do example = Abc.new without paramaters it will have default values of 1,4,0 but you could do:

     example2 = Abc.new 5, 5 

without passing a value for c and you'd have values of a = 5 and b = 5 with by default c = 0 still.

More broadly, in your Ruby code examples above, you are using brackets where not needed. a def method_name begins a block, and a end will finish it. They serve in place of how brackets are traditionally used in other languages. So for your method getSum you can simply do

def get_sum
  #your_code
end

Also, note def getSum (camelCase) would typically be def get_sum (snake_case) in Ruby. Also note in the examples I give above that parenthesis are dropped. They are not needed in Ruby.

Sign up to request clarification or add additional context in comments.

4 Comments

Abc.new(a = 5, b = 5) is a little misleading. While you can evaluate code and even assign variables in method parameters, these are not in the same scope as the variables in the method definition (try switching it to a = 5, c = 5 to see what I mean). Ruby 2.0 will have keyword arguments similar to this, but for 1.9 and below, the arguments need to be given in the order they were declared. You can also mimic this behavior with an options hash.
@joofsh: And if you meant named parameters, you gotta wate for Ruby 2.0, which will be out very soon.
@BorisStitnicky It must have been on my mind, but yea I meant to answer for 1.9 and below. It's been changed.
However this may look ugly once you have many instance variables and if I want to setup the default value for them def initialize a = 1, b = 4, c = 0 , k =2, l=3, m=4, o=5, p=3, q=4, r=5

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.