GitHub Copilot - Patterns & Exercises
GitHub 🌟
en 🇬🇧
en 🇬🇧
  • Introduction
  • Contributing to the Project
  • General
    • Code completion
    • Comment to code
    • Code to comment
    • Quick Q&A
    • Regular expression
    • Language translation
    • Type hinting
    • Code to document
    • Object generation from structured data
    • Showing examples
  • Client Side Tips
    • Copilot snnipet handling
    • GitHub Copilot Shortcuts
    • Go to definition
    • Pin the files you need
  • Design Patterns
    • AI readable naming convention
    • Consistent coding style
    • High-level architecture first
    • Working on small chunks
    • Context-less Architecture
    • Eliminating a tiny OSS dependency
  • Collaboration
    • AI friendly documentation
    • Coaching on prompts
  • Test
    • Creating unit tests
    • Specify how to generate test code
    • Writing failure case first
    • Writing test cases in natural language first
    • Test only what is necessary
  • Refactoring
    • Writing test code before refactoring
    • Making the calculation part independent
    • Asking with open-ended questions
  • Archived
    • GitHub Copilot Patterns & Exercises Guide
    • Translations
      • German 🇩🇪
      • Spanish 🇪🇸
      • French 🇫🇷
      • Italy 🇮🇹
      • Japanese 🇯🇵
      • Portuguese 🇵🇹
      • Chinese 🇨🇳
Powered by GitBook
On this page
  • Description
  • Example
  • Exercise
  • Checklist for Further Learning
Edit on GitHub
  1. General

Showing examples

Generating code from given examples

Last updated 1 year ago

Description

Using GitHub Copilot, developers can generate code based on provided examples. This can be incredibly useful when you expect the code that produces a specific output. In this pattern, we'll explore how to create a Ruby on Rails model from a given example, such as generating JSON.

Example

The following sample illustrates how you can provide an example in comments and ask GitHub Copilot to generate Ruby on Rails code to create the corresponding model.

# Example of code generation to create the following JSON:
# {
#   "name": "John Smith",
#   "age": 30,
#   "description": "This is a sample description.",
#   "country": "Japan",
#   "title": "Customer Success Architect",
#   "email": "johnsmith@example.com"
# }
rails g model users name:string age:integer description:text country:string title:string email:string

Exercise

  • Exercise 1: Based on the following example, generate the Ruby on Rails code to create a model for books.

    {
      "title": "Book",
      "author": "Jane Doe",
      "price": 19.99
    }
  • Exercise 2: Experiment with different attributes and types in the JSON example, then generate the corresponding Rails code.

  • Exercise 3: Test the generated code in a Rails project to ensure that it creates the expected model.

Checklist for Further Learning

  • Does the generated code accurately reflect the given example, creating the appropriate Ruby on Rails code?

  • What were the key considerations or challenges when generating code from the provided example?

  • How can you further customize or optimize the generated code to suit specific project requirements?