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

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

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

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

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

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1202

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T05:57:06+00:00 2024-11-25T05:57:06+00:00

$ruby_variables

  • 62k

Introduction

Coming from Javascript background, I found ruby variable declaration a bit simple. Simple in a bad way. I was introduced to declaring Ruby variables as:

variable_name = [1,2,3,4] 
Enter fullscreen mode Exit fullscreen mode

This may seem great. After all, there is no need to “memorize more syntax”; however, issues arise when code gets more complex.

Issues

When it comes to scoping. How was I sure that my variable was local and not global? Well easy enough right? I just had to make sure that I declared the variable outside a function to make it global and inside a function to make it local.

As I am writing this, I am realizing that the issues I have with ruby variable declarations is less functional and more syntactic. I would like to have a way to declare a variable and also the type of variable.

What I learned

As I was reading documentation for Classes and Objects in Ruby, I felt as if an angel had answered my prayers. There is more to meets the eye when it comes to declaring variables in Ruby.

In order to understand some of these variables you must be familiar with classes and that is what my next post is about. I will introduce them here and explain them more in depth on my next post.

1.Local variables

Local variables is what I was initially introduced to. To declare a local variable, you have two options. Declare the lowercase variable name within the block you want to access it in or place an underscore before the variable name.

_var_name = 1 
Enter fullscreen mode Exit fullscreen mode

2.Instance variables

Instance variables are limited to what an object itself refers to and different objects (class instances) of the same Class are allowed to have different values for the instance variable. It is declared with an @ prior to the variable name. Does this look familiar when using erb?

@var_name = 2 
Enter fullscreen mode Exit fullscreen mode

3.Class variables

A class variable is a static variable that persists throughout each instance of a class. If it gets modified in an instance it is modified in all instances. In order to declare this variable, you write @@ prior to your variable name.

@@var_name = 3 
Enter fullscreen mode Exit fullscreen mode

4.Global variables

Although declaring global variables is frowned upon for clouding the global namespace and causing unintended bugs. There is also a way to declare global variables in Ruby. Kind of similar to declaring local variables, if you want to declare a global variable just declare the lowercase variable name in the global scope. Luckily, we can also prepend the $ sign to visually see that it is a global variable.

$var_name = 4 
Enter fullscreen mode Exit fullscreen mode

Conclusion

Ruby variable declaration is not as simple as I thought. Understanding these different variable declarations have helped me understand the need for an @ sign in some of my previous code; as well as, understand Classes in a deeper level.

backenddevelopmentlearningrubywebdev
  • 0 0 Answers
  • 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

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

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

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

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.