DEV Community

ArkfulDodger
ArkfulDodger

Posted on • Updated on

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!

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 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"
    ]
}
Enter fullscreen mode Exit fullscreen mode

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

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"
    ]
}
Enter fullscreen mode Exit fullscreen mode

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

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",
    ]
}
Enter fullscreen mode Exit fullscreen mode

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

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"
    ]
}
Enter fullscreen mode Exit fullscreen mode

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

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"
    ]
}
Enter fullscreen mode Exit fullscreen mode

Purpose: creates basic class definition with inheritance for use with the ActiveRecord ORM gem

Demo:
Animated Gif showing demo of ActiveRecord Class Snippet

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

Top comments (1)

Collapse
 
svgatorapp profile image
SVGator

"I have a minor addiction to writing snippets." - that's a cool problem to have lol
Great work!