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

  • SEARCH
  • Home
  • 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 7158277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:02:19+00:00 2026-05-28T13:02:19+00:00

In a class definition, what is the difference between these two methods? def func(var)

  • 0

In a class definition, what is the difference between these two methods?

def func(var)
...
end

def func=(var)
...
end

Is there any, or is one of them not valid?

  • 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-05-28T13:02:20+00:00Added an answer on May 28, 2026 at 1:02 pm

    To explain some things about reader/writer AKA getter/setter methods in Ruby:

    Ruby doesn’t force us to use = in the method definition for a setter. We get to choose whether the method has one.

    Consider this:

    class Foo
    
      # automagically creates:
      #   .v
      #   .v=
      attr_accessor :v
    
      def initialize(v)
        puts "inside initialize(#{ v })"
        @v = v
      end
    
      def setter(v)
        puts "inside setter(#{ v })" 
        @v = v 
      end
    
      def setter=(v) 
        puts "inside setter=(#{ v })"
        @v = v 
      end
    
    end
    
    f = Foo.new(1)
    puts f.v
    
    f.setter(2)
    puts f.v
    
    f.setter = 3
    puts f.v
    
    f.setter=(4)
    puts f.v
    
    f.v = 5
    puts f.v
    
    f.v=(6)
    puts f.v
    

    Running the code outputs:

    inside initialize(1)
    1
    inside setter(2)
    2
    inside setter=(3)
    3
    inside setter=(4)
    4
    5
    6
    

    The = is simply another letter in the method name because Ruby is smart enough to know if it sees f.setter = 3 it should use the setter=(v) method.

    Ruby doesn’t force using = to set a variable, you can decide if it makes more sense to you when you define the method. It is idiomatic that we use = because it helps make a setter look like an assignment, removing the urge to name all the setters something like set_v(v).

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

Sidebar

Related Questions

If so, what is the difference between the two? Are there actually two separate
I don't know if there's a difference between Josh Smith's and Laurent Bugnion's implementations
Overview I'm looking to analyse the difference between two characters as part of a
Lately I've been wondering if there's a difference between initializing the variables that have
Possible Duplicates: *.h or *.hpp for your class definitions What is the difference between
as the title says, what is the difference between @var and @@var in a
Say I have a class definition: class CustomClass { int member; }; Why is
A random class definition: class ABC: x = 6 Setting some values, first for
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
Session contains objects whose class definition isn\'t available. Remember to require the classes for

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.