Comparison of JavaScript objects and Ruby classes

** Compare JavaScript objects with Ruby classes. ** **

class Fruits

  def initialize(name, color)
    @name = name
    @color = color

  def info
    puts "#{@name}Is#{@color}is."


apple ="Apple", "red")
#Output result:The apples are red.

I will convert the above ruby description to the JavaScript description in my own way. (Please forgive me if there is a simpler description.) Methods in JavaScript are functions, so ...

const Fruits = function(name, color){ //Formal argument = name //this.But@I want to see it as an alternative to.
  this.color = color
  this.getName = function(){ return } //Return that could be omitted in ruby, an error will occur if omitted in JavaScript.
  this.getColor = function(){ return this.color }

const apple = new Fruits('Apple', 'red') //Actual argument//Instance generation
console.log(`${apple.getName()}Is${apple.getColor()}is.`)  //console.logがputsの代替is.
             //instance.Call by function expression name.()If there is no, an error will occur.
//Output result:The apples are red.

However, the following description is possible in JavaScript.

const Fruits = function(name, color){ = name
  this.color = color

const apple = new Fruits('Apple', 'red')
//Output result:The apples are red.

It is possible to output the value of the instance variable in the defined ruby outside the object. If you replace this with the description of ruby, you will use a getter, and it will be the following description.

class Fruits

  def initialize(name, color)
    @name = name
    @color = color

  def name

  def color


apple ="Apple", "red")
puts "#{}Is#{apple.color}is."
#Output result:The apples are red.

That is all.

Thank you for watching until the end.

We would be grateful if you could point out any mistakes or lack of knowledge.

Recommended Posts

Comparison of JavaScript objects and Ruby classes
[Ruby] Classes and instances
Ruby Learning # 29 Classes & Objects
Ruby classes and instances
[Ruby] Singular methods and singular classes
About Ruby classes and instances
Equivalence comparison of Java wrapper classes and primitive types
Ruby methods and classes (basic)
Creating Ruby classes and instances
[Ruby] Singular methods and singular classes
Comparison of how to write Callback function (Java, JavaScript, Ruby)
[Ruby] Creating code using the concept of classes and instances
Comparison of Android Handler Looper and runOnUiThread
Summary of hashes and symbols in Ruby
Thorough comparison of Android and iOS implementations
Memorandum (Ruby: Basic Grammar: Classes and Instances)
[Java / Swift] Comparison of Java Interface and Swift Protocol
[Ruby] Classification and usage of loops in Ruby
Write code using Ruby classes and instances
Sort an array of Ruby homebrew classes
Classes and instances
Chrome59 Comparison of normal and headless mode operation
Differences in writing Java, C # and Javascript classes
[Ruby] "Reference to object" and "Contents of variable"
Basics of Ruby
Ruby on Rails ~ Basics of MVC and Router ~
Kantai Collection Java # 1 Classes and Objects [For Beginners]
Java and JavaScript
Ruby and Gem
[Ruby] Difference between receiver and object. Differences between Ruby objects and JS objects
The comparison of enums is ==, and equals is good [Java]
About the difference between classes and instances in Ruby
A rough note about Ruby arrays and hash objects
Usability of date and time classes in each language
[Technical memo] About the advantages and disadvantages of Ruby
[Ruby] Class nesting, inheritance, and the basics of self
Handling of date and time in Ruby. Use Date and Time properly.
Symbols and Destructive Ruby
java classes, instances, objects
HashMap and HashSet classes
[Ruby] Big Decimal and DECIMAL
About classes and instances
[Java] String comparison and && and ||
mutable and immutable objects
Comparison operators and conditionals
Source of cellular objects
definition of ruby method
Ruby inheritance and delegation
Ruby variables and methods
List and happy classes
java (classes and instances)
[For beginners] Explanation of classes, instances, and statics in Java
[Java] Comparison method of character strings and comparison method using regular expressions
[Ruby] Questions and verification about the number of method arguments
The nth and n + 1st characters of a Ruby string
[Ruby on Rails] Easy scroll animation of javascript (using ScrollReveal.js)
Comparison of Web App for Containers and Azure Container Instances
[Ruby] About the difference between 2 dots and 3 dots of range object.
Impressions and memories of openssl, curl, ruby, homebrew, rbenv installation
Install Ruby 3.0.0 Preview 1 with a combination of Homebrew and rbenv
The difference between programming with Ruby classes and programming without it