rin 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 89e40f1f24bf5249a57601996d351d6edcbf28ea
4
+ data.tar.gz: a43fc75f22f239d57f81aff28a9aa32afb2c1a30
5
+ SHA512:
6
+ metadata.gz: 1b5bfabb728c38eb8ebc33c42078046f5d2cee4442212af557dba3454a521849162bfad5ecfd49fa654f39dfdfc0b133260e54cd5a1324904acb0b8dce2d9b92
7
+ data.tar.gz: 24975450e1fe82b702f07f4f490c9bd207ca75c771af11c69157b9c3bb019093f65ab670105b7e76103e5cce1fac2dee50eef9d564d8807d0dbce860710e4564
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ script: bundle exec rspec
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.2
7
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,91 @@
1
+ [![Build Status](https://secure.travis-ci.org/JoshCheek/rin.png?branch=master)](http://travis-ci.org/JoshCheek/rin)
2
+
3
+ Rin: Inspects Ruby Integers
4
+ ===========================
5
+
6
+ Change the base that numbers display with in Ruby.
7
+
8
+
9
+ The executable
10
+ --------------
11
+
12
+ Prints whatever the script evaluates to.
13
+ Default inspect is hex.
14
+
15
+ ```sh
16
+ $ rin '170 + 17'
17
+ BB
18
+ ```
19
+
20
+ Can be told what base to use by passing the base as a flag.
21
+
22
+ ```sh
23
+ $ rin -8 '7 + 7'
24
+ 16
25
+ ```
26
+
27
+ You can put whatever Ruby you want in there.
28
+
29
+ From a Ruby commandline script
30
+ ------------------------------
31
+
32
+ From the shell, you can require rin, and output will be set by default to hex,
33
+ or to the base of the first argument to the script.
34
+
35
+ ```sh
36
+ $ ruby -rin -e 'p 15'
37
+ 0xF
38
+ ```
39
+
40
+
41
+ Polite Ruby script
42
+ ------------------
43
+
44
+ Use in Ruby without fucking with the environment.
45
+
46
+ ```ruby
47
+ require 'rin'
48
+ rin.hex { 15 } # => "0xF"
49
+ rin.oct { 15 } # => "017"
50
+ rin.bin { 15 } # => "0b1111"
51
+ rin.dec { 15 } # => "15"
52
+ ```
53
+
54
+
55
+ Invasive Ruby script
56
+ --------------------
57
+
58
+ But less annoying to use
59
+
60
+ ```
61
+ 15 # => 15
62
+
63
+ rin.hex!
64
+ 15 # => 0xF
65
+
66
+ rin.dec!
67
+ 15 # => 15
68
+ ```
69
+
70
+ Other things that could be fun
71
+ ------------------------------
72
+
73
+ But that I don't intend to do,
74
+ unless I use this much.
75
+
76
+ * env var for for config:
77
+ * preferred base
78
+ * preferred format (eg uppercase/lowercase/whatev)
79
+ * Support Ruby's executable arguments
80
+ * Support `String#to_i`
81
+ * Support `[1..100].map(&Rin.hex)`
82
+ * It has a naive preprocessor that swaps them to the correct values.
83
+ Eventually would be nice to get this:
84
+ ```sh
85
+ $ rin 'AA + 11'
86
+ BB
87
+ ```
88
+ * exit statuses be meaningful
89
+ eg `rin 'BB == (AA + 11)` exits with 0
90
+ * `0d123` for decimal
91
+ * Case insensitive
data/bin/rin ADDED
@@ -0,0 +1,61 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
+
5
+ program = nil
6
+ base = 16
7
+ print_result = true
8
+ print_help = false
9
+ unknown_args = []
10
+
11
+ ARGV.each do |arg|
12
+ case arg
13
+ when '-P'
14
+ print_result = false
15
+ when '-h', '--help'
16
+ print_help = true
17
+ when /^-(\d+)$/
18
+ base = $1.to_i
19
+ when /^-.*/
20
+ unknown_args << arg
21
+ else
22
+ program = arg
23
+ end
24
+ end
25
+
26
+ if print_help
27
+ $stdout.puts "Usage: rin [-base] [-P] [-h] program"
28
+ $stdout.puts ""
29
+ $stdout.puts "Synopsis:"
30
+ $stdout.puts " Inspects Ruby Integers"
31
+ $stdout.puts ""
32
+ $stdout.puts "Examples:"
33
+ $stdout.puts " $ rin '7+8' # prints \"E\""
34
+ $stdout.puts " $ rin -8 '7+2' # prints \"11\""
35
+ $stdout.puts " Instead of printing, write to a file"
36
+ $stdout.puts " $ rin -P 'File.write \"out\", (7+8).inspect'"
37
+ $stdout.puts ""
38
+ $stdout.puts "Flags:"
39
+ $stdout.puts " -base sets the base to that number"
40
+ $stdout.puts " defaults to 16 (hex)"
41
+ $stdout.puts " eg: -2 for binary, -8 for octal"
42
+ $stdout.puts ""
43
+ $stdout.puts " -P turns off program result autoprinting"
44
+ elsif unknown_args.any?
45
+ $stderr.puts "Unknown arguments: #{unknown_args.inspect}"
46
+ exit 1
47
+ elsif !program
48
+ $stderr.puts "No code specified, run with -h for examples"
49
+ exit 1
50
+ else
51
+ require 'rin'
52
+ begin
53
+ rin.base base do
54
+ result = binding.eval program, '-e'
55
+ p result if print_result
56
+ end
57
+ rescue ArgumentError => e
58
+ $stderr.puts e.message
59
+ exit 1
60
+ end
61
+ end
@@ -0,0 +1 @@
1
+ require 'rin' # hook to enable `ruby -rin`
@@ -0,0 +1,103 @@
1
+ def rin
2
+ Rin.instance
3
+ end
4
+
5
+ class Rin
6
+ def self.instance
7
+ @instance ||= begin
8
+ instance = new(10)
9
+ instance.enable!
10
+ instance
11
+ end
12
+ end
13
+
14
+ def initialize(base)
15
+ @base = base
16
+ @inspects = {
17
+ Fixnum => Fixnum.instance_method(:inspect),
18
+ Bignum => Bignum.instance_method(:inspect),
19
+ }
20
+ end
21
+
22
+ def disable!
23
+ @inspects.each do |klass, original|
24
+ define_inspect klass, original
25
+ end
26
+ end
27
+
28
+ def enable!
29
+ @inspects.each do |klass, _original|
30
+ define_inspect klass, Proc.new {
31
+ Rin.instance.inspect_num self
32
+ }
33
+ end
34
+ end
35
+
36
+ # uhm... returns the base if no base is given
37
+ # if base is given, it sets the base to that and calls the block
38
+ def base(set_base=false, &block)
39
+ return @base unless set_base
40
+ temporary_base set_base, &block
41
+ end
42
+
43
+ def inspect_num(n)
44
+ n.to_s(@base).upcase
45
+ end
46
+
47
+ def hex(&block)
48
+ temporary_base 16, &block
49
+ end
50
+
51
+ def hex!
52
+ @base = 16
53
+ end
54
+
55
+ def oct(&block)
56
+ temporary_base 8, &block
57
+ end
58
+
59
+ def oct!
60
+ @base = 8
61
+ end
62
+
63
+ def bin(&block)
64
+ temporary_base 2, &block
65
+ end
66
+
67
+ def bin!
68
+ @base = 2
69
+ end
70
+
71
+ def dec(&block)
72
+ temporary_base 10, &block
73
+ end
74
+
75
+ def dec!
76
+ @base = 10
77
+ end
78
+
79
+ private
80
+
81
+ def define_inspect(klass, method_def)
82
+ klass.class_eval do
83
+ define_method :inspect, method_def
84
+ end
85
+ end
86
+
87
+ def temporary_base(overriden_base, &block)
88
+ validate_base! overriden_base
89
+ initial_base = @base
90
+ @base = overriden_base
91
+ block.call
92
+ ensure
93
+ @base = initial_base if initial_base # don't reset to nil, when error is raised
94
+ end
95
+
96
+ def validate_base!(base)
97
+ 1.to_s base
98
+ rescue ArgumentError
99
+ raise ArgumentError, "Invalid base: #{base.inspect}"
100
+ end
101
+ end
102
+
103
+ rin.hex! if $0 == '-e' # stupid that accessing the var turns it on
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "rin"
3
+ s.version = '0.0.1'
4
+ s.authors = ["Josh Cheek"]
5
+ s.email = ["josh.cheek@gmail.com"]
6
+ s.homepage = "https://github.com/JoshCheek/rin"
7
+ s.summary = %q{Make it easier to script with numbers of alternate bases.}
8
+ s.description = %q{bin/lib that overrides inspect on integers to print numbers in a desired base}
9
+ s.license = "WTFPL"
10
+
11
+ s.files = `git ls-files`.split("\n")
12
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
14
+
15
+ s.add_development_dependency "rspec", '~> 3.0'
16
+ end
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+
3
+ spec_helpers = Module.new do
4
+ def matcher_for(str_or_regex)
5
+ case str_or_regex
6
+ when Regexp then match str_or_regex
7
+ else eq str_or_regex
8
+ end
9
+ end
10
+
11
+ def runs!(*program)
12
+ opts = program.pop if program.last.kind_of? Hash
13
+ out = opts.fetch :out, //
14
+ err = opts.fetch :err, ""
15
+ status = opts.fetch :status, 0
16
+
17
+ actual_out, actual_err, actual_status = Open3.capture3(*program)
18
+
19
+ expect(actual_err).to matcher_for(err)
20
+ expect(actual_out).to matcher_for(out)
21
+ expect(actual_status.exitstatus).to eq status
22
+ end
23
+ end
24
+
25
+ RSpec.describe '$ rin' do
26
+ include spec_helpers
27
+
28
+ it 'prints the inspection of whatever the script evaluates to' do
29
+ runs! 'rin', '"hello"', out: %'"hello"\n'
30
+ end
31
+
32
+ it 'sets __FILE__ to "-e", like Ruby\'s -e' do
33
+ runs! 'rin', '__FILE__', out: %'"-e"\n'
34
+ end
35
+
36
+ it 'accepts preferred base as flags' do
37
+ runs! 'rin', '-8', '7 + 7', out: "16\n"
38
+ end
39
+
40
+ it 'defaults the base to hex' do
41
+ runs! 'rin', '7 + 7', out: "E\n"
42
+ end
43
+
44
+ it 'translates things that are probably numbers into the correct base'
45
+
46
+ specify '-P turns off autoprinting' do
47
+ runs! 'rin', '-P', '1+1', out: ''
48
+ end
49
+
50
+ it 'sets the base before printing' do
51
+ runs! 'rin', '15.inspect', out: %'"F"\n'
52
+ end
53
+
54
+ it 'prints help on -h and --help' do
55
+ runs! 'rin', '-h', out: /usage/i
56
+ runs! 'rin', '--help', out: /usage/i
57
+ end
58
+
59
+ describe 'errors' do
60
+ it 'notifies you if you omit the program' do
61
+ runs! 'rin', '-8', '-P', status: 1,
62
+ err: /no code specified/i,
63
+ out: ''
64
+ end
65
+
66
+ it 'notifies you if you choose an invalid base' do
67
+ runs! 'rin', '-36', '1', status: 0
68
+ runs! 'rin', '-37', '1', status: 1,
69
+ err: /invalid base/i,
70
+ out: ''
71
+ end
72
+
73
+ it 'notifies you if you supply an unknown arg' do
74
+ runs! 'rin', '-', '1', status: 1,
75
+ err: /unknown arg.*?"-"/i,
76
+ out: ''
77
+ end
78
+ end
79
+ end
80
+
81
+
82
+ RSpec.describe '$ ruby -rin -e' do
83
+ include spec_helpers
84
+
85
+ it 'defaults the base to hex' do
86
+ runs! 'ruby', '-rin', '-e', 'p 7 + 7', out: "E\n"
87
+ end
88
+
89
+ it 'translates things that are probably numbers into the correct base'
90
+ end
@@ -0,0 +1,98 @@
1
+ require 'spec_helper'
2
+ require 'rin'
3
+
4
+ # base!
5
+
6
+ RSpec.describe 'rin' do
7
+ specify 'always returns the same object' do
8
+ # call it under various different `self`s
9
+ expect(rin).to equal Object.new.instance_eval { rin }
10
+ end
11
+
12
+ describe '.base(n) { ... }' do
13
+ specify '.base(n) { ... } allows for arbitrary base overrides' do
14
+ twenty_four_base_thirteen =
15
+ rin.base(13) { (12 + 12).inspect }
16
+ expect(twenty_four_base_thirteen).to eq '1B'
17
+ end
18
+
19
+ it 'resets the base on .<base> { ... }, even if the block raises an exception' do
20
+ expect {
21
+ rin.hex do
22
+ expect(rin.base).to eq 16
23
+ raise 'zomg'
24
+ end
25
+ }.to raise_error RuntimeError, 'zomg'
26
+ expect(rin.base).to eq 10
27
+ end
28
+
29
+ it 'raises the ArgumentError that to_s would, if given an invalid base' do
30
+ expect { 15.to_s 37 }.to raise_error ArgumentError, /invalid/i
31
+ expect { rin.base(37) { nil } }.to raise_error ArgumentError, /invalid/i
32
+ expect(rin.base).to eq 10
33
+ end
34
+ end
35
+
36
+ describe 'named bases' do
37
+ def self.test_named_base(basename, base, tests)
38
+ specify ".#{basename} { ... } sets the inspect to base #{base} within the block" do
39
+ tests.each do |int, (dec_inspect, base_inspect)|
40
+ expect(int.inspect).to eq dec_inspect
41
+ rin.__send__ basename do
42
+ expect(int.inspect).to eq base_inspect
43
+ end
44
+ expect(int.inspect).to eq dec_inspect
45
+ end
46
+ end
47
+
48
+ specify ".#{basename}! sets the inspect to #{base} permanently" do
49
+ tests.each do |int, (dec_inspect, base_inspect)|
50
+ expect(int.inspect).to eq dec_inspect
51
+ rin.__send__ "#{basename}!"
52
+ expect(int.inspect).to eq base_inspect
53
+ rin.dec!
54
+ end
55
+ end
56
+ end
57
+
58
+ test_named_base 'hex', 16, 15 => ['15', 'F']
59
+ test_named_base 'oct', 8, 15 => ['15', '17']
60
+ test_named_base 'bin', 2, 10 => ['10', '1010']
61
+ test_named_base 'dec', 10, 15 => ['15', '15']
62
+ end
63
+
64
+ it 'exposes the current base' do
65
+ expect(rin.base).to eq 10
66
+ end
67
+
68
+ it 'returns the block value on .<base>' do
69
+ expect(rin.hex { 'zomg' }).to eq 'zomg'
70
+ end
71
+
72
+ it 'works for bignums' do
73
+ n = 10**1000
74
+ expect(n).to be_a_kind_of Bignum
75
+ expect(rin.hex { n.inspect })
76
+ .to eq n.to_s(16).upcase
77
+ end
78
+
79
+ it 'supports nested overrides' do
80
+ expect(15.inspect).to eq '15'
81
+ rin.hex do
82
+ expect(15.inspect).to eq 'F'
83
+ rin.oct { expect(15.inspect).to eq '17' }
84
+ expect(15.inspect).to eq 'F'
85
+ end
86
+ expect(15.inspect).to eq '15'
87
+ end
88
+
89
+ it 'can be enabled and disabled' do
90
+ rin.hex!
91
+ expect(15.inspect).to eq 'F'
92
+ rin.disable!
93
+ expect(15.inspect).to eq '15'
94
+ rin.enable!
95
+ expect(15.inspect).to eq 'F'
96
+ rin.dec!
97
+ end
98
+ end
@@ -0,0 +1,11 @@
1
+ require 'open3'
2
+
3
+ lib_path = File.expand_path '../../lib', __FILE__
4
+ bin_path = File.expand_path '../../bin', __FILE__
5
+ ENV['PATH'] = "#{bin_path}:#{ENV['PATH']}"
6
+ ENV['RUBYOPT'] = "-I #{lib_path} #{ENV['RUBYOPT']}"
7
+
8
+ RSpec.configure do |config|
9
+ config.disable_monkey_patching!
10
+ config.fail_fast = true
11
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Josh Cheek
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ description: bin/lib that overrides inspect on integers to print numbers in a desired
28
+ base
29
+ email:
30
+ - josh.cheek@gmail.com
31
+ executables:
32
+ - rin
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - ".gitignore"
37
+ - ".rspec"
38
+ - ".travis.yml"
39
+ - Gemfile
40
+ - Readme.md
41
+ - bin/rin
42
+ - lib/in.rb
43
+ - lib/rin.rb
44
+ - rin.gemspec
45
+ - spec/integration_spec.rb
46
+ - spec/rin_spec.rb
47
+ - spec/spec_helper.rb
48
+ homepage: https://github.com/JoshCheek/rin
49
+ licenses:
50
+ - WTFPL
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.4.5
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Make it easier to script with numbers of alternate bases.
72
+ test_files:
73
+ - spec/integration_spec.rb
74
+ - spec/rin_spec.rb
75
+ - spec/spec_helper.rb