DEV Community

Vicki Langer
Vicki Langer

Posted on

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.

Top comments (4)

Collapse
 
topofocus profile image
Hartmut B. • Edited

Just a reminder

def throw( amount , toys="toys", for: "the dog")
   name = for
  ["ball", "frisbee", "rock", "another ball", "cat"].each do  | toy |
        puts "threw #{toy} #{amount} times for #{name.capitalize}"
    end
end

# then these calls are valid
throw 4
throw 4, :toys 
throw 3, :toys, for:  'my dog'
throw 3, for: 'any dog'
Enter fullscreen mode Exit fullscreen mode

is more rubish

Collapse
 
vickilanger profile image
Vicki Langer

Interesting. Like I said in my post, Ruby is brand new to me. Thanks for the refactor.

In def throw( amount , toys="toys, for: "the dog"), there are only 3 quotes. I there a reason they aren't in pairs? I'm not sure what this part means right now

Collapse
 
topofocus profile image
Hartmut B. • Edited

Just edited my comment to correct the mistake.
I always try to design interfaces in a human stile. If you are able to express a method-call like a short sentence, no comments are needed.

Thread Thread
 
vickilanger profile image
Vicki Langer

Okay, I thought maybe it was a typo, but with Ruby being new to me I wasn't sure if it was just something new to me.

Thanks for helping me out and not throwing me some foo bar example