An Interest In:
Web News this Week
- April 27, 2024
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
Ruby VS Code Snippets
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!
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)", "${1/^(\\w+)|,\\s(\\w+)/${2:+
}@$1$2 = $1$2/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|\\w+)/:$1/g}$3", "", "def initialize($1$2)", "${1/^(\\w+)|,\\s(\\w+)/${2:+
}@$1$2 = $1$2/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}/}}", "attr_accessor ${2/(\\w+,\\s|\\w+)/:$1/g}$4", "", "def initialize($2$3)", "${2/^(\\w+)|,\\s(\\w+)/${2:+
}@$1$2 = $1$2/g}$5", "end", "", "$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": [ "@@all = []", "", "${TM_SELECTED_TEXT/(end)$/
@@all << self
end/}", "", "def self.all", "@all", "end" ]}
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", "$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
Original Link: https://dev.to/arkfuldodger/ruby-vs-code-snippets-37id
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To