CodeNewbie Community

Vicki (she/they)
Vicki (she/they)

Posted on • Originally published at dev.to

Picking up Ruby Fast, as a Python Dev pt II

This is a continuation of Picking up Ruby Fast, as a Python Dev. 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.

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 "<br>
@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...  raw `pry` endraw  and you can do  raw `Object::Object::Object::Object::Object` endraw  because  raw `BasicObject` endraw  and  raw `Object` endraw  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"

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
# 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")
Enter fullscreen mode Exit fullscreen mode

Here it is in 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"
Enter fullscreen mode Exit fullscreen mode

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.

# 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")
Enter fullscreen mode Exit fullscreen mode

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. Picking up Ruby Fast, as a Python Dev

Discussion (0)