A summary of personal learning! I learned the basic grammar of 3 languages, so I summarized it!
Ruby
puts "Hello, world"
Python
print("Hello, world")
PHP
echo "Hello, world";
Ruby
variable = "variable"
Python
variable = "variable"
PHP
$variable = "variable";
Ruby
# attr_accessor :Define getters by name etc.
puts person.name
Python
# def __init__Define getters with etc.
print(person.name)
PHP
echo $person->name();
Ruby
if number % 15 == 0
puts "FizzBuzz"
elsif number % 3 == 0
puts "Fizz"
elsif number % 5 == 0
puts "Buzz"
else
puts number
end
Python
if number % 15 == 0:
print("FizzBuzz")
elif number % 3 == 0:
print("Fizz")
elif number % 5 == 0:
print("Buzz")
else:
print(number)
PHP
if (number % 15 == 0){
echo "FizzBuzz";
} elseif (number % 3 == 0){
echo "Fizz";
} elseif (number % 5 == 0){
echo "Buzz";
} else {
echo $number;
}
Ruby
#Array
array = ["Red", "Blue", "yellow"]
#hash
hash = {"Osaka": "Osaka", "Aichi prefecture": "Nagoya", "Tokyo": "Tokyo?"}
Python
#list
lists = ["Red", "Blue", "yellow"]
#dictionary
dict = {"Osaka": "Osaka", "Aichi prefecture": "Nagoya", "Tokyo": "Tokyo?"}
PHP
//Array
$array = ["Red", "Blue", "yellow"];
//Associative array
$associative_array = [
"Osaka" => "Osaka",
"Aichi prefecture" => "Nagoya",
"Tokyo" => "Tokyo?"
];
Ruby
i = 1
while i <= 100 do
#processing
i += 1
end
Python
i = 1
while i <= 100:
#processing
i += 1
for i in range(1, 101):
#processing
PHP
for ($i = 1;$i <= 100;$i++){
//processing
}
Ruby
array.each do |value|
#processing
end
hash.each {|key, value|
#processing
}
Python
for value in lists:
#processing
for key in dict:
#processing
PHP
foreach($array as $value){
//processing
}
foreach($associativeArray as $key => $value){
//processing
}
Ruby
def hello
#processing
end
Python
def hello():
#processing
return #Return value
PHP
function hello() {
//processing
return //Return value
}
Ruby
class Person
@@number = 0
def self.classmethod
#processing
end
def initialize(name)
@name = name
end
person = Person.new("Yamada")
end
Python
class Person:
number = 0
@classmethod
def classmethod(cls):
#processing
def __init__(self, name):
self.name = name
end
person = Person("Yamada")
end
PHP
class Person {
private static $number;
private $name;
public function static function classmethod(){
echo self::$number;
}
public function __construct($name){
$this->name = $name;
}
$person = new Person();
}
Ruby
def setName(name)
@name = name
end
def getName
@name
end
#Or
attr_accessor :name
Python
def __init__(self, name):
self._name = name
@property
def name(self):
return self.__name
@name.setter
def name(self, name):
self.__name = name
PHP
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
It feels like there is a personality in how (end / indent / {}) one block is specified! Thank you for reading! : relaxed:
Recommended Posts