"Well-Grounded Rubyist" Listing 5.6: Keeping track of car manfacturing stats with class variables

Hi guys,

Can someone explain line by line what is happening with the @@cars hash
here: https://gist.github.com/arslanfarooq/1653f172e4192b1e78b9

It looks like @@cars contains each make in a Hash when one is created,
defaults to a value of 50 (why?), and adds one to that make every time a
Car is created with an make which exists inside the Array @@makes.

On 25.09.2013 13:49, Arslan F. wrote:

Hi guys,

Can someone explain line by line what is happening with the @@cars
hash
here: https://gist.github.com/arslanfarooq/1653f172e4192b1e78b9

  • The Hash is initialised on line 6
  • Keys (Strings representing ‘makes’) are added on line 22 with a
    default value of 50
  • Line 30 increments the value for a given ‘make’ by one when a new Car
    with the given ‘make’ is initialised
  • The value for a given ‘make’ is read on line 38.

If your question is why is the default count 50 then I have no idea!

Ah so the @@cars[make] = 50 is setting the key for the hash. Hm.

Alex G. wrote in post #1122405:
[…]

If your question is why is the default count 50 then I have no idea!

No in original code it was set to 0. I just changed it to see what
happens, and then later copied the code to gist without changing it back
to 0.

Thank you Alex.

I think I need to spend some time on Ruby hashes before moving forward
in the book.

Thanks again.

It’s probably best not to spend too long dwelling on class variables.
What they’re intended to do can usually be managed better by judicious
use of Observer classes.

Joel P. wrote in post #1122420:

It’s probably best not to spend too long dwelling on class variables.
What they’re intended to do can usually be managed better by judicious
use of Observer classes.

Thank you Joel!

I am a beginner of Ruby, and I am following this book to learn it. I
don’t have a proper programming background (only know a little PHP), so
I don’t know yet about observer classes.

Right now my priority is to just follow this book wherever it takes me
:slight_smile:

I learned a lot about OOP and problem-solving by reading the book
“Design Patterns in Ruby”, site here: http://designpatternsinruby.com/

I don’t know whether it’s the best book on the subject, but it was very
informative and has helped me simplify solutions in real-life
situations. It’s probably worth adding it to your “to read” list :slight_smile:

Thank you Joel! I’ll definitely give it a try. :slight_smile:

Am 25.09.2013 20:36, schrieb Joel P.:

I learned a lot about OOP and problem-solving by reading the book
“Design Patterns in Ruby”, site here: http://designpatternsinruby.com/

I don’t know whether it’s the best book on the subject, but it was very
informative and has helped me simplify solutions in real-life
situations. It’s probably worth adding it to your “to read” list :slight_smile:

And another great book by Russ Olsen: Eloquent Ruby.

But both probably require some basic understanding of Ruby.

Regards,
Marcus

But both probably require some basic understanding of Ruby.

Should a new programmer pick “Design Patterns in Ruby” as the first
book? Or would you guys recommend something else.

Design Patterns is not a first book. For ruby /Eloquent Ruby/ and
/Well-Grounded Rubyist/ are good introductions. The Orielly book by
Matz/Flanghan is great for both intro and reference. For the science and
engineering end of programming in general the books on lisp from mit
though
I wouldn’t recommend that to a beginner. There are many intro to new
developers available for free online for example:
http://ruby.bastardsbook.com/

Have at it: https://mitpress.mit.edu/sicp/full-text/book/book.html

Stu wrote in post #1122628:

Have at it: https://mitpress.mit.edu/sicp/full-text/book/book.html

Just started reading it. Thank you! God bless you.

Stu wrote in post #1122617:

Design Patterns is not a first book. For ruby /Eloquent Ruby/ and
/Well-Grounded Rubyist/ are good introductions. The Orielly book by
Matz/Flanghan is great for both intro and reference.[…]

Okay, but may be I should clarify that by “new programmer” I meant not
new to Ruby, but rather new to programming.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs