
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










Other popular topics










Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /emacs
- /java
- /haskell
- /onivim
- /svelte
- /typescript
- /crystal
- /c-plus-plus
- /kotlin
- /tailwind
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /vscode
- /ash
- /opensuse
- /centos
- /php
- /deepseek
- /html
- /zig
- /scala
- /textmate
- /debian
- /sublime-text
- /nixos
- /lisp
- /agda
- /react-native
- /kubuntu
- /arch-linux
- /ubuntu
- /revery
- /spring
- /django
- /manjaro
- /diversity
- /lua
- /nodejs
- /c
- /slackware
- /julia
- /neovim