Ruby Packer (rubyc)
repository·master·Indexed 23 days ago
https://github.com/pmq20/ruby-packerA tool that packs Ruby and/or Rails applications into a single, portable executable binary. It supports native C extensions and works across Windows, macOS, and Linux without requiring the end-user to have Ruby installed.
What's inside ruby-packer
- test-unit (Test::Unit) is an xUnit family unit testing framework for Ruby. Based on xUnit principles (originally designed for Smalltalk's SUnit), it provides tools for writing tests, checking results, and performing automated testing within Ruby applications.
Overview of MSpec
masterMSpec is a specialized testing framework designed for writing specs for Ruby implementations in the
ruby/specrepository. It is syntax-compatible with RSpec 2, supporting standard blocks likedescribe,it,before, andafteractions.Key features include:
- Guards: Control spec execution and annotate why specs are run or skipped.
- Aliased Method Support: A specialized shared spec implementation to handle Ruby's numerous aliased methods.
- Helper Methods: Simplifies common tasks, such as creating temporary file names.
- Specialized Runner Scripts: Includes a configuration facility with default project files and user overrides.
- Tagging: Allows excluding specs known to fail on specific Ruby implementations and managing tags during execution.
Note: MSpec is not a full replacement for RSpec; it provides a subset or superset of RSpec features depending on the context and does not include all RSpec matchers.
Understand the difference between specs and tests
masterIn this repository,
spec/ruby/(specs) andtest/(tests) serve different purposes:- Specs (
spec/ruby/): Designed to document behavior. They use descriptive English to explain what is being tested, typically focus on a single aspect of behavior, and use a syntax like(value).should == expected. - Tests (
test/): Standard assertions using syntax likeassert_equal expected, actual.
Example of a spec's descriptive style:
describe "The for expression" do it "iterates over an Enumerable passing each element to the block" do j = 0 for i in 1..3 j += i end j.should == 6 end end- Specs (
Use version guards for changing Ruby behavior
masterWhen adding new features or modifying existing behavior that changes between Ruby versions, you must use version guards (
ruby_version_is) to ensure compatibility with other Ruby implementations. This allows the specs to run correctly across different versions by isolating version-specific logic.describe "Some spec" do ruby_version_is ""..."2.7" do it "some example" do # Old behavior for Ruby < 2.7 end end ruby_version_is "2.7" do it "some example" do # New behavior for Ruby >= 2.7 end end endUnderstand the differences between StringIO and IO
masterStringIOacts as a pseudoIOclass that allows you to treat aStringas an IO object. However, it has two key differences compared to the standardIOclass:filenomethod: Calling.filenoon aStringIOinstance will raise aNotImplementedError.- Encoding conversion: Encoding conversion is not implemented in
StringIO. Any attempts to perform encoding conversions will be ignored silently.
How the entry.rb program works
masterThe
entry.rbprogram is a specialized Ruby script where the source code consists exclusively of Ruby reserved words. It is designed to run without compile errors, warnings, or runtime errors by carefully structuring these reserved words to satisfy Ruby's syntax requirements.Key techniques used to maintain valid syntax include:
- Method Definitions: Using
def,alias, orundefto treat reserved words as method parameters (e.g.,def class ... enddefines a method namedclass). - Keyword Usage: Utilizing
defined?with reserved words as arguments. - Avoiding Void Value Errors: Using
ororandto prevent syntax errors in expressions that would otherwise result in a "void value expression" (e.g., usingif begin false or return end then true endinstead ofif begin return end then true end). - Structural Constraints: Ensuring keywords like
retryare placed withinrescueblocks, andbreak/next/redoare placed within looping constructs.
- Method Definitions: Using
How Net::Telnet handles socket delegation and raw data
masterNet::Telnetdelegates all methods of a socket object (by default aTCPSocket) to the instance. This allows you to use methods like.close()to end a session or.sysread()to read data directly from the host.Warning: If you use
.sysread()while in telnet mode, you should pass the output through.preprocess()to extract and handle telnet command sequences correctly.How Minitest components work together
masterMinitest is a suite of testing facilities that provides different styles of testing depending on your needs:
- minitest/unit: A fast, simple unit testing framework using standard Ruby classes and assertions.
- minitest/spec: A spec engine that provides a BDD-style DSL (using
describeandit) and hooks intominitest/unitto bridge assertions and expectations. - minitest/mock: A tiny framework for creating mock and stub objects.
- minitest/benchmark: A tool to assert the performance of algorithms in a repeatable way.
- minitest/autorun: The standard way to automatically run all loaded tests when the script exits.
How the Kinaba Pi computation works
masterThe Kinaba program computes the first 10,000 digits of Pi using the following series formula:
Pi/2 = 1 + 1/3 + 1/3*2/5 + 1/3*2/5*3/7 + 1/3*2/5*3/7*4/9 + ...To fit the program within the constraints of the Pi digits (where token lengths must match specific digits), the implementation uses several Ruby techniques:
aliasof global variables: Allows accessing the same value from different token-length positions.srand: Used as a value-store because it returns the "previous seed," allowing values to be written without needing a 1-letter=token.- Token Length Constraints: Lexical tokens are not just space-separated; characters like
*or+are treated as separate tokens. For example,a*b + cdefresults in token lengths[1, 1, 1, 1, 4]rather than[3, 1, 4].
Compatibility and limitations of the Yhara sample
masterThe Yhara sample script relies on specific Ruby internal behaviors, such as using characters in constants within the
Objectclass and intentionally raising exceptions.Limitations:
- JRuby Incompatibility: This program does not work on JRuby because
returndoes not raise an exception in that environment.
- JRuby Incompatibility: This program does not work on JRuby because
Understand the HOTPO implementation trick
masterThe program is an obfuscated implementation that avoids conditional branches and arithmetic operations. It simulates the even/odd logic using string manipulation and regular expression matching via
evalandString#join.How it works
The core logic relies on an
n-length sequence of",fragments. The double-quote"alternates between opening and closing string literals:- If
nis even: The last double-quote closes the string, causing the remainder of theevalexpression to be treated as a comment. This results in a string of commas, where the regex(.)...\1=matches at indexn/2. - If
nis odd: The last double-quote does not close the string, causing theevalexpression to include the text3x+1?. The regex(.)...\1=matches at index3n+1.
Readable Equivalent
This is the logic expressed in a readable format:
n = ARGV[0].to_i begin # do nothing end while begin puts n n = (/(.)...\1=/ =~ eval('[",,,,,"'+ '",'*n + ' ?=].join#"].join("3x+1?")')) end- If
Generate 3D STL files using the DSL
masterThe program uses a Domain Specific Language (DSL) to generate 3D data files in STL format. By default, it generates
wine_glass.stl, but you can modify the shape or create different objects (like a sake cup) by modifying thegen3dblock.In the DSL:
+and-characters are used for visual representation in the code and have the same functional meaning.- The structure of the block defines the geometry of the resulting STL file.
gen3d 'ochoko.stl' do l------------------------l l-ww------------------ww-l l-ww------------------ww-l l-ww++++++++++++++++++ww-l l-ww++++++++++++++++++ww-l l--ww++++++++++++++++ww--l l---wwww++++++++++wwww---l l----wwwwwwwwwwwwwwww----l l----www----------www----l end