Skip to content

Ruby

cotrTypesBoolTrue

Ruby Boolean True

true

cotrVarString

Ruby Create String Variable

${1:my_string} = ${2:'Your string here'}

cotrVarNum

Ruby Create Float Variable

${1:my_float} = ${2:0.0}

cotrSwitch

Ruby Case Statement

case ${1:variable}
when ${2:value1}
# Your code here
when ${4:value2}
# Your code here
else
# Your code here
end

cotrEqual

Ruby Equal To

==

cotrVarStatic

Ruby Static Variable

@@${1:my_static_var} = ${2:value}
# Access the class variable
# MyClass.class_variable_get(:@@${1:my_static_var})

cotrTypesInt

Ruby Integer Type

Integer

cotrConst

Ruby Create Constant

${1:MY_CONST} = $2

cotrTypesNum

Ruby Double Type

double

cotrTypesBoolFalse

Ruby Boolean False

false

cotrVarMap

Ruby Create Hash Variable

${1:my_hash} = {
${2:key1}: ${3:value1},
${4:key2}: ${5:value2},
# Add more key-value pairs here
}

cotrEntry

Ruby Entry Point

# Ruby Entry Point
# To run this program, use: `ruby filename.rb`
puts 'Hello, World!'

cotrStructure

Ruby Project Structure (High-Level)

# Recommended High-Level Ruby Project Structure:
# - /
# - Gemfile: Defines project dependencies.
# - Rakefile: Provides automated tasks like database migrations, running tests, etc.
# - config.ru: Rack configuration file for Rack-based applications.
# - app/
# - Contains the main application code.
# - models/: Contains the models for an MVC framework.
# - controllers/: Contains the controllers for an MVC framework.
# - views/: Contains the views for an MVC framework.
# - config/
# - Configuration files for the application.
# - db/
# - Database related files, like migrations and seeds.
# - lib/
# - Library code that can be reused in other projects.
# - public/
# - Static files like images, stylesheets, and JavaScript files.
# - test/ or spec/
# - Test files, following the naming convention of the testing framework (e.g., RSpec uses `spec`).
# Note:
# - This structure can vary, especially with different frameworks like Rails, Sinatra, etc.
# - Ensure that the project structure aligns with the conventions of the used framework.

cotrTypes

Ruby Types

$BLOCK_COMMENT_START
Ruby is a dynamically typed language.
Some common types in Ruby include:
- Integer: Integer
- Float: Floating-point number
- String: String
- Symbol: Symbol
- Array: Array
- Hash: Hash (associative array)
- Range: Range
- Regexp: Regular expression
- TrueClass, FalseClass: Boolean values
- NilClass: Represents the absence of a value
- Proc: Block of code (lambda, proc)
- Method: Method object
- Class, Module: Class and module objects
- Time: Time object
- File: File object
- IO: Input/output stream
- Exception: Base class for exceptions
$BLOCK_COMMENT_END

cotrTypesList

Ruby List Type

Array

cotrInterpolate

Ruby Interpolate String

"Your string here \#{${1:variable}}"

cotrTypesBool

Ruby Boolean Type

# Ruby uses true and false for boolean values.

cotrPrint

Ruby Print

puts ${1:'Your message here'}

cotrThrow

Ruby Throw Exception

raise ${1:'Your message here'}

cotrOperators

Ruby Mathematical Operators

# Ruby Mathematical Operators
# Addition: +
# Subtraction: -
# Multiplication: *
# Division: /
# Modulus (Remainder): %
# Exponentiation: **
# Increment: Use += 1
# Decrement: Use -= 1
# Assignment: =
# Addition assignment: +=
# Subtraction assignment: -=
# Multiplication assignment: *=
# Division assignment: /=
# Modulus assignment: %=
# Exponentiation assignment: **=

cotrDynamic

Ruby Dynamic Type

# Ruby is a dynamically typed language.

cotrVarSyntax

Variable Declaration Syntax

# Ruby Variable Declaration Syntax:
# - Ruby is dynamically typed, so there is no explicit type declaration.
# - Variables are created when you first assign a value to them.
# - Variables can be reassigned to different types.
# Note:
# - Ruby does not have direct equivalents to 'var', 'let', or 'const'.
# - Use descriptive variable names to improve code readability.

cotrConcat

Ruby Concatenate Strings

"${1:string1}" + "${2:string2}"

cotrVarStringMulti

Ruby Create Multiline String Variable

${1:my_string} = <<~HEREDOC
${2:Your string here}
HEREDOC

cotrFuncSyntax

Ruby Function Syntax

# Ruby Function Syntax
# Basic function: def function_name(parameters) ... end
# Function with arguments: def function_name(arg1, arg2, ...) ... end
# Function with named arguments: def function_name(arg1: value1, arg2: value2, ...) ... end

cotrClass

Ruby Class

class ${1:MyClass}
${2:# Your code here}
end

cotrTypeCompare

Ruby Type Comparison

# Check if two variables have the same type:
if ${1:variable1}.class == ${2:variable2}.class
# Your code here
end

cotrTypesDate

Ruby Date Type

require 'date'
Date

cotrNull

Ruby Null Type

nil

cotrVarList

Ruby Create Array Variable

${1:my_array} = [
${2:'item1'},
${3:'item2'},
# Add more items here
]

cotrGenList

Ruby Generate Array

${1:my_array} = Array.new(${2:length}) { |i| ${3:'item'} + i.to_s }

cotrFuncAnon

Ruby Anonymous Function

lambda { |${1:parameters}|
${2:# Your code here}
}

cotrTypesString

Ruby String Type

String

cotrTypesMap

Ruby Map Type

Hash

cotrFuncLambda

Ruby Lambda Function

${2:my_function} = ->(${3:parameters}) {
${4:# Your code here}
}

cotrNow

Ruby Date Now

require 'date'
Date.today

cotrVar

Ruby Create Variable

${1:my_var} = $2

cotrVarDate

Ruby Create Date Variable

${1:my_date} = Date.new(${2:year}, ${3:month}, ${4:day})

cotrForLoop

Ruby For Loop

${1:10}.times do |${2:i}|
# Your code here
end

cotrFunc

Ruby Function

def ${2:my_function}(${3:parameters})
${4:# Your code here}
end

cotrComment

Ruby Comment

# ${1:Your comment here}

cotrTypeConvert

Ruby Type Conversion

# Ruby Type Conversion:
# Implicit conversions (Ruby performs automatically):
# - Can be unpredictable, especially with loose equality (==).
# Explicit conversions:
# - variable.to_i // Converts to integer
# - variable.to_f // Converts to float
# - variable.to_s // Converts to string
# - !!variable // Converts to boolean
# Note:
# - Be aware of implicit conversions and use explicit conversions when necessary for clarity and control.

cotrTypesChar

Ruby Character Type

# Ruby does not have a separate char type; use String instead.

cotrVarTyped

Ruby Create Typed Variable

# Note: Ruby is dynamically typed; explicit type annotations are not used.
${1:my_var} = $2

cotrTryCatch

Ruby Try Catch

begin
${1:# Your code here}
rescue => ${2:exception}
${3:# Your code here}
end

cotrIf

Ruby If Statement

if ${1:condition}
${2:# Your code here}
end

cotrTernary

Ruby Ternary Operator

${1:condition} ? ${2:true_value} : ${3:false_value}

cotrInfo

Ruby Info

Typing: Dynamically typed
Paradigm: Multi-paradigm: object-oriented, imperative, functional, reflective
Compilation: Interpreted
Concurrency: Supports multi-threading and concurrent programming with fibers

cotrNotEqual

Ruby Not Equal To

!=

cotrTypeCheck

Ruby Type Check

${1:variable}.class

cotrTypesNumAlt

Ruby Float Type

Float

cotrVarInt

Ruby Create Integer Variable

${1:my_int} = ${2:0}

cotrVarBool

Ruby Create Boolean Variable

${1:my_bool} = ${2:true}

cotrForInLoop

Ruby For…In Loop

${1:iterable}.each do |item|
# Your code here
end

cotrFuncArgsNamed

Ruby Function Named Args

def ${2:my_function}(${3:arg1}: ${4:value1}, ${5:arg2}: ${6:value2})
${7:# Your code here}
end

cotrIfElse

Ruby If Else Statement

if ${1:condition}
${2:# Your code here}
elsif ${3:condition}
${4:# Your code here}
else
${5:# Your code here}
end

cotrOperatorsBool

Ruby Boolean Operators

# Ruby Boolean Operators
# Logical AND: && or and
# Logical OR: || or or
# Logical NOT: ! or not
# Equality: ==
# Inequality: !=
# Greater than: >
# Less than: <
# Greater than or equal to: >=
# Less than or equal to: <=

cotrPrintMulti

Ruby Print Multi

puts ${1:"""Line 1}
${2:Line 2}
${3:Line 3}"""}

cotrWhileLoop

Ruby While Loop

while ${1:condition} do
# Your code here
end

cotrFuncArgs

Ruby Function Args

# In Ruby, functions can have arguments with default values.
def ${2:my_function}(${3:arg1}, ${4:arg2}=${5:'defaultVal'})
${6:# Your code here}
end

cotrCommentMulti

Ruby Multi-Line Comment

=begin
${1:Your comment here}
=end

cotrEnum

Ruby Enum (with Symbols)

${1:MyEnum} = [:${2:value1}, :${3:value2}]
# Access: ${1:MyEnum}[0] # :${2:value1}