BernardK
Programming Ruby 3.2 (5th Edition): B4.0 page 521, my world has collapsed . .
… when I read that in “abcdefg”, f is the character at index -1, the last character
.
page 521, first bullet. The problem is that the explanation starts with an inclusive range, “abcdefg”[1..3], then switches to excluding ranges. I suggest to use inclusive ranges :
-
so “abcdefg”[3..-2] returns “def”, since d is the character at index 3, and f is the character at index -2
-
Halfway ranges also work, so “abcdefg”[..-2] (or “abcdefg”[...-1]) returns all but the last character
-
and “abcdefg”[3..] returns everything from index 3 on (“abcdefg”[3...] including the last character could be seen as an anomaly)
First Post!
noelrappin
Wow, not sure how I missed that. I think inclusive ranges are easier to understand here.
Changed to
"abcdefg"[4..-1] returns "efg", since e is the character at index f, and f is the character at index -1, the last character. Halfway ranges also work, so "abcdefg"[..-2] returns all but the last character, and "abcdefg"[3..] returns everything from index 3 on
Popular Pragmatic Bookshelf topics
Modern front-end development for Rails, second edition - Struggling to get the first chapter to work
Other popular topics
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /rails
- /python
- /js
- /security
- /go
- /swift
- /vim
- /clojure
- /emacs
- /haskell
- /java
- /svelte
- /onivim
- /typescript
- /kotlin
- /crystal
- /c-plus-plus
- /tailwind
- /react
- /gleam
- /ocaml
- /elm
- /flutter
- /vscode
- /ash
- /html
- /opensuse
- /centos
- /php
- /deepseek
- /zig
- /scala
- /sublime-text
- /lisp
- /textmate
- /react-native
- /nixos
- /debian
- /agda
- /kubuntu
- /arch-linux
- /django
- /revery
- /ubuntu
- /spring
- /deno
- /manjaro
- /nodejs
- /diversity
- /lua
- /julia
- /slackware
- /c






