Comparing JavaScript's and Ruby's Classes
The tables below show the syntactically highlighted Classes topic for the two languages.
If you see any incorrect information, please help us improve! Feel free to either create an issue or use the links at the bottom of the language columns to correct any information.
Defining Classes
Concept
JavaScript's Implementation
Ruby's Implementation
Normal class
class ClassName {
// Class body containing variables and methods
}
Newer versions of Javascript permit class but are prototypes (an alternative approach to the object concept) which is the core concept of Javascript.
class Car
# class body
end
Abstract class
Not Implemented In This Language
Unknown
Interface
Not Implemented In This Language
Unknown
Read-only class
Not Implemented In This Language
Unknown
Static class
Not Implemented In This Language
Unknown
Inner class
Not Implemented In This Language
class Car
class CarPart
# car part body
end
end
Packages
A package manager like NPM (https://www.npmjs.com/) or Deno (https://deno.land/) can be used to create and manage Javascript packages.
Unknown
Class with a generic type
Not Implemented In This Language
Unknown
Adding Private and Public Members
Concept
JavaScript's Implementation
Ruby's Implementation
Defining private variables
class ClassName {
#privateField = 0;
}
Unknown
Defining protected variables
Not Implemented In This Language
Unknown
Defining public variables
class ClassName {
publicField = 0;
}
class Car
attr_accessor :model_name
end
Defining static variables
class ClassName {
static staticField = 0;
}
class Car
class << self
attr_accessor :makes
end
end
Defining private functions
Not Implemented In This Language
Unknown
Defining protected functions
Not Implemented In This Language
Unknown
Defining public functions
class ClassName {
publicMethod() {
// Method body
}
}
class Car
def drive
# drive implementation
end
end
Defining static functions
class ClassName {
static staticMethod() {
// Method body
}
}
class Car
def.class_method
# method body
end
#OR
class << self
def class_method
# method body
end
end
end
Extending and Implementing Classes
Concept
JavaScript's Implementation
Ruby's Implementation
Class that inherits/extends another class
class ChildClassName extends ParentClassName {
// Class body containing variables and methods
}
class Sedan < Car
# class body
end
Class/Interface that inherits/extends another class/interface
Not Implemented In This Language
Unknown
Calling a superclass function
super.methodName();
class Sedan < Car
def drive
super
# custom drive implementation
end
end
Overriding a superclass function
class ChildClassName extends ParentClassName {
publicMethod() {
// Method body
// This overrides publicMethod() in the parent class
}
}
class Sedan < Car
def drive
# override drive implementation
end
end
No special notation is needed to override super class functions in ruby
Creating Objects and Polymorphism
Concept
JavaScript's Implementation
Ruby's Implementation
Instantiating a new object
const objectName = new ClassName();
class Car
end
car = Car.new
Instantiating a polymorphic object
Not Implemented In This Language
Unknown
Constructors and Deconstructor
Concept
JavaScript's Implementation
Ruby's Implementation
Implementing a class constructor
class ClassName {
constructor() {
// Constructor body
}
}
class Car
def initialize
# create new instance
end
end
Implementing a class deconstructor
Not Implemented In This Language
Unknown