core tools, libraries & scripts - logger, config, dates, alphabets, shell & more
CC0-1.0 License
Gems:
Bool()
, to_b
, parse_bool
/ to_bool
, bool?
, false?
, true?
, true.is_a?(Bool)==true
, false.is_a?(Bool)==true
, and moreAfter programming in ruby for more than 10+ years and sharing / publishing 200+ gems and - yes, believe it or not - getting perma-banned ("cancel-cultured" ) for life twice (thanks to Richard Schneeman, Brandon Weaver, et al) on r/reddit and ruby-talk - see the public service announcement for some background - I (Gerald Bauer) will try to keep a public log on how to make ruby even more fun by collecting ideas (mostly backed-up by "real-world" code & monkey patches) right here on this page. Your questions and comments are more than welcome.
About Strings
Did you know? In rubyland a string (like a character) started as a series of bytes. In the old days a character was a byte (that is, an unsigned integer number in the range of 0-255).
Background Reading
That all changed with wide-chars, unicode, & friends. In 2024 does ruby need a (Binary) Buffer class? Why? Why Not? Discuss.
Working with binary and hex(adecimal) strings in ruby
In 2024 the "classic" way to convert a binary string to a hex(adecimal) string and vice versa in ruby is like:
def hex_to_bin( hex )
raise TypeError, "hex_to_bin - non-hexadecimal digit found in >#{hex}<" unless hex =~ /\A(?:0x)?[0-9a-f]*\z/i
## note: assume pack always returns string with BINARY/ASCII-8BIT encoding!!!
if ['0x', '0X'].include?( hex[0,2] ) ## cut-of leading 0x or 0X if present
[hex[2..-1]].pack('H*')
else
[hex].pack('H*')
end
end
def bin_to_hex( bin )
# note: unpack returns string with <Encoding:US-ASCII>
# convert to default encoding
hex = bin.unpack('H*').first
hex.encode!( Encoding::UTF_8 )
hex
end
The idea is to add new hex helpers to String#hex
and Kernel#hex
.
That let's you use convert hex strings to bin(ary) string via Kernel#hex
e.g.
bin = hex"00000000000000000000000000000000000000000000000000000000000004d2"
and vice versa via String#hex
:
bin.hex
#=> "00000000000000000000000000000000000000000000000000000000000004d2"
Did you know? In rubyland String#hex
like String#oct
is already defined
and is an alias for String#to_i(16)
or String#to_i(8)
.
"41".to_i(16) #=> 65
"0x41".to_i(16) #=> 65 - same as "41" - 0x hex prefix gets skipped
"41".hex #=> 65
"0x41".hex #=> 65 - same as "41" - 0x hex prefix gets
The idea for ruby 4.0 is to redefine String#hex
to return a hex string.
Another idea is to use a different name (and aliases)
such as String#hexdump
or String#hexdigest
and so on.
But somehow the matching symetry of String#hex
and Kernel#hex
and principle of least surprise gets lost.
Anyone ever used the old String#hex
? Let's find real-world code snippets / references.
Did you know? In rubyland there is no Bool
class only TrueClass
and FalseClass
.
This can easily monkey-patched - why not make it official? See the safebool gem for a start.
Background Reading:
Did you know? In rubyland there is no Enum
class.
The official party line is that
there's no class needed, just use symbols :-) or use constants. Example:
Color = [:red, :blue, :green]
Color[0] #=> :red
Color[1] #=> :blue
# -or-
Color = {red: 0, blue: 1, green: 2}
Color[:red] #=> 0
Color[:blue] #=> 1
Color.keys #=> [:red, :blue, :green]
Color.values #=> [0, 1, 2]
# -or-
module Color
RED = 0
BLUE = 1
GREEN = 2
end
Color::RED #=> 0
Color::BLUE #=> 1
Color.constants #=> [:RED, :BLUE, :GREEN]
# ...
Why? Why not? Discuss.
See the enums gem for a start on a enum class.
Are there any auto-include quick-starter prelude & prolog gems out there in rubyland?
You could argue ruby is dead and rails is the "All your base are belong to us" prelude & prolog quick-starter gem.
Why not make the concept of auto-include quick-starter prelude & prolog gems more popular and offer more options?
See the cocos (code commons) gem for a start on an (off-rails) auto-include quick-starter prelude & prolog gem.
I have written a seven part series on how the standard csv package in rubyland is broken years ago (some issues got fixed thanks to Sutou Kouhei - you are a hero) but others are "unfixable" evergreens.
See the csvreader gem series for a start.
Background Reading:
Did you know? In rubyland everyone rolls their own project scaffolder (bundler, hoe, rails, jekyll, etc.) - why not use a shared project scaffolder open to everyone?
See the quik gem series for a start.
Background Reading:
To be continued & updated...
Reminder: Your questions and comments are more than welcome.
The scripts are dedicated to the public domain. Use it as you please with no restrictions whatsoever.