A VS Code extension that copies Ruby class/module names from file paths using Rails-style conventions.
- Quick class name copying: Copy the inferred Ruby constant name for the active file to your clipboard
- Rails conventions: Automatically handles Rails directory structures (
app/models,app/controllers, etc.) - Path-based inference: Converts file paths to Ruby constant names following Rails naming conventions
- Optional code-based inference: Can parse Ruby files to find actual class/module definitions
- Acronym support: Configure custom acronym handling (e.g.,
api_controller.rb→APIController) - Configurable prefixes: Customize which directory prefixes should be stripped
- Open a Ruby file
- Press
Cmd+Shift+P(Mac) orCtrl+Shift+P(Windows/Linux) - Type "Ruby: Copy class name for active file"
- The class name is copied to your clipboard
- Mac:
Cmd+Shift+C - Windows/Linux:
Ctrl+Shift+C
The extension will show a notification with the copied constant name.
Given these file paths:
| File Path | Copied Constant |
|---|---|
app/controllers/hub/api_controller.rb |
Hub::ApiController |
app/models/user.rb |
User |
app/controllers/admin/users_controller.rb |
Admin::UsersController |
app/controllers/api/v1/accounts_controller.rb |
Api::V1::AccountsController |
app/models/hub/oauth_client.rb |
Hub::OauthClient |
lib/money/formatter.rb |
Money::Formatter |
app/services/admin/user_sync/service.rb |
Admin::UserSync::Service |
Configure which directory prefixes should be stripped when inferring class names:
{
"rubyClassName.rootPrefixes": [
"app/models",
"app/controllers",
"app/jobs",
"app/services",
"app/mailers",
"app/workers",
"app/channels",
"app/serializers",
"app/forms",
"app/validators",
"lib"
]
}Map snake_case fragments to their preferred constant forms:
{
"rubyClassName.acronyms": {
"api": "API",
"oauth": "OAuth",
"http": "HTTP",
"url": "URL"
}
}With this configuration:
api_controller.rb→APIController(instead ofApiController)oauth_client.rb→OAuthClient(instead ofOauthClient)
Enable code-based inference to find actual class/module definitions:
{
"rubyClassName.useCodeInference": true,
"rubyClassName.inferencePreference": "code"
}When enabled, the extension will parse the Ruby file to find class or module definitions. The inferencePreference setting determines which method to prefer when both are available:
"path"(default): Use path-based inference"code": Use code-based inference (from actual class/module definitions)
- Get the relative path from workspace root
- Strip the configured root prefix (e.g.,
app/controllers) - Remove the
.rbextension - Convert each path segment from snake_case to CamelCase
- Join segments with
::
Example: app/controllers/hub/api_controller.rb → Hub::ApiController
When enabled, the extension searches for top-level class or module definitions using regex:
class Hub::ApiController
# ...
endThe extension extracts Hub::ApiController directly from the code.
- VS Code 1.85.0 or higher
- Ruby files (
.rbextension)
- Open VS Code
- Go to Extensions (Cmd+Shift+X / Ctrl+Shift+X)
- Search for "Ruby Class Name"
- Click Install
Or install from the command line:
code --install-extension andrewmcodes.ruby-class-name# Using mise (recommended)
mise run install
mise run compile
# Or using pnpm directly
pnpm install
pnpm run compile
# Watch for changes
mise run watch
# Or: pnpm run watchSee TESTING.md for comprehensive testing instructions, including:
- Testing in your local Ruby projects
- Testing custom configurations
- Running unit tests
- Debugging the extension
Quick test:
- Open the extension directory in VS Code
- Press F5 to open a new Extension Development Host window
- Test the extension in the new window
# Run unit tests
mise run test:unit
# Or: pnpm run test:unit
# Lint code
mise run lint
# Or: pnpm run lint
# Format code
mise run format
# Or: pnpm run formatContributions are welcome! Please feel free to submit issues or pull requests.
MIT
- Initial release
- Path-based constant inference
- Optional code-based inference
- Configurable root prefixes and acronyms
- Keyboard shortcut support