Picking up Ruby Fast, as a Python Dev pt II
Tags: ruby, python, codenewbie
Published: 2020-11-17 20:14:23

*This is a continuation of [Picking up Ruby Fast, as a Python Dev](https://dev.to/vickilanger/python-to-ruby-1hc2). Again, expect examples with no math and no crappy names*

## Some Reflection

I really never thought I'd pick up a second language, but now that I am it's a lot easier than expected. Now, don't take that like I said it's easy. It's just not quite as hard and confusing as it was when I had no clue how to code.

{% twitter 1328545765132791809 %}

##More differences

### Classes, Functions, and Methods, oh my

Turns out, Python isn't a true OOP language as it still has functions. Ruby, on the other hand, is a true OOP language and does not call functions. Instead, messages are sent to methods of an instance of an object

> we don't call functions, we send messages to methods on an instance of a given object in Ruby!

![Screenshot of discord: Message from me "but but, why does python call them functions. Ugh, never mind. Got it. Methods, but basically the same thing, right?" Response to me "
@Vicki (vicki_langer) Because python is kinda hybrid if that makes sense? It has the concept of both methods and functions, methods belong to a class... in Ruby literally everything is an object... `pry` and you can do `Object::Object::Object::Object::Object` because `BasicObject` and `Object` are inherited by literally everything including keywords like true, and false Ruby does not have the concept of a classes function, everything is an instance of something else"](https://dev-to-uploads.s3.amazonaws.com/i/dd5s8qa0wrin8wdkq82a.png)

### Iterating

Both Python and Ruby have loops, but the syntax is different. Here's a comparison of the same code in each language

In Python
```python
# python
# throw each toy some number of times for some dog
def throw_toy(amount, name = "the dog")
    toy_list = ["ball", "frisbee", "rock", "another ball", "cat"]
    for toy in toy_list:
        print(f"threw {toy} {amount} times for {name.capitalize()}"

throw_toy(3, "Cheeto")
throw_toy(1, "wiley")
```

Here it is in Ruby
```ruby
#ruby
# throw each toy some number of times for some dog
def throw_toy(amount, name = "the dog")
  toy_list = ["ball", "frisbee", "rock", "another ball", "cat"]
  toy_list.each{
     |toy| puts "threw #{toy} #{amount} times for #{name.capitalize}"
    }
end

throw_toy 3, "Cheeto"
throw_toy 1, "wiley"
```


### Parentheses in Ruby

Turns out, parentheses are optional but preferred in some cases.

If you only have one arg, parentheses probably aren't needed. More than one arg and it's a good idea to have them.

```ruby
# this is acceptable
def throw_toy amount, name = "the dog"

# this is preferred because there is >1 argument
def throw_toy(amount, name = "the dog")
```


## A Few More Quick Points of Difference

| Ruby        | Python           |
| --------- |-------------|
|  `BasicObject` & `Object` are inherited by literally everything |  has concept of both methods & functions where methods belong to a class |
| methods | functions |
| `494595.to_s` | `string(494595)` |

---
If you missed the first post, I've heard it's a good read and there's cats.
{% post https://dev.to/vickilanger/python-to-ruby-1hc2 %}