I just started to pick up
Ruby today (to install:
sudo apt-get install ruby irb ri rubygems). Here are some things I learned along the way, coming from a Python mindset. The (very good and highly recommendable) references I looked at were (in order):
- Ruby in Twenty Minutes
- To Ruby From Python
- Ruby Essentials (free, on-line book)
- Important Language Features and Some Gotchas
Similarities- Brackets for arrays, braces for dictionaries (called hashes in Ruby)
- Strong dynamic typing
- Everything is an object, variables are just references
- Exceptions are similar
- No special line termination characters
- # for single line comments
- def to define a method, class to define a class
Differences- Interactive prompt: python => irb
- Interactive prompt help: help(str.count) => help "String#count"
- Interactive prompt reload: reload(foo) => load "foo.rb"
- Command line docs: pydoc => ri
- File extension: .py => .rb
- Shebang line: #!/usr/bin/python => #!/usr/bin/env ruby
- Indentation and blocks: : and tabs => either { } or end
- Strings: immutable => mutable (can use freeze method for immutability)
- Naming conventions: unenforced => enforced (ex. class names start with a capital letter, variables start with a lowercase letter)
- Raw strings: r"blah" => 'blah'
- Parantheses: mandatory => sometimes optional
- Booleans: True, False => true, false
- Null value: None => nil
- Else-if statement: elif => elsif
- Module import: import foo => require "foo"
- Boolean conversion: 0, False, None and anything empty => Only nil and false
- Doc generation: docstrings below things => regular comments above things
- Output: print => puts (or print if you don't want a trailing newline)
- Command line execution: python -c => ruby -e
- Global Variables
- Ranges: range(x,y) => x...y exclusive, x..y inclusive (.to_a to return array)
- Slicing: arr[1:3] => arr[1...3] or arr[1..2] (inclusive)
- Object initialization: __init__ => initialize
- Object creation: a() => a.new
- Ternary operator: if_true if statement else if_false => statement ? if_true : if_false
- Substring matching: 'foo' in str => str['foo']
- "Main": if __name__ == '__main__' => if __FILE__ == $0 ... end
Philosophical differences- Attribute access: direct access => method calls
- Need getter and setter methods to access attribute outside class
- Access permissions: convention by underscore => public, protected, private
- Multiple inheritance: allowed => mixins
Stuff Ruby has that Python doesn't- Double quoted strings allow expression substitution #{} and escape sequences \t\n
- You can re-open a class at any time and add more methods
- Block comments: ==begin and ==end
- Special variable characters (begins with...):
- $ global
- @ instance
- [a-z_] local
- [A-Z] constant (triggers warnings when reassigned)
- @@ class
- Arrays support some set operations
- difference -
- intersection &
- union |
- Method conventions (ends with...)
- ! changes internal state of object
- ? returns boolean
- do keyword -- optional loop keyword (unless on single line)
- Crazy for loop alternatives
- General delimited strings
- Case statement
- Lots of options for string element access and string substitution and insertion
- Regular expressions as first-order objects
- Perl-like control flow constructs that can be tacked onto the end of an expression
No comments:
Post a Comment