0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

プログラミング言語食べ比べ:PythonとRuby編

Posted at

はじめに

この記事は同じ動作をPythonとRubyで書いたらどうなるのかという比較をするための記事です。

あくまで備忘録として書いてるので拙いかもしれませんが、ご了承ください。

対話コンソール

Pythonではpythonコマンド、Rubyではirbを使う。

Python
$ python
Python 3.8.5 ...
Type "help", "copyright", "credits" or "license" for more information.
>>> 
Ruby
$ irb
irb(main):001:0> 

対話コンソールから抜ける場合、
Pythonであればexit()Ctrl + Z(Windowsの場合)、Ctrl + D(Linuxの場合)、
RubyであればexitCtrl + D
で対話コンソールから出ることができる

Hello World!を書く

Pythonはprint、Rubyはputs

Python
print("Hello World!")
Ruby
puts "Hello World!"

分岐

両者とも書き方に大差はないが、
Pythonはif内はインデントを設ける必要があり、Rubyは定義終了時にendを付けなければいけない
else ifを設ける場合、Pythonはelif [式]:、Rubyはelsif [式]なので注意

Python
a = 1

if a == 1:
    print("aは1")
elif a == 2:
    print("aは2")
else:
    print("aは1でも2でもない")
Ruby
a = 1

if a == 1
  puts "aは1"
elsif a == 2
  puts "aは2"
else
  puts "aは1でも2でもない"
end

繰り返し(反復)

配列等による繰り返しであれば、Pythonはfor、Rubyであればeachを使う

Python
for a in range(10):
    print(a)
Ruby
(0..9).each do |a|
  puts a
end

関数定義

引数なしの場合

Pythonだとこんな感じ

Python
def func_def():
    print("It's function")
    
if __name__ == "__main__":
    func_def()

Pythonの場合は引数がなくても()を付ける必要がある(ある場合とない場合では動作が違う)

Rubyだとこう

Ruby
def func_def
  puts "It's function"
end

if __FILE__ == $0
  func_def # func_def() と同様
end

Rubyの場合、引数がない場合は()をつける必要がない(付けても動作する)

引数ありの場合

これもさほど変わらない。

Python
def arg_func(one, two=2):
    print(f"one: {one}, two: {two}")

if __name__ == "__main__":
    arg_func(1)
Ruby
def arg_func (one, two=2)
  puts "one: #{one}, two: #{two}"
end

if __FILE__ == $0
  arg_func(1)
end

余談だが、

Pythonの場合はif __name__ == "__main__":
Rubyの場合はif __FILE__ == $0でメインファイルとして取り扱われるかを判別できる

また、
Pythonはf"{variables}"
Rubyは"#{variables}"で各変数を文字列に代入できる

クラス

PythonもRubyにもクラスの概念がある
ただ、両者を比べると使い勝手が少し違う

定義

クラスのコンストラクターは、Pythonは__init__、Rubyはinitializeで定義する

Python
class Greeter:
    def __init__(self, name="World"):
        self.name = name

    def hello(self):
        print(f"Hello {self.name}!")

    def evening(self):
        print(f"Good evening {self.name}!")
Ruby
class Greeter
  def initialize(name = "World")
    @name = name
  end

  def hello
    puts "Hello #{@name}!"
  end

  def evening
    puts "Good evening #{@name}!"
  end
end

Rubyの変数はattr_accessor :[インスタンス変数]をすると、読むことができるようになる
とにかく、これを設定しないとインスタンス変数はプライベート扱いになると言っても過言ではない
なお、Pythonは変数の最初に__(アンダーバー2つ)を付け足すとプライベート扱いになる(関数も同様)

また、Pythonだと同じクラスを設定すると置き換わってしまうが、
Rubyの場合は同じクラスをもう一度書いても、付け足すことができる
(しかも既に作られているインスタンスにも適用される)

Ruby
# -- snip -- (上記のGreeterクラスが既に定義されているとする)
class Greeter
  attr_accessor :name
end

インスタンス作成

インスタンスの作成は、Pythonはそのままクラス名を、Rubyはクラス名に加えて.newを使う

Python
# -- snip --
greet = Greeter("Qiita")
Ruby
# -- snip --
greet = Greeter.new("Qiita")

最後に

  • PythonとRubyは似ているところは似ている
  • Pythonはインデントによる分岐等の処理記述
  • Rubyはendまでが処理記述なので区切りがわかる

Rubyのメモ書きはScrapbox(Yuzulia-ProgBuilder)にまとめているので、気が向いたら御覧ください。
https://scrapbox.io/yuzulia-pb/Ruby

気が向いたらまたこの記事を更新します。

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?