Project

analog

0.0
No release in over 3 years
Low commit activity in last 3 years
Scale a number from one range or set to another
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 5.5.0, ~> 5.5
>= 1.1.0, ~> 1.1
>= 10.4.2, ~> 10.4
>= 1.2.1, ~> 1.2
 Project Readme

Analog

A Ruby helper for scaling numbers

Background

Provides a quick way to scale a number from one range or set to another.

A simple example is:

You'd like to plot a point on a 500px graph using a data that lies in the 0..1 range.

Scale.transform(0.5).from(0..1).to(0..500)
=> 250

It's commonly useful for preparing data to be consumed by generalized APIs and communication protocols.

Personally, I've used it in graphics, audio and for converting music data between OSC and MIDI.

Usage

require "scale"

This example will scale a number down by 1/10th

Scale.transform(22).from(0..150).to(0..15)
=> 2

Output a float by using a float in the destination range

Scale.transform(22).from(0..150).to(0..15.0)
=> 2.2

Use Arrays and Sets

Scale.transform(0.40).from(0..1).to([0, 2, 4, 8, 12, 16, 32, 64, 128, 512])
=> 8

Scale.transform(8).from(Set.new([0, 2, 4, 8, 16, 64])).to(0..10)
=> 6

See the examples for more

Core Extension

There is a Numeric extension that you can optionally include.

require "scale/core_ext"

0.40.scaled_from(0..1).to([0, 2, 4, 8, 12, 16, 32, 64, 128, 512])
=> 8

See the core_ext example for more examples.

Installation

gem install analog

or with Bundler

gem "analog"

License

Licensed under Apache 2.0, See the file LICENSE

Copyright (c) 2014-2017 Ari Russo