{"id":29404532,"url":"https://github.com/janlelis/code","last_synced_at":"2025-07-10T20:13:09.757Z","repository":{"id":29499303,"uuid":"33037192","full_name":"janlelis/code","owner":"janlelis","description":"Displays a Ruby method's source code","archived":false,"fork":false,"pushed_at":"2020-12-29T21:45:45.000Z","size":8,"stargazers_count":24,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-09T13:27:56.126Z","etag":null,"topics":["documentation","hacktoberfest","introspection","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/janlelis.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"MIT-LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-03-28T14:55:50.000Z","updated_at":"2023-11-24T03:16:00.000Z","dependencies_parsed_at":"2022-08-03T15:11:52.783Z","dependency_job_id":null,"html_url":"https://github.com/janlelis/code","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/janlelis/code","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janlelis%2Fcode","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janlelis%2Fcode/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janlelis%2Fcode/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janlelis%2Fcode/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/janlelis","download_url":"https://codeload.github.com/janlelis/code/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janlelis%2Fcode/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264648521,"owners_count":23643669,"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":["documentation","hacktoberfest","introspection","ruby"],"created_at":"2025-07-10T20:13:04.298Z","updated_at":"2025-07-10T20:13:04.967Z","avatar_url":"https://github.com/janlelis.png","language":"Ruby","readme":"# code [![[version]](https://badge.fury.io/rb/code.svg)](https://badge.fury.io/rb/code)\n\nShows a method's code with syntax highlighting. Tries to find a Ruby definition of the method first, then falls back to the C version (if the **core_docs** gem is available).\n\n## Setup\n\n```\ngem install code core_docs\n```\n\n\n## Usage\n\n```ruby\n\u003e\u003e Code.for :require\n# in /home/jan/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:38\n##\n# When RubyGems is required, Kernel#require is replaced with our own which\n# is capable of loading gems on demand.\n#\n# When you call \u003ctt\u003erequire 'x'\u003c/tt\u003e, this is what happens:\n# * If the file can be loaded from the existing Ruby loadpath, it\n#   is.\n# * Otherwise, installed gems are searched for a file that matches.\n#   If it's found in gem 'y', that gem is activated (added to the\n#   loadpath).\n#\n# The normal \u003ctt\u003erequire\u003c/tt\u003e functionality of returning false if\n# that file has already been loaded is preserved.\ndef require path\n  RUBYGEMS_ACTIVATION_MONITOR.enter\n\n  path = path.to_path if path.respond_to? :to_path\n\n  spec = Gem.find_unresolved_default_spec(path)\n  if spec\n    Gem.remove_unresolved_default_spec(spec)\n    gem(spec.name)\n  end\n\n  # If there are no unresolved deps, then we can use just try\n  # normal require handle loading a gem from the rescue below.\n\n  if Gem::Specification.unresolved_deps.empty? then\n    RUBYGEMS_ACTIVATION_MONITOR.exit\n    return gem_original_require(path)\n  end\n\n  # If +path+ is for a gem that has already been loaded, don't\n  # bother trying to find it in an unresolved gem, just go straight\n  # to normal require.\n  #--\n  # TODO request access to the C implementation of this to speed up RubyGems\n\n  spec = Gem::Specification.stubs.find { |s|\n    s.activated? and s.contains_requirable_file? path\n  }\n\n  begin\n    RUBYGEMS_ACTIVATION_MONITOR.exit\n    return gem_original_require(spec.to_fullpath(path) || path)\n  end if spec\n\n  # Attempt to find +path+ in any unresolved gems...\n\n  found_specs = Gem::Specification.find_in_unresolved path\n\n  # If there are no directly unresolved gems, then try and find +path+\n  # in any gems that are available via the currently unresolved gems.\n  # For example, given:\n  #\n  #   a =\u003e b =\u003e c =\u003e d\n  #\n  # If a and b are currently active with c being unresolved and d.rb is\n  # requested, then find_in_unresolved_tree will find d.rb in d because\n  # it's a dependency of c.\n  #\n  if found_specs.empty? then\n    found_specs = Gem::Specification.find_in_unresolved_tree path\n\n    found_specs.each do |found_spec|\n      found_spec.activate\n    end\n\n  # We found +path+ directly in an unresolved gem. Now we figure out, of\n  # the possible found specs, which one we should activate.\n  else\n\n    # Check that all the found specs are just different\n    # versions of the same gem\n    names = found_specs.map(\u0026:name).uniq\n\n    if names.size \u003e 1 then\n      RUBYGEMS_ACTIVATION_MONITOR.exit\n      raise Gem::LoadError, \"#{path} found in multiple gems: #{names.join ', '}\"\n    end\n\n    # Ok, now find a gem that has no conflicts, starting\n    # at the highest version.\n    valid = found_specs.select { |s| s.conflicts.empty? }.last\n\n    unless valid then\n      le = Gem::LoadError.new \"unable to find a version of '#{names.first}' to activate\"\n      le.name = names.first\n      RUBYGEMS_ACTIVATION_MONITOR.exit\n      raise le\n    end\n\n    valid.activate\n  end\n\n  RUBYGEMS_ACTIVATION_MONITOR.exit\n  return gem_original_require(path)\nrescue LoadError =\u003e load_error\n  RUBYGEMS_ACTIVATION_MONITOR.enter\n\n  if load_error.message.start_with?(\"Could not find\") or\n      (load_error.message.end_with?(path) and Gem.try_activate(path)) then\n    RUBYGEMS_ACTIVATION_MONITOR.exit\n    return gem_original_require(path)\n  else\n    RUBYGEMS_ACTIVATION_MONITOR.exit\n  end\n\n  raise load_error\nend\n```\n\n```c\n\u003e\u003e Code.for File, :open #=\u003e nil\n// in io.c:6219\n// call-seq:\n//    IO.open(fd, mode=\"r\" [, opt])                -\u003e io\n//    IO.open(fd, mode=\"r\" [, opt]) { |io| block } -\u003e obj\n// \n// With no associated block, \u003ccode\u003eIO.open\u003c/code\u003e is a synonym for IO.new.  If\n// the optional code block is given, it will be passed +io+ as an argument,\n// and the IO object will automatically be closed when the block terminates.\n// In this instance, IO.open returns the value of the block.\n// \n// See IO.new for a description of the +fd+, +mode+ and +opt+ parameters.\nstatic VALUE\nrb_io_s_open(int argc, VALUE *argv, VALUE klass)\n{\n    VALUE io = rb_class_new_instance(argc, argv, klass);\n\n    if (rb_block_given_p()) {\n\treturn rb_ensure(rb_yield, io, io_close, io);\n    }\n\n    return io;\n}\n\n```\n\n## Goal\n\nBe as powerful as pry's source browsing: https://github.com/pry/pry/wiki/Source-browsing\n\n\n## MIT License\n\nCopyright (C) 2015, 2017 Jan Lelis \u003chttps://janlelis.com\u003e. Released under the MIT license.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjanlelis%2Fcode","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjanlelis%2Fcode","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjanlelis%2Fcode/lists"}