Ruby VS Code Snippets
Tags: vscode, ruby, productivity, snippets
Published: 2022-04-08 19:38:31

I have a minor addiction to writing snippets. Below are a few of the ones I have created for use in programming with Ruby. Feel free to add these to your own snippet files in VS Code!

**UPDATE: 4/11/21:** Init Var, Init Attr, and Class Setup snippets updated. Will now work with keyword arguments and arguments with default values

**Ref for adding custom snippets to VS Code:**
- [Stack Overflow: How to Add Custom Code Snippets in VSCode](https://stackoverflow.com/questions/29995863/how-to-add-custom-code-snippets-in-vscode#answers)
- [VS Code Snippets Docs](https://code.visualstudio.com/docs/editor/userdefinedsnippets)

**My blog post on VS Code Snippet Transformations**
- [VS Code Snippet Transformations](https://dev.to/arkfuldodger/transforming-vs-code-snippets-4jcc)

**The program I use to record my gifs:**
- [LICEcap](https://www.cockos.com/licecap/)

## Snippet List
* [Initialize with Variable Assignments](#init-with-var)
* [Initialize with Attr_Accessor](#init-with-attr)
* [Class Setup](#class-setup)
* [All Variable](#all-var)
* [ActiveRecord Model Class](#ar-class)

---

## Initialize with Variable Assignments <a name="init-with-var"></a>
```json
"Initialize With Var Assign" : {
	"prefix": "init var",
	"body": [
		"def initialize($1$2)",
		"\t${1/^(\\w+)((:\\s*|\\s*=\\s*)((\"[^\"]*\"|'[^']*'|\\[[^\\]]*\\]|{[^}]*})?[^,\"'\\[{]*)*)?|,\\s*(\\w+)((:\\s*|\\s*=\\s*)((\"[^\"]*\"|'[^']*'|\\[[^\\]]*\\]|{[^}]*})?[^,\"'\\[{]*)*)?/${6:+\n\t}@$1$6 = $1$6/g}$0",
		"end"
	]
}
```

**Purpose:** creates initialize method for a class, taking arguments which are assigned to class-scoped variables

**Demo:**
![Animated Gif showing demo of Init Var Snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vutpq82c5omf2mmwutv2.gif)

**How-To:**
- **Prefix:** "init var"
- **Tabstop 1:** enter any parameters which should be set as class variables, separated by a comma
 - _format will update after tabbing out of this tabstop_
- **Tabstop 2:** enter any parameters which should not be set as class variables, separated by a comma
- **Tabstop 0:** cursor placed within end of initialize method

---

## Initialize with Attr_Accessor <a name="init-with-attr"></a>
```json
"Initialize With Attr" : {
	"prefix": "init attr",
	"body": [
		"attr_accessor ${1/^(\\w+)((:\\s*|\\s*=\\s*)((\"[^\"]*\"|'[^']*'|\\[[^\\]]*\\]|{[^}]*})?[^,\"'\\[{]*)*)?|,\\s*(\\w+)((:\\s*|\\s*=\\s*)((\"[^\"]*\"|'[^']*'|\\[[^\\]]*\\]|{[^}]*})?[^,\"'\\[{]*)*)?/${6:+, }:$1$6/g}$3",
		"",
		"def initialize($1$2)",
		"\t${1/^(\\w+)((:\\s*|\\s*=\\s*)((\"[^\"]*\"|'[^']*'|\\[[^\\]]*\\]|{[^}]*})?[^,\"'\\[{]*)*)?|,\\s*(\\w+)((:\\s*|\\s*=\\s*)((\"[^\"]*\"|'[^']*'|\\[[^\\]]*\\]|{[^}]*})?[^,\"'\\[{]*)*)?/${6:+\n\t}@$1$6 = $1$6/g}$0",
		"end"
	]
}
```

**Purpose:** creates initialize method for a class, as with the prior snippet, but additionally creates attr_accessors for each class variable set

**Demo:**

![Animated Gif showing demo of Init Attr Snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mfddp5hynxlu0o9xk675.gif)

**How-To:**
- **Prefix:** "init attr"
- **Tabstop 1:** enter any initialize method parameters which should be set as class variables and attr_accessors, separated by a comma
 - _format will update after tabbing out of this tabstop_
- **Tabstop 2:** enter any initialize method parameters which should not be set as class variables, separated by a comma
- **Tabstop 3:** enter any additional attr_accessors, or enter other needed code such as attr_readers on a new line
- **Tabstop 0:** cursor placed within end of initialize method

---

## Class Setup <a name="class-setup"></a>
```json
"Class Setup" : {
	"prefix": "class setup",
	"body": [
		"class ${1:${TM_FILENAME_BASE/[^a-zA-Z]*([a-zA-Z]+.*_.*)|[^a-zA-Z]*([a-zA-Z].*)/${1:/pascalcase}${2:/capitalize}/}}",
		"\tattr_accessor ${2/^(\\w+)((:\\s*|\\s*=\\s*)((\"[^\"]*\"|'[^']*'|\\[[^\\]]*\\]|{[^}]*})?[^,\"'\\[{]*)*)?|,\\s*(\\w+)((:\\s*|\\s*=\\s*)((\"[^\"]*\"|'[^']*'|\\[[^\\]]*\\]|{[^}]*})?[^,\"'\\[{]*)*)?/${6:+, }:$1$6/g}$4",
		"",
		"\tdef initialize($2$3)",
		"\t\t${2/^(\\w+)((:\\s*|\\s*=\\s*)((\"[^\"]*\"|'[^']*'|\\[[^\\]]*\\]|{[^}]*})?[^,\"'\\[{]*)*)?|,\\s*(\\w+)((:\\s*|\\s*=\\s*)((\"[^\"]*\"|'[^']*'|\\[[^\\]]*\\]|{[^}]*})?[^,\"'\\[{]*)*)?/${6:+\n\t\t}@$1$6 = $1$6/g}$5",
		"\tend",
		"",
		"\t$0",
		"end",
	]
}
```
**Purpose:** creates initialize method and attr_accessors, as with the prior snippet, but also creates the containing class definition which derives its name from the filename

**Demo:**
![Animated Gif showing demo of Class Setup Snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jlk1qdgtstvo6dpsk0p3.gif)

**How-To:**
- **Prefix:** "class setup"
- **Tabstop 1:** enter class name
 - _by default, will be set to file name in PascalCase (UpperCamelCase) omitting leading numbers_
- **Tabstop 2:** enter any initialize method parameters which should be set as class variables and attr_accessors, separated by a comma
 - _format will update after tabbing out of this tabstop_
- **Tabstop 3:** enter any initialize method parameters which should not be set as class variables, separated by a comma
- **Tabstop 4:** enter any additional attr_accessors, or enter other needed code such as attr_readers on a new line
- **Tabstop 5:** enter any additional code within initialize method
- **Tabstop 0:** cursor placed within end of class definition

---

## All Variable <a name="all-var"></a>
```json
"All Class Variable" : {
	"prefix": "all",
	"body": [
		"\t@@all = []",
		"",
		"${TM_SELECTED_TEXT/(end)$/\n\t\t@@all << self\n\tend/}",
		"",
		"\tdef self.all",
		"\t\t@all",
		"\tend"
	]
}
```
**Purpose:** for an existing class, creates class-level **@@all** variable to contain array of class instances, adds instance to array within initialize method, and creates class-level get method

**Demo:**
![Animated Gif showing demo of All Variable Snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ncvscsbvt4gtj61hfa76.gif)

**How-To:**
- **Select:** initialize method, starting with the beginning of the first row through the final word "end" closing the method definition
 - _NOTE_: the "end" must be the final characters in your selection for the snippet to work properly. If they are not, the shovel method within initialize will not generate
- **Type Prefix:** "all"
- **Tabstop 0:** cursor placed at end of getter method

---

## ActiveRecord Model Class <a name="ar-class"></a>
```json
"ActiveRecord Class" : {
	"prefix": "ar class",
	"body": [
		"class ${1:${TM_FILENAME_BASE/[^a-zA-Z]*([a-zA-Z]+.*_.*)|[^a-zA-Z]*([a-zA-Z].*)/${1:/pascalcase}${2:/capitalize}/}} < ActiveRecord::Base",
		"\t$0",
		"end"
	]
}
```
**Purpose:** creates basic class definition with inheritance for use with the ActiveRecord ORM gem

**Demo:**
![Animated Gif showing demo of ActiveRecord Class Snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u1jcnfv559psxlmsma32.gif)

**How-To:**
- **Prompt:** "ar class" 
- **Tabstop 1:** enter class name
 - _by default, will be set to file name in PascalCase (UpperCamelCase) omitting leading numbers_
- **Tabstop 0:** cursor placed within class definition