What you write in C # is written in Ruby like this

Note

Only describe the writing styles that you often use personally. It is still incomplete. I will update it when I have free time.

Standard stream

Standard output

C#


Console.Write("No line breaks");
Console.WriteLine("With line breaks");

Ruby


print "No line breaks"
puts  "With line breaks"

Standard input

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.

Loop processing

for statement

C#


for (var i = 0; i < 10; i++)
{
    Console.WriteLine(i);
}

Ruby


for i in 0..9
    puts i
end

foreach statement

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

While statement

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!

Conditional branch

if statement

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 ...

Switch statement

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.

Ternary operator

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

What you write in C # is written in Ruby like this
[Technical memo] What is "include" in Ruby?
[Ruby] What if you put each statement in each statement? ??
[Ruby] What is true?
[Ruby / namespace] Deepen ":: ← this" that you often see in Ruby
What is a Ruby module?
Write class inheritance in Ruby
What is this? Discover how anyone can write a program
[Ruby] What is `!!` used for?
[Ruby] What is an instance?
fields_for is used like this
[Ruby comprehension check] Can you explain what is happening? && and and
I wrote a C parser (like) using PEG in Ruby
How to write when you want to handle "array of C language strings" like argv [] in Ruby-FFI
[For beginners] You will definitely understand in 10 minutes! What is JavaBeans?
I tried to write code like a type declaration in Ruby
Write Ruby methods using C (Part 1)
[Ruby on Rails] What is Bcrypt?
Write keys and values in Ruby
What is a snippet in programming?
What is @Autowired in Spring boot?
Is there no type in Ruby?
This problem is soberly difficult ... (Ruby)
What is a Ruby 2D array?
How do you write in Scala that was written in Java? (List → Map)
What to do if the prefix c is not bound in JSP
What is a class in Java language (3 /?)
Spring Autowired is written in the constructor
What is Swift? Findings obtained in 3 weeks
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
What is the main method in Java?
Write Ruby methods using C (Numo :: NArray)
What is gem'foge', require: false in Gemfile?
Write Ruby methods using C ++ (Part 2) Benchmark
[Ruby] What is next if? Skip processing in case of specific conditions in loop processing
Androd: What to do about "The Realm is already in a write transaction in"