Only describe the writing styles that you often use personally. It is still incomplete. I will update it when I have free time.
C#
Console.Write("No line breaks");
Console.WriteLine("With line breaks");
Ruby
print "No line breaks"
puts "With line breaks"
C#
//If there is only one value
var single = Console.ReadLine();
//If there are multiple
var multiple = Console.ReadLine().Split(",");
Ruby
#If there is only one value
single = gets
#If there are multiple
multiple = gets.split(",")
If it is just separated by a space, it seems that you can write it like this.
multiple = gets.split
Ruby is amazing.
C#
for (var i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
Ruby
for i in 0..9
puts i
end
C#
var array = new int[5] {1, 2, 3, 4, 5};
foreach (var i in array)
{
Console.WriteLine(i);
}
Ruby
array = (1..5)
array.each do |i|
puts i
end
C#
var num = 0;
while (num < 5)
{
Console.WriteLine(num);
num++;
}
Ruby
num = 0
while num < 5
puts num
num += 1
end
It looks like Ruby doesn't have increments and decrements. Instead? Apart from adding 1, it seems that there are succ and next.
succ
Ruby
num = 1
apt = "a"
letter = "z"
puts num.succ #output: 2
puts apt.succ #output: b
puts letter.succ #output: aa
This is convenient. It's also interesting that aa is next to z (I don't know where to use it ...) ~~ However, there seems to be no corresponding decrement. ~~ There seems to be something close ...
pred
Ruby
num = 1
puts num.pred #output: 0
However, when I did it in the alphabet, I got an error. Well, there shouldn't be such a problem!
C#
var name = "Takeshi";
if (name == "Takeshi")
{
Console.WriteLine("Gata Gata Gata Gata Gata Gata");
}
else if (name == "Takuro")
{
Console.WriteLine("Unfortunately handsome");
}
else
{
Console.WriteLine("Mika");
}
Ruby
name = "Takeshi"
if name == "Takeshi"
puts "Gata Gata Gata Gata Gata Gata"
elsif name == "Takuro"
puts "Unfortunately handsome"
else
puts "Mika"
end
Personally, Ruby may be a little hard to see ...
C#
var energyDrink = "Monster";
switch (energyDrink)
{
case "Monster":
Console.WriteLine("Has a lot of taste");
break;
case "Zone":
Console.WriteLine("Cheap and big");
break;
default:
Console.WriteLine("Red Bull seems to cut the price");
break;
}
Ruby
energyDrink = "Monster"
case energyDrink
when "Monster"
puts "Has a lot of taste"
when "Zone"
puts "Cheap and big"
else
puts "Red Bull seems to cut the price"
end
I'm grateful that it was troublesome to write breaks one by one.
C#
var num = 10;
var results = (num == 10) ? "true" : "false";
Ruby
num = 10;
results = (num == 10) ? "true" : "false";
This guy didn't turn into a stone.
Recommended Posts