{"id":22981020,"url":"https://github.com/rubyworks/capsule","last_synced_at":"2025-08-13T17:33:46.673Z","repository":{"id":665101,"uuid":"308193","full_name":"rubyworks/capsule","owner":"rubyworks","description":"Encapsulate Ruby Scripts","archived":false,"fork":false,"pushed_at":"2012-05-27T17:37:05.000Z","size":730,"stargazers_count":3,"open_issues_count":2,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-21T05:32:10.156Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://rubyworks.github.com/capsule","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rubyworks.png","metadata":{"files":{"readme":"README.rdoc","changelog":"HISTORY.rdoc","contributing":null,"funding":null,"license":"COPYING.rdoc","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2009-09-15T20:54:10.000Z","updated_at":"2015-09-10T03:48:32.000Z","dependencies_parsed_at":"2022-07-05T07:01:57.709Z","dependency_job_id":null,"html_url":"https://github.com/rubyworks/capsule","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubyworks%2Fcapsule","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubyworks%2Fcapsule/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubyworks%2Fcapsule/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubyworks%2Fcapsule/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rubyworks","download_url":"https://codeload.github.com/rubyworks/capsule/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229773540,"owners_count":18122031,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-15T01:46:44.972Z","updated_at":"2024-12-15T01:46:45.751Z","avatar_url":"https://github.com/rubyworks.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"= Capsule\n\n{Homepage}[home: http://rubyworks.github.com/capsule] |\n{Documentation}[http://rubydoc.info/gems/capsule] |\n{Development}[http://github.com/rubyworks/capsule] |\n{Report Issue}[http://github.com/rubyworks/capsule/issues] |\n{Mailing List}[http://groups.google.com/group/rubyworks-mailinglist] |\n{IRC Channel}[irc://irc.freenode.net/rubyworks]\n\n{\u003cimg src=\"http://travis-ci.org/rubyworks/capsule.png\" /\u003e}[http://travis-ci.org/rubyworks/capsule]\n\n\n== Description\n\nCapsule is a subclass of Module. A module which is an instance of the Capsule\nclass encapsulates in its scope the top-level methods, top-level constants, and\ninstance variables defined in a Ruby script (and its dependent files)\nloaded by a Ruby program. This allows use of script files to define objects that\ncan be loaded into a program in much the same way that objects can be loaded\nfrom YAML or Marshaled files. There is also an autoimport method which functions\nlike Ruby's autoload but based on is Capsule.load.\n\n\n== Synopsis\n\nTo encapsulate a script in a Capsule:\n\n  myscript = Capsule.new('myscript.rb')\n\nIf the script is in Ruby's $LOAD_PATH, then you can use +Capsule.load+.\n\n  myscript = Capsule.load('myscript.rb')\n\nHere is an example:\n\n  # myscript.rb\n\n  VALUE = [1,2,3]\n\n  def run\n    puts \"#{self} is running.\"\n  end\n\nAnd the encapsulating program:\n\n  # program.rb:\n\n  require 'capsule'\n\n  myscript = Capsule.load(\"myscript.rb\")\n\n  p myscript::VALUE\n\n  myscript.run\n\nRunning `program.rb` will result in:\n\n  $ ruby program.rb\n  [1, 2, 3]\n  #\u003cCapsule:myscript.rb\u003e is running.\n\n\n== Usage\n\nCapsule modules are instantiated with \u003ctt\u003eCapsule.new(main_file)\u003c/tt\u003e or the alias\n\u003ctt\u003eCapsule.load(main_file)\u003c/tt\u003e. All the top-level constants and top-level\nmethods that are defined in the +main_file+ and its dependent local files (see\nbelow) are scoped in the same Capsule module, and are thereby available to the\ncalling program.\n\nThe +main_file+ can load or require other files with +load+ and +require+, as\nusual. These methods, in the Capsule context, add some behavior to the +Kernel+\n+load+ and +require+ methods: \u003ctt\u003eCapsule#load\u003c/tt\u003e and \u003ctt\u003eCapsule#require\u003c/tt\u003e\nfirst search for files relative to the +main_file+'s dir. Files loaded in this\nway (\"dependent local files\") are treated like the script file itself: top-level\ndefinitions are added to the script module that is returned by +load+ or\n+require+.\n\nBoth \u003ctt\u003eCapsule#load\u003c/tt\u003e and \u003ctt\u003eCapsule#require\u003c/tt\u003e fall back to the Kernel\nversions if the file is not found locally. Hence, other ruby libraries can be\nloaded and required as usual, assuming their names do not conflict with local\nfile names. Definitions from those files go into the usual scope (typically\nglobal). The normal ruby +load+ and +require+ behavior can be forced by calling\n\u003ctt\u003eKernel.load\u003c/tt\u003e and \u003ctt\u003eKernel.require\u003c/tt\u003e.\n\nA Capsule immitates the way the top-level ruby context works, so a ruby file that\nwas originally intended to be run from the top level, defining top-level\nconstants and top-level methods, can also be run as a Capsule, and its top-level\nconstants and top-level methods are wrapped in the script's scope. The\ndifference between this behavior and simply wrapping the loaded definitions in\nan _anonymous_ module using \u003ctt\u003eKernel.load(main_file, true)\u003c/tt\u003e is that the\ntop-level methods and top-level constants defined in the script are accessible\nusing the Capsule instance.\n\nThe top-level definitions of a Capsule can be accessed after it has been\nloaded, as follows:\n\n\u003ctt\u003ecapsule.meth\u003c/tt\u003e\n\n- Call a method defined using \u003ctt\u003edef meth\u003c/tt\u003e or \u003ctt\u003edef self.meth\u003c/tt\u003e in\n  the script file.\n\n\u003ctt\u003ecapsule::K\u003c/tt\u003e\n\n- Access a class, module, or constant defined using \u003ctt\u003eK = val\u003c/tt\u003e in the\n  script file.\n\nAn \"input\" can be passed to the script before loading. Simply call Capsule.new\n(or Capsule.load) with a block. The block is passed a single argument, the\nCapsule module, and executed before the files are loaded into the Capsule's\nscope. Setting a constant in this block makes the constant available to the\nscript during loading. For example:\n\n  script = Capsule.load(\"my-script.rb\") { |capsule| capsule::INPUT = 3 }\n\nNote that all methods defined in the script file are both instance methods of\nthe module and methods of the module instance (the effect of\n\u003ctt\u003eModule#module_function\u003c/tt\u003e). So \u003ctt\u003einclude\u003c/tt\u003e-ing a Capsule module in a\nclass will give instances of the class all the methods and constants defined in\nthe script, and they will reference the instance's instance variables,\nrather than the Capsule module's instance variables.\n\nThe Capsule class was inspired by Nobu Nokada's suggestion in\nhttp://ruby-talk.org/62727, in a thread (started in http://ruby-talk.org/62660)\nabout how to use ruby script files as specifications of objects.\n\n\n== Installation\n\nTo install with RubyGems simply open a console and type:\n\n  gem install capsule\n\nLocal installation requires Setup.rb (gem install setup),\nthen download the tarball package and type:\n\n  tar -xvzf capsule-1.0.0.tgz\n  cd capsule-1.0.0\n  sudo setup.rb all\n\nWindows users use 'ruby setup.rb all'.\n\n\n== Legal\n\n(FreeBSD License)\n\nCopyright (c) 2007 Thomas Sawyer\n\nUnless otherwise agreed upon by the copyright holder, this program is\nditributed under the terms of the BSD-2-Clause license.\n\nCapsule is based on Joel VanderWerf's Script library.\n\nCopyright (c) 2004 Joel VanderWerf\n\nSee COPYING.rdoc file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frubyworks%2Fcapsule","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frubyworks%2Fcapsule","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frubyworks%2Fcapsule/lists"}