DEV Community

Vicki Langer
Vicki Langer

Posted on • Edited on

*, ?, and yield in Ruby, as a Python Dev

This is a continuation of Picking up Ruby Fast, as a Python Dev. Again, expect examples with minimal math


It's been a bit since I wrote my last post about learning Ruby. I didn't stop learning, but I had been reviewing what I had already written about and learning things that didn't seem worth writing about.

Yesterday and today, I stumbled across several things that are totally new to me. So, here are some explanations and non-math examples of Ruby things that I hadn't seen before.


Just Some New-to-Me things

A bunch of examples and explanations for future reference.

Blocks

Blocks are more than just code in {}. Code between do and end are also called blocks. I knew this, but not recalling the do/end blocks when trying to learn about yield. For real, it was blocking my understanding of it.

yield

So, understanding what yield meant took me what felt like forever. Apparently, yield is just passing or sending info to a block.

def greet_pets(list)
    i = 0
    while i < list.length
        yield list[i] # send element 
        i = i + 1
    end
end

pets = ["Cheeto", "Remmy", "Wiley", "Puppy", "Chip"]

greet_pets(pets) do |name|
    if name.end_with?("y")
        puts "Hi, #{name}, how was your day?"
    end
end

# output
Hi, Remmy, how was your day?
Hi, Wiley, how was your day?
Hi, Puppy, how was your day?
=> nil
Enter fullscreen mode Exit fullscreen mode

Splat *

This is new to me. I'm pretty sure Python has this, but it's still new to me and belongs on this list.

Using a splat * in the parameters allows to given an unspecified amount of things. That could be zero things or it could be 1000 things.

def print_pets *pets
    puts pets.join(', ')
end

print_pets("Cheeto", "Wiley", "Puppy", "Remmy", "Chip")

# output
Cheeto, Wiley, Puppy, Remmy, Chip
Enter fullscreen mode Exit fullscreen mode

NoMethodError

The terminal is giving such fantastic amazing errors that are just so helpful. I'm in awe. I had if name.ends_with?("y") in my code when it gave this error:

NoMethodError (undefined method `ends_with?' for "Cheeto":String)
Did you mean?  end_with?

Enter fullscreen mode Exit fullscreen mode

return

This one is a lie. return isn't new to me. It's just something I've always had a heck of a time understanding.

return seems to be used for saving things into variables. This can't be done with puts because it just shows stuff on the screen instead of saving it for later use.


Built-In ? Methods

I am amazed every single time I see one of these methods that has a ? in it. They always throw me off at fir then amaze me with how simple they make things.

They make the code feel so much like spoken English that it feels natural. I'm a fan.

block_given?

def make_bio(name, language, *pets)
    puts "Hi, I'm #{name} and I do #{language} programming"
    if block_given?
        yield pets
    else
        puts pets
    end
end

make_bio("Vicki", "Python & Ruby", "Cheeto", "Chip"){|pets| puts pets.join(' & ') + " btw, yield was used"}

# output
Hi, I'm Vicki and I do Python & Ruby programming
Cheeto & Chip btw, yield was used
=> nil
Enter fullscreen mode Exit fullscreen mode

If you missed the first post in this series, I've heard it's a good read and there's cats.

Top comments (0)