@buildwithallan@Emp_Sel@felix_asante We’re reading and making notes, discussions, questions on The Well-Grounded Rubyist here to learn Ruby. Please jot here the most important things you learned for each chapter and add questions if you have them. Let’s keep everything in this thread.
Okay, let’s start with variables. In the book, I learn’t the 4 types of variables.
local
instance
class
global
Local variables start with a lower case letter or an underscore. Eg fruit, first_name. Also, when composing a variable with multiple names we should use underscore rather than camel case. So we use first_name rather than firstName
instance variables start with @. Eg, @last_name
class variables start with two at(@@) signs. eg @@total
global variables start with a dollar sign ($). eg $population
It seems the one I’ve got doesn’t even have all the chapters.
Take your time. Chapter 1 is very light, but I want to make mention of the fact that in Ruby, we don’t use class variables much, it’s good you learn about it but in practice it has problems and we don’t use it professionally, just keep it in mind. You might want to read the first answer here why class variables are bad.
Oh ok Thanks. what of global variables? I learned from odinproject that we shouldn’t use it. My question is will there ever be the need to use it? and when should we use it
Global variables are generally a bad idea. To put it simply if the variable is global, it means anything can access it right? Since it’s also a variable, any object or process at all can change it any time, doesn’t make any sense. Instead of a global variable, you could go for a global constant instead and possibly freeze it too. I’ve never used a global variable before, and never seen anyone use it in production. Ruby itself uses global constants instead.
Hello, I am back. I was having internet issues. It’s been sorted out. Yes please on chapter 2. I am taking my time to read it well. Also, I am building my portfolio alongside lol