Hello. My name is @ sho-hata and I started working from home as soon as I joined the company. While working from home has the nice point of not having commuting time, it is difficult to switch it on and off. By the way, I've only touched Python until now, but now that I've touched Ruby on Rails at work, I spend my days studying every day. This time, since it was the first week for me to start touching Ruby, I found ["From python to Ruby"](https: // www) in the "Introduction to Ruby from Multilingual" section of ** Ruby official document **. While reading .ruby-lang.org/ja/documentation/ruby-from-other-languages/to-ruby-from-python/), I'm often worried about Ruby and python ** Similarities / Differences ** I briefly investigated and summarized. Contents will be added as appropriate. In addition, all of the following are based on Ruby.
It's really convenient, isn't it? It feels like when you want to try out the code quickly, or when you want to show the true character of an interpreted language. Ruby interactive execution environment
Ruby
irb
irb(main):001:0>
Python interactive execution environment
Python
python
>>>
A little rich Python interactive execution environment
Python
ipython
In [1]:
The built-in list container is a variable length array. So, if you add elements, the number of elements will increase without permission!
Ruby
array1 = [1, 2, 3] # => [1, 2, 3]
array1[3] = 4 # => 4
array1
# => [1, 2, 3, 4]
In Python, string literals are immutable. Therefore, if they have the same value, they refer to the same address in memory.
Python
str1, str2 = "hoge", "hoge"
str1 is str2 # -> True
id(str1) # -> 140682191246064
id(str2) # -> 140682191246064
** * (4/18) postscript **
In the case of a character string, the same object is shared if it is the same string literal, but if a character string is created by an operation etc., it refers to a different memory address even if it has the same value.
Mr. @shiracamus pointed out. As shown below, when reassigning a value to a variable, it refers to a different memory address even if it has the same value. That's because a new object is created on reassignment and its reference is stored in the variable str2.
Python
str1 = "hoge"
str2 = "ho"
str2 += "ge"
str1 == str2 # -> True
str1 is str2 # -> False
id(str1) # -> 140239943766896
id(str2) # -> 140239943767152
When a variable is declared, if a string literal is concatenated by an operation to create a string, the same address in memory will be referenced if the values are the same.
Python
str1 = "hoge"
str2 = "ho" + "ge"
str1 is str2 # -> True
str1 == str2 # -> True
However, if an empty string is generated when a variable is declared and reassignment is performed, the object id of the variable after reassignment will be the same as the id of the variable with the same value. Language is difficult. .. ..
Python
str1 = "hoge"
str2 = ""
id(str1) # -> 140239943766896
id(str2) # -> 140240212059056
str2 += "hoge"
str1 is str2 # -> True
id(str2) # -> 140239943766896
However, in Ruby string literals are ** mutable **. Each points to a different memory address.
Ruby
str1, str2 = "hoge", "hoge"
p str1.equal?(str2) # => false
p str1.object_id # => 70220509475900
p str2.object_id # => 70220643515600
However, Ruby has ** symbols ** literals that are used as an alternative to string literals, which look like strings on the source and are treated internally as integers. Symbols are often used because they are immutable and consume less memory. ~~ Symbol I don't know anything ... ~~
In Ruby, identifiers that start with an uppercase alphabet are treated as constants. here, ** Constants in programming languages = Once initialized, their contents cannot be changed ** I think that is the recognition. ** But ** In Ruby it is possible to reassign to an initialized constant. (It seems that it is not recommended because it gives a warning) Therefore, no error occurs when reassigning.
Ruby
LANG = "Ruby" # => "Ruby"
LANG = "Python"
warning: already initialized constant LANG
warning: previous definition of LANG was here
# => Ruby
For me, a constant in Ruby = points to the same object id Is it the idea? It is the recognition. By the way, constants are ** not supported in Python **, but when dealing with variables (= fixed values) ** that are treated as constants, it is best to configure them with all uppercase letters and underscores (_) **. It is customary **.
Python
PREFIX = 'thank you for your hard work'
MAX_COUNT = 255
Reference: PEP 8 – Style Guide for Python Code | Python.org
Python has an object called a tuple whose elements are immutable. It is used when you want to create a lightweight list container with fixed elements, or when you want to pack and unpack multiple elements.
Python
tuple1 = (1, 2, 3)
tuple2 = 1, 2, 3
type(tuple1) # -> <class 'tuple'>
type(tuple2) # -> <class 'tuple'>
However, Ruby does not implement tuples as built-in objects. However, it seems that tuple-like things can be done depending on the writing style.
Ruby
rb_tuple = [1, 2, 3].map(&:freeze).freeze
rb_tuple[2] = 3
# => FrozenError (can't modify frozen Array)
(Reference: Ruby engineer studied Python to diss Python)
In Ruby, ** els if ** is written as a way to express ** else if **. In Python, it is written as ** elif **, but the pronunciation has changed as an abbreviation for ** else if **. Looking at what Mats, the creator of Ruby, said, it seems that the shortest number of characters and pronunciation are balanced. https://twitter.com/yukihiro_matz/status/1161556434728808448
Even with the same dynamic tidying language, Python / Ruby's unique parts were hidden in a few places. ** It is the real pleasure of studying a new language that you can think about "what kind of thought did the language designers create the language?" **! Great fun!
Recommended Posts