Ruby VS Code Snippets
ArkfulDodger
Posted on April 8, 2022
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:
My blog post on VS Code Snippet Transformations
The program I use to record my gifs:
Snippet List
- Initialize with Variable Assignments
- Initialize with Attr_Accessor
- Class Setup
- All Variable
- ActiveRecord Model Class
Initialize with Variable Assignments
"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
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
"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:
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
"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
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
"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
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
"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
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
Posted on April 8, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.