Differences in writing in Ruby, PHP, Java, JS

Introduction

"I learned the basics of programming in Ruby, so let's try js next time!" ↓ "I don't know what it is because it's messed up!"

Is there such a person?

I myself used to stuff it too much and it became a bun, so I tried to summarize the differences in writing style in an easy-to-understand manner. You don't have to remember each one, so please stock this article. I think you will deepen your understanding of object-oriented programming, so please take a look to the end!

Character output

Output of basic characters. It's difficult because the processing is different depending on the language, but it's basic, so let's hold it firmly. After all Java has a long description w

python


  #Ruby
  puts "Hello"

python


  #PHP
  echo "Hello";
  //Java
  System.out.println("Hello");
  //JavaScript
  console.log("Hello");

Variable assignment

It is a super basic of programming, variable assignment.

python


  #Ruby
  name = "Tom"

python


  #PHP
  $name = "Tom";
  //Java(Specify a type such as String, int, etc.)
  String name = "Tom";
  //JavaScript
  let name = "Tom";

Variable expansion, string + variable

I also use this a lot. They are all similar, but there are differences. * The writing method is just an example.

python


  #Ruby
  "my name is#{name}is."

python


  #PHP
  "my name is{$name}is."
  //Java
  "my name is"+ name + "is."
  //JavaScript(''not``(Back quotation)Surround with)
  `my name is${name}is.`

if statement

The representative of processing is the if statement. Note the difference in elsif.

python


  #Ruby
  if age >= 20
    #processing
  elsif age >= 10
    #processing
  else
    #processing
  end

python


  #PHP
  if (age>= 20){
    #processing
  }elseif (age >= 10){
    #processing
  }else{
    #processing
  }
  //Java & JavaScript
  if (age>= 20){
    //processing
  }else if (age >= 10){
    //processing
  }else{
    //processing
  }

Array and how to retrieve

I often use array retrieval, and there are various methods for extracting one by one depending on the language.

python


  #Ruby
  names = ["Tom","Kenta","John"]
  names[0]

python


  #PHP
  $names = array("Tom","Kenta","John");
  $names[0];
  //Java
  String names[] = {"Tom","Kenta","John"};
  names[0];
  //JavaScript
  const names = ["Tom","Kenta","John"];
  names[0];

Hash, associative array, object and how to retrieve

Be careful when searching, as the names differ depending on the language.

python


  #Ruby(hash)
  user = {name: "Tom",age: 20}
  user[:name]

python


  #PHP(Associative array)
  $user = array("name" => "Tom","age" => 20)
  $user["name"]
  //JavaScript(object)
  const user = {name: "Tom",age: 20};
  user.name

Normal methods and functions

This is the important point. This time I created an add method (function) that sums the two values. The return value is also important, so hold it firmly.

python


  #Ruby
  def add(a, b)
    return a + b
  end
  sum = add(5, 2)

python


  #PHP
  function add($a, $b){
    return $a + $b;
  }
  $sum = add(5, 2);
  //Java
  public static int add(int a, int b){
  //int after static specifies the return type, use void for methods with no return value
    return a + b; 
  }
  int sum = add(5, 2);
  //JavaScript
  const add = function(a, b){
    return a + b;
  };
  let sum = add(5,2);

  //Or
  const add = (a, b) => {
    return a + b;
  };
  let sum = add(5,2);

  //Or
  function add (a, b){
    return a + b;
  }
  let sum = add(5,2);

The recommended description of js differs depending on the version.

Class and instance creation

The long-awaited object-oriented beginning! A Menu class is created, and an instance is created based on that class and assigned to the variable menu1.

python


  #Ruby
  class Menu
    ##processing
  end
  menu1 = Menu.new

python


  #PHP
  class Menu{
    ##processing
  }
  $menu1 = new Menu();
  //Java
  class Menu{
    //processing
  }
  Menu menu1 = new Menu();
  //JavaScript
  class Menu{
    //processing
  }
  const menu1 = new Menu();

Definition of instance variables, instance fields, and properties

The menu contains "information" such as name and price. Declare it in advance in the class.

python


  #Ruby
  attr_accessor :name
  attr_accessor :price

python


  #PHP
  private $name;
  private $price;
  //Java
  private String name;
  private int price;
  //JavaScript
  //No predefinition required?

Assignment to instance variables and properties in the initial method

Now, I've come to a place I don't understand! The initial method is the method that is called when it is new (the very first). This time, the value specified in the new argument is entered in the variable as instance information in the initial method.

python


  #Ruby
  def initialize(name, price)
    self.name = name
    self.price = price
  end
  menu1 = Menu.new("Hamburger",300)

python


  #PHP
  public function __construct($name,$price){
    this->name = $name;
    this->price = $price;
  }
  $menu1 = new Menu("Hamburger",300);
  //Java
  //Define a method with the same name as the class in the class
  Menu(String name, int price){
    this.name = name;
    this.price = price;
  }
  Menu menu1 = new Menu("Hamburger",300)
  //JavaScript
  constructor (name, price){
    this.name = name;
    this.price = price;
  }
  const menu1 = new Menu("Hamburger",300)

Instance methods and calls

Well last. Instances have "processing" in addition to "information". This time, how to define and call the process. Since the instance created from the Menu class is assigned to the variable menu1, The output will be "This hamburger costs 300 yen".

python


  #Ruby
  def show
    puts "this#{self.name}Is#{self.price}It's a yen"
  end
  menu1.show

python


  #PHP
  public function show(){
    echo "this{$this->name}Is{$this->price}It's a yen";
  }
  $menu1->show();
  //Java
  public void show(){
    System.out.println("this"+this.name+"Is"+this.price+"It's a yen");
  }
  menu1.show();
  //JavaScript
  show(){
    console.log(`this${this.name}Is${this.price}It's a yen`);
  }
  menu1.show();

At the end

Thank you for watching until the end. There may be omissions and mistakes, but please be patient! !!

It is said that it is not so good to learn shallowly and widely in this world, but I think it is good to try another language to deepen your knowledge.

Recommended Posts

Differences in writing in Ruby, PHP, Java, JS
Differences in writing Java, C # and Javascript classes
Implement PHP implode function in Java
Continued Talk about writing Java in Emacs @ 2018
The story of writing Java in Emacs
Reading and writing gzip files in Java
Partization in Java
Ruby vertical writing
Changes in Java 11
[Understanding] Differences between hashes and arrays in Ruby
Class in Ruby
Rock-paper-scissors in Java
Summarize the differences between C # and Java writing
Heavy in Ruby! ??
Writing code Ruby
Pi in Java
Encrypt / decrypt with AES256 in PHP and Java
FizzBuzz in Java
Organize your own differences in writing comfort between Java lambda expressions and Kotlin lambda expressions.
[Ruby] Difference between receiver and object. Differences between Ruby objects and JS objects
Differences between Ruby syntax error statements in Ruby and binary
What impressed me as a beginner in writing Ruby
Differences in code when using the length system in Java
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
About eval in Ruby
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Ruby Learning # 27 Writing Files
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
From Java to Ruby !!
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Output triangle in Ruby
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Variable type in ruby
Use PreparedStatement in Java
Fast popcount in Ruby
What's new in Java 9,10,11