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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:06:54+00:00 2026-06-15T00:06:54+00:00

Trying to learn Ruby, and in a tutorial, I came across this code on

  • 0

Trying to learn Ruby, and in a tutorial, I came across this code on this page:

class Greeter
  def initialize(name = "World")
    @name = name
  end
  def say_hi
    puts "Hi #{@name}!"
  end
  def say_bye
    puts "Bye #{@name}, come back soon."
  end
end

I am also told that the @name variable is an instance variable, and that it is scoped to ALL methods within the class (in this case, Greeter). I am familiar-ish with JS, and it is getting a bit confusing with regards to scoping. Let me clarify a bit. Here is my understanding of the code in JS:

function Greeter (name){
    name = (typeof name === "undefined") ? "World" : name;

    function say_hi (name){
        alert('Hi ' + this.name);
    };

    function say_bye (name){
        alert('Bye ' + this.name + ', come back soon.');
    };
};

So in the ruby example, it appears there is a method called initialize, and I guess in ruby, they get the luxury of defining variable within the parameter? In initialize it defines the var @name. In the JS example, I explicitly abstract the naming of the var name, by declaring this.name to mean the name WITHIN say_hi & say_bye. I understand this to be good coding practice, to differentiate the scoping, even though in this case name remains the same throughout the code, being either “World” if an empty parameter is passed, or the name of the person passed as a parameter. Either way, I am unsure of how the scoping works in Ruby, as I am not aware that this is possible in JS. Take the above JS code, and compare it to :

function Greeter (name){

    function say_hi (name){
        this.name = (typeof name === "undefined") ? "World" : name;
        alert('Hi ' + this.name);
    };

    function say_bye (name){
        this.name = (typeof name === "undefined") ? "World" : name;
        alert('Bye ' + this.name + ', come back soon.');
    };
};

If I failed to declare the this.name in the sub-methods, it could mean the Greeter class variable name, not the say_hi or say_bye variables name respectively.

Questions:

1 – is the @ in @name required to gain the capability to be visible to ALL methods within the class, or is this convention? If it is convention, what does @ signify? (I am equating this to a variable declared as $this in JQuery, where it signifies that the variable is a JQuery element, different from when you query by $('whatever'))

2 – Is the method initialize acting like a getter/setter method scoped within the Greeter class?

3 – Any other comments about your assumptions of my logic, code and questions are very welcome!

  • 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-15T00:06:55+00:00Added an answer on June 15, 2026 at 12:06 am

    @ is roughly the same as this in javascript, it refers to the current instance. The initialize method in a class is a constructor, not a getter/setter.

    function Greeter( name ) { 
        /*Functions called with `new` return objects
         with their proto link pointing to the object
         pointed at function's .prototype property at the time of instanciation. */
        this.name = arguments.length < 1 ? "World" : name; //No real way to do optional arguments
    }
    
    Greeter.prototype.say_hi = function() {
        alert( "Hi " + this.name + "!" ); //Methods are defined outside on the `.prototype` object
    };
    
    Greeter.prototype.say_bye = function() {
        alert( "Bye " + this.name + ", come back soon." );
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to learn Ruby on Rails using this Rails Tutorial. Earlier, I
In trying to learn about Ruby execution methods, I found this blog post on
I'm trying to learn to use Ruby on Rails (going through Hartl's tutorial for
I'm trying to learn Ruby but I geep getting this error: The error occurred
I'm trying to learn ruby on rails. I've been going through a tutorial, but
I am trying to learn Ruby and I have the following code a[0]={ :artist
I'm trying to learn Ruby as well as Ruby on Rails right now. I'm
Hello I'm trying to learn ruby blocks. But I have a trouble to get
I am trying to learn Ruby on Rails by playing around with stuff and
I am trying to learn Ruby on Rails and trying to write some of

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.