Skip to content

Instantly share code, notes, and snippets.

@coolbrg
Last active November 23, 2019 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coolbrg/ae3873aa2a9b0af4029cab2c893ff091 to your computer and use it in GitHub Desktop.
Save coolbrg/ae3873aa2a9b0af4029cab2c893ff091 to your computer and use it in GitHub Desktop.
Python vs Ruby Differences

NOTE: P -> Python, R -> Ruby

1. Float Division

P: >>> 13/4 # 3.25

R: > 13/4 => 3  

2. Non-keywords assignment behavior

P:  >>> print = "hello"
    >>> print
    'hello'
    >>> print("hello world")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'str' object is not callable
    
R:  >> print = "hello"
    => "hello"
    >> print
    => "hello"
    >> print("hello world")
    hello world=> nil

3. Accessing non-existing key-value in Hash

P:  >>> hash["c"]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: 'c'
R:  >> hash[:c]
    => nil

4. Instance variable behavior

P:
>>> class Website:
      pass
      
>>> github = Website()
>>> github.url = "https://github.com"
>>> github.url
'https://github.com'

R:
>> class Website
>> end
=> nil

>> github = Website.new
=> #<Website:0x00007f912d88f0c0>

>> github.url = "https://github.com"
Traceback (most recent call last):
...
NoMethodError (undefined method `url=' for #<Website:0x00007f912d88f0c0>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment