Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8855005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:01:18+00:00 2026-06-14T14:01:18+00:00

I am learning ruby and I am specifically playing with OOPS in it. I

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:01:19+00:00Added an answer on June 14, 2026 at 2:01 pm
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning Ruby and found this code sample in some documentation: require 'find'
I learning ruby singletons and have misunderstanding with such code: class MyClass def self.class_singleton_mymethod
I'm just learning ruby and trying to understand the scope of code executed in
I am learning Ruby and I set up a controller like this: class Mobile::HomeController
I've just started learning ruby, so this question is simple. I created @subject in
I'm currently learning Ruby and RoR and I stumbled across this declaration: link_to_remote(name, options
I am learning Ruby on Rails so I'm sure I'll find this out sooner
Learning ruby on rails, playing around with routes and path methods. I have built
I'm currently learning Ruby and I saw this declaration: the_count = [1, 2, 3,
I recently started learning ruby, and I understood that you coud use code blocks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.