Skip to content
Snippets Groups Projects
Commit 2b99c982 authored by Stephen D's avatar Stephen D
Browse files

turn into ruby gem

parent 3d6b4e7e
No related branches found
No related tags found
No related merge requests found
Pipeline #8144 failed
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
default:
image: ruby:3.3.6
before_script:
- gem install bundler -v 2.5.22
- bundle install
example_job:
script:
- bundle exec rake
AllCops:
TargetRubyVersion: 3.0
Style/StringLiterals:
EnforcedStyle: double_quotes
Style/StringLiteralsInInterpolation:
EnforcedStyle: double_quotes
......@@ -2,6 +2,13 @@
source "https://rubygems.org"
# gem "rails"
# Specify your gem's dependencies in rubic.gemspec
gemspec
gem "rake", "~> 13.0"
gem "minitest", "~> 5.16"
gem "rubocop", "~> 1.21"
gem "rb-readline", "~> 0.5.5"
PATH
remote: .
specs:
rubic (0.1.0)
GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
json (2.9.1)
language_server-protocol (3.17.0.3)
minitest (5.25.4)
parallel (1.26.3)
parser (3.3.6.0)
ast (~> 2.4.1)
racc
racc (1.8.1)
rainbow (3.1.1)
rake (13.2.1)
rb-readline (0.5.5)
regexp_parser (2.10.0)
rubocop (1.69.2)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 2.9.3, < 3.0)
rubocop-ast (>= 1.36.2, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 4.0)
rubocop-ast (1.37.0)
parser (>= 3.3.1.0)
ruby-progressbar (1.13.0)
unicode-display_width (3.1.3)
unicode-emoji (~> 4.0, >= 4.0.4)
unicode-emoji (4.0.4)
PLATFORMS
ruby
x86_64-linux
DEPENDENCIES
minitest (~> 5.16)
rake (~> 13.0)
rb-readline (~> 0.5.5)
rubic!
rubocop (~> 1.21)
BUNDLED WITH
2.5.22
The MIT License (MIT)
Copyright (c) 2025 Stephen D
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
# Rubic
TODO: Delete this and the text below, and describe your gem
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rubic`. To experiment with that code, run `bin/console` for an interactive prompt.
## Installation
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
Install the gem and add to the application's Gemfile by executing:
```bash
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
```
If bundler is not being used to manage dependencies, install the gem by executing:
```bash
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
```
## Usage
TODO: Write usage instructions here
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rubic.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
Rakefile 0 → 100644
# frozen_string_literal: true
require "bundler/gem_tasks"
require "minitest/test_task"
Minitest::TestTask.create
require "rubocop/rake_task"
RuboCop::RakeTask.new
task default: %i[test rubocop]
#!/usr/bin/env ruby
# frozen_string_literal: true
require "bundler/setup"
require "rubic"
# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
require "irb"
IRB.start(__FILE__)
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx
bundle install
# Do any other automated setup that you need to do here
# frozen_string_literal: true
require 'readline'
require 'irb'
require_relative "rubic/version"
module Rubic
class Error < StandardError; end
def self.run
Rubic.new.eval_forever
end
class LineGroup
def initialize
@lines = {}
end
def update(line_num, expr)
if expr.nil?
@lines.delete(line_num)
else
@lines[line_num] = expr
end
end
def resize(gap)
@lines =
@lines
.sort_by { |line_num, _| line_num }
.each_with_index
.map { |hsh, i| [(i + 1) * gap, hsh[1]] }
.to_h
end
def stringify(with_line_numbers)
lines = @lines.map { |k, v| [k, v] }
lines.sort_by {|k, _| k}.map do |line_num, line|
if with_line_numbers
"#{line_num} #{line}"
else
line
end
end.join("\n")
end
def save(file_name, with_line_numbers)
File.open(file_name, 'w') do |file|
file.write(self.stringify(with_line_numbers))
file.write("\n")
end
end
def load(file_name, with_line_numbers)
@lines = File.readlines(file_name, chomp: true)
.each_with_index
.map do |line, i|
if with_line_numbers
line_num, line = line.split(' ')
line_num = Integer(line_num)
[line_num, line]
else
[i + 1, line]
end
end.to_h
end
end
class Rubic
def initialize
@workspace = IRB::WorkSpace.new(TOPLEVEL_BINDING.dup)
end
def eval_forever
lines = LineGroup.new
# so we can reset settings after
history_len = Readline::HISTORY.size
og_completion_proc = Readline.completion_proc
Readline.completion_proc = ->(target) { completion(target) }
loop do
line = Readline::readline('> ')
break if line.nil?
Readline::HISTORY.push(line) if line.strip != ''
cmd, args = line.split(' ', 2)
case cmd
when 'list'
puts lines.stringify(true)
when 'run'
run(lines.stringify(false))
when 'load'
if args.size == 0
puts "Usage: load filename.rub"
else
lines.load(args, true)
end
when 'save'
if args.size == 0
puts "Usage: save filename.rub"
else
lines.save(args, true)
end
when 'import'
if args.size == 0
puts "Usage: import filename.rb"
else
lines.load(args, false)
end
when 'export'
if args.size == 0
puts "Usage: export filename.rb"
else
lines.save(args, false)
end
when 'squish'
lines.resize(1)
when 'widen'
lines.resize(10)
when 'help'
print_help
when 'exit', 'bye'
break
else
line_num, expr = line.split(' ', 2)
line_num = Integer(line_num) rescue nil
if line_num.nil?
run(line) if line != ''
else
lines.update(line_num, expr)
end
end
rescue Interrupt
puts "^C"
end
# so that the shell is put on a new line
# if we exit with ctrl+d
puts
ensure
# reset history
delta = Readline::HISTORY.size - history_len
(0...delta).each do
Readline::HISTORY.pop
end
Readline.completion_proc = og_completion_proc
end
private
def completion(target)
line_num, expr = target.split(' ', 2)
line_num = Integer(line_num) rescue nil
unless line_num.nil?
target = expr || ''
end
completor = IRB::RegexpCompletor.new
candidates = completor.completion_candidates('', target, '', bind: @workspace.binding)
if line_num.nil?
candidates += %w[list run load save import export squish widen help exit bye]
end
candidates
end
def run(expr)
ret = begin
@workspace.evaluate(expr)
rescue Exception => e
e
end
print "=> "
pp ret
end
def print_help
puts """
Usage:
\thelp\t\tDisplay this help message
\t{num} {expr}\tSet line {num} to {expr}
\t{num}\t\tClear line {num}
\tlist\t\tList current program
\trun\t\tRun current program
\tsquish\t\tRe-numbers lines so that they go 1, 2, 3, etc.
\twiden\t\tRe-numbers lines so that they go 10, 20, 30, etc.
\tload {file}\tLoad program from file (expects line numbers in the file)
\tsave {file}\tSave program to file (includes line numbers in file)
\timport {file}\tImports a \"normal\" Ruby program (no line numbers)
\texport {file}\tExports a \"normal\" Ruby program (no line numbers)
"""
end
end
end
# frozen_string_literal: true
module Rubic
VERSION = "0.1.0"
end
require 'readline'
require 'irb'
class LineGroup
def initialize
@lines = {}
end
def update(line_num, expr)
if expr.nil?
@lines.delete(line_num)
else
@lines[line_num] = expr
end
end
def resize(gap)
@lines =
@lines
.sort_by { |line_num, _| line_num }
.each_with_index
.map { |hsh, i| [(i + 1) * gap, hsh[1]] }
.to_h
end
def stringify(with_line_numbers)
lines = @lines.map { |k, v| [k, v] }
lines.sort_by {|k, _| k}.map do |line_num, line|
if with_line_numbers
"#{line_num} #{line}"
else
line
end
end.join("\n")
end
def save(file_name, with_line_numbers)
File.open(file_name, 'w') do |file|
file.write(self.stringify(with_line_numbers))
file.write("\n")
end
end
def load(file_name, with_line_numbers)
@lines = File.readlines(file_name, chomp: true)
.each_with_index
.map do |line, i|
if with_line_numbers
line_num, line = line.split(' ')
line_num = Integer(line_num)
[line_num, line]
else
[i + 1, line]
end
end.to_h
end
end
class Rubic
def initialize
@workspace = IRB::WorkSpace.new(TOPLEVEL_BINDING.dup)
end
def eval_forever
lines = LineGroup.new
# so we can reset settings after
history_len = Readline::HISTORY.size
og_completion_proc = Readline.completion_proc
Readline.completion_proc = ->(target) { completion(target) }
loop do
line = Readline::readline('> ')
break if line.nil?
Readline::HISTORY.push(line) if line.strip != ''
cmd, args = line.split(' ', 2)
case cmd
when 'list'
puts lines.stringify(true)
when 'run'
run(lines.stringify(false))
when 'load'
if args.size == 0
puts "Usage: load filename.rub"
else
lines.load(args, true)
end
when 'save'
if args.size == 0
puts "Usage: save filename.rub"
else
lines.save(args, true)
end
when 'import'
if args.size == 0
puts "Usage: import filename.rb"
else
lines.load(args, false)
end
when 'export'
if args.size == 0
puts "Usage: export filename.rb"
else
lines.save(args, false)
end
when 'squish'
lines.resize(1)
when 'widen'
lines.resize(10)
when 'help'
print_help
when 'exit', 'bye'
break
else
line_num, expr = line.split(' ', 2)
line_num = Integer(line_num) rescue nil
if line_num.nil?
run(line) if line != ''
else
lines.update(line_num, expr)
end
end
rescue Interrupt
puts "^C"
end
# so that the shell is put on a new line
# if we exit with ctrl+d
puts
ensure
# reset history
delta = Readline::HISTORY.size - history_len
(0...delta).each do
Readline::HISTORY.pop
end
Readline.completion_proc = og_completion_proc
end
private
def completion(target)
line_num, expr = target.split(' ', 2)
line_num = Integer(line_num) rescue nil
unless line_num.nil?
target = expr || ''
end
completor = IRB::RegexpCompletor.new
candidates = completor.completion_candidates('', target, '', bind: @workspace.binding)
if line_num.nil?
candidates += %w[list run load save import export squish widen help exit bye]
end
candidates
end
def run(expr)
ret = begin
@workspace.evaluate(expr)
rescue Exception => e
e
end
print "=> "
pp ret
end
def print_help
puts """
Usage:
\thelp\t\tDisplay this help message
\t{num} {expr}\tSet line {num} to {expr}
\t{num}\t\tClear line {num}
\tlist\t\tList current program
\trun\t\tRun current program
\tsquish\t\tRe-numbers lines so that they go 1, 2, 3, etc.
\twiden\t\tRe-numbers lines so that they go 10, 20, 30, etc.
\tload {file}\tLoad program from file (expects line numbers in the file)
\tsave {file}\tSave program to file (includes line numbers in file)
\timport {file}\tImports a \"normal\" Ruby program (no line numbers)
\texport {file}\tExports a \"normal\" Ruby program (no line numbers)
"""
end
end
Rubic.new.eval_forever
# frozen_string_literal: true
require_relative "lib/rubic/version"
Gem::Specification.new do |spec|
spec.name = "rubic"
spec.version = Rubic::VERSION
spec.authors = ["Stephen D"]
spec.email = ["webmaster@scd31.com"]
spec.summary = "TODO: Write a short summary, because RubyGems requires one."
spec.description = "TODO: Write a longer description or delete this line."
spec.homepage = "TODO: Put your gem's website or public repo URL here."
spec.license = "MIT"
spec.required_ruby_version = ">= 3.0.0"
spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
gemspec = File.basename(__FILE__)
spec.files = IO.popen(%w[git ls-files -z], chdir: __dir__, err: IO::NULL) do |ls|
ls.readlines("\x0", chomp: true).reject do |f|
(f == gemspec) ||
f.start_with?(*%w[bin/ test/ spec/ features/ .git .gitlab-ci.yml appveyor Gemfile])
end
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
# Uncomment to register a new dependency of your gem
# spec.add_dependency "example-gem", "~> 1.0"
# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
end
module Rubic
VERSION: String
# See the writing guide of rbs: https://github.com/ruby/rbs#guides
end
# frozen_string_literal: true
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
require "rubic"
require "minitest/autorun"
# frozen_string_literal: true
require "test_helper"
class TestRubic < Minitest::Test
def test_that_it_has_a_version_number
refute_nil ::Rubic::VERSION
end
def test_it_does_something_useful
assert false
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment