PythonIs a very good language, fromPythonMigrateRuby, You will find some Syntactic differences.
Similarity
AndPythonSimilarlyRubyMedium,...
- There is an interactive prompt(CalledIRB ).
- You can read the document in the command line.(PassRiCommand to replace
Pydoc ).
- No special end line symbol(Except for new lines).
- The text can contain multiple lines, just likePythonThree quotation marks in.
- ListUse[],DictUse{} (DictInRubyCalled"Hashes ").
- ArraysSame way of working(2ItemsArrayAdd to a longerArray,HoweverA3 = [a1, a2]Merge to generate an array containing arrays.).
- ObjectsThere are fixed types and dynamic Conversions.
- Everything isObject,The variable name only pointsObjectPointer.
- Different keywords,HoweverExceptionsDoes not change.
- You have embedded document tools
(InRubyCalledRdoc ).
Differences
AndPythonDifferent,InRubyMedium,...
- StringsYes.
- You can use Constants(The constant value varies.).
- Some mandatory writing requirements(For example:ClassThe name must start with an uppercase letter.,Variable name starts with a lowercase letter).
- There is only one container.(Array ),
And it can be changed.
- In quotation marksStringThe limits are different.
- There is no new styleClass,ClassThere is only one style.
- You cannot directly access the attribute.RubyIs implemented through method calls.
- Using () in a method call is an optional policy.
- Used herePrivateAnd so on.PythonIn.
- "Mixin's"Used to replace multiple inheritance.
- You can modify the existingClassAnd add a new method.
- UseTrueAndFalseTo replaceTrueAndFalse
(UseNilTo replaceNone).
- When determining the true value,OnlyFalseAndNilWill be considered false.All others think it is true(Including0, 0.0 ,"",And[]).
- UseElsifSubstitutionElif.
- UseRequireSubstitutionImport.However, the usage is the same.
- UseUsual-styleTo comment out the document(SubstitutionDocstrings)It is also used to generate documents.
Original article address:
Http://www.ruby-lang.org/zh_CN/documentation/ruby-from-other-languages/to-ruby-from-python/