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 8565921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:32:22+00:00 2026-06-11T17:32:22+00:00

I was looking through the docs for instance_variable_set and saw that the sample code

  • 0

I was looking through the docs for instance_variable_set and saw that the sample code given does this:

obj.instance_variable_set(:@instnc_var, "value for the instance variable")

which then allows you to access the variable as @instnc_var in any of the class’s instance methods.

I’m wondering why there needs to be a colon : before the @instnc_var. What does the colon do?

  • 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-11T17:32:23+00:00Added an answer on June 11, 2026 at 5:32 pm

    My first instinct is to tell you not to use instance_variable_set unless you really know what you are using it for. It’s essentially a tool for metaprogramming or a hack to bypass visibility of the instance variable.

    That is, if there is no setter for that variable, you can use instance_variable_set to set it anyway. It’s far better if you control the code to just create a setter.

    Try looking at accessors for the vast majority of your instance variable setting needs. They are an idiomatic way to have getters and setters for your instance variables “for free” without you having to write those functions.

    If you really do need instance_variable_set, it allows the first argument to be a symbol which is the name of the instance variable to set. A colon is part of the Ruby language that is like a “symbol literal”: when you type :foo, you’ve created a symbol with value foo, just like when you type "bar" directly into your code, you create a string literal with value “bar”.

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

Sidebar

Related Questions

While looking through some old code I came across this gem: MyObject o =
I have been going through the docs and source code looking for something without
I'm looking for sample code on how to sync with ical through sync services.
After looking through Paypal docs for a solution for this circumstance, I came up
I was looking through the docs and stumbled onto mysql_real_escape_string() and I'm not understanding
Looking through a few SQL implementations, I noticed that most DBMSs support defining an
Looking through the documentation for CFDataRef I can't see anything that will compress a
Looking through the massive android source code, anyone on here who know where exactly
After looking through the docs on App Engine and the StringListProperty or the ListProperty
I'm looking through the docs for the jQuery UI Datepicker ( http://jqueryui.com/demos/datepicker/ ), but

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.