Skip to content
/ core Public

core tools, libraries & scripts - logger, config, dates, alphabets, shell & more

License

Notifications You must be signed in to change notification settings

rubycocos/core

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Jun 14, 2024
28292f4 · Jun 14, 2024

History

62 Commits
Jun 14, 2024
Dec 11, 2022
Oct 31, 2020
Oct 31, 2020
Aug 9, 2021
Jan 10, 2023
Oct 31, 2020
Oct 31, 2020
Oct 31, 2020
Oct 31, 2020
Oct 31, 2020
Oct 31, 2020
Aug 25, 2021
Sep 28, 2021
Sep 26, 2021
Oct 31, 2020
Jun 12, 2021
Sep 27, 2021
Oct 31, 2020
Jan 10, 2023

Repository files navigation

core tools, libraries & scripts

Gems:

  • hexutils - hex(adecimal) encode/decode helpers 'n' more for String, NilClass, Kernel and more
  • bytes - bits 'n' bytes made easy/easier incl. new buffer helper / wrapper class to help with the string byte vs character dichotomy
  • enums - safe enum / enumeration types - a set of symbolic keys bound to unique integer numbers (incl. bit flags option)
  • safebool - safe bool / boolean type adds Bool(), to_b, parse_bool / to_bool, bool?, false?, true?, true.is_a?(Bool)==true, false.is_a?(Bool)==true, and more
  • props - manage settings hierarchies (commandline, user, home, defaults, etc.)
  • props-activerecord - addon for database support (ConfDb, props model, etc.)
  • iniparser - read / parse INI configuration, settings and data files into a hash (incl. named subsections)
  • alphabets - alphabet (a-z) helpers incl. unaccent, downcase, variants, and more
  • date-formats - read / parse and print dates (and times) from around the world
  • date-formatter - date formatter by example; auto-builds the strftime format string from an example date
  • records - frozen / immutable structs with copy on updates
  • safedata - safe (algebraic) union data types with (enumerated) variants

If I Were King Matz (aka Yukihiro Matsumoto) - Ideas For Ruby 4.0 - What's Broken & Missing in Ruby 3.x and How To Fix It

After 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.

Core Language & Types

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.

The Missing Bool Class

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:

The Missing Enum Class?

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.

The Missing Preludes / Quick Starters?

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.

Standard Gems / Libraries

(Open) Tabular Data Formats - A Better CSV Gem (Or Better) Gems

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:

Standard Project Scaffolders

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.

License

The scripts are dedicated to the public domain. Use it as you please with no restrictions whatsoever.

About

core tools, libraries & scripts - logger, config, dates, alphabets, shell & more

Topics

Resources

License

Stars

Watchers

Forks

Languages