Skip to content

Two questions: #to_hash and custom units #22

Open
@zverok

Description

@zverok

(Sorry, it's currently not an bug report/feature request, rather a questions. I am currently evaluating Unitwise for my tasks and haven't find another way to communicate.)

About #to_hash methods

As far as I know, Ruby has following convention:

  • object provides to_h if it can be somehow converted to hash;
  • object provides to_hash if it thinks of itself as a some kind of hash.

Unitwise's classes currently define to_hash method, while, I think, they can not be seen as "some kind of hash".

Theoretical example of bad consequences:

def test(**options)
  p options
end

test(1) # fails with ArgumentError, which is as expected
test(Unitwise(1, 'm')) # calls test with a large hash, which is strange

Practical examle of bad consequences: when using awesome_print gem in irb (which is pretty common), it outputs those large hashes instead of compact #inspect results.

About custom units

Unitwise provides very cool, effective, well-writen and well-tested code for working with units. What could make me (and, maybe, not only me?) happy is possibility, optional, to work with any units outside the controlled dictionary. Supposed synopsys:

# Variant 1:
Unitwise(1000, 'person') # => fails
require 'unitwise/custom_units'
Unitwise(1000, 'person') # => ok

# Variant 2:
Unitwise(1000, 'person') # => fails
Unitwise::Unit.define 'person' # + some options
Unitwise(1000, 'person') # => ok

# ...and now:
Unitwise(1000, 'person') / Unitwise(1, 'km') # => Unitwise(1000 'person/km')
Unitwise(10, 'person')**2 # => Unitwise(100, 'person2')

WDYT?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions