Did Ruby 1.7 used to do these things?

I thought my old copy of SAMS TY Ruby in 21 Days, even though it assumes Ruby 1.7, was going to be good enough to get me started, but now I’m a little closer to thinking I might have to (horror of horrors) pay for a real book.

The book claims that

s = "AaBbCc 012"
s[0]

will return 65. When I, with Ruby 2.7 installed, try this in an irb session,

s[0]

unsurprisingly returns “A”. Was it the case that Ruby 1.7 would’ve returned the ASCII value, or are the authors way off track regardless?

Another claim they make is that if

footwear = “blue suede shoes”

footwear[“suede”]

footwear[“blue”] = “red”

footwear[“socks”] = “sandals”

…that the second line would just return “suede”, which it did, that the third line would cause footwear to equal “red suede shoes”, which it did, the last line wouldn’t return an error; would instead just leave the footwear string unaffected. But when I tried it in irb I got a multiline error message ending with “IndexError (string not matched)”

Is it true that attempting to index a nonexistent substring didn’t cause any errors in Ruby until later versions?

(And - since I think I know where this is all going anyway - what’s a good book to get started with contemporary Ruby? I’ve heard that O’Reilly’s Learning Ruby is actually well below the usual O’Reilly standard : / )

Yes, there have been many changes in Ruby over the years. I seem to remember there being major changes leading up to version 1.8, with a push for developers to upgrade, but I can’t find a changefile just now.

Your string change happened in version 1.9.1 when the internal encoding changed - see the news item: NEWS-1.9.1 - RDoc Documentation

You can compare the documentation for 1.8.6: Class: String (Ruby 1.8.6) and for 2.7.0: class String - RDoc Documentation

Depending on your knowledge of programming, maybe the official documentation will do? The main page is: RDoc Documentation and you can click on a topic in the left menu to read about methods etc.

I myself used the pickaxe book, but it has not been updated since Ruby 2.0.

1 Like

You see, Ruby is evolving very fast. Every new minor version (2.6, 2.7, etc.) is released generally on 25th December every year. These new versions come with a lot of features.

So… I will rather say it’s not particularly good to read books, what you are learning is probably not valid in the latest versions. Most programming books except C and C++ (in most cases) gets outdated pretty quickly…

The Ruby’s online documentation is really well written. You can follow the documentation here:

https://docs.ruby-lang.org/en/

Read docs about any latest versions, except the outdated ones!

If you are offline (say on a journey, maybe), you can utilize the ri documentations. You can use Termux on your Android phone to read the docs when you are offline! But books can be a bit outdated, but sometimes books cover most fascinating topics at the same time!