DEV Community

Vicki Langer
Vicki Langer

Posted on

More Ruby from a Python Dev

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


puts and gets Methods

The puts method in Ruby is similar to the print() function in Python. puts sends output to the $stdout variable which is then shown in the console.

We can do the opposite and get a value from input in the console that will be stored in $stdin until you use the gets method.

#ruby
puts 'What should we call you?'
name = gets.strip
puts "Welcome, #{name}! ๐Ÿ‘‹  Nice to meet you."
puts "So #{name}, I see you're not familiar with Ruby."
puts "What makes you think we should hire you?"
why_qualified = gets.strip
puts "Okay, I see you #{why_qualified}."
puts "I've also heard you spent the last week picking up Ruby. How's that going?"
feels_about_ruby = gets.strip
puts "It sounds like you could pick Ruby up rather quickly"
puts "I imagine you'd do well working with our senior TSE"
puts "Anything else?"
confident_ending = gets.strip
puts "#{confident_ending}, too. We'll be in touch soon"

# output
"""
What should we call you?
Vicki
Welcome, Vicki! ๐Ÿ‘‹  Nice to meet you.
So Vicki, I see you're not familiar with Ruby.
What makes you think we should hire you?
teach kids Python & JS and have experience building & maintaining oss Python projects
Okay, I see you teach kids Python & JS and have experience building & maintaining oss Python projects.
I've also heard you spent the last week picking up Ruby. How's that going?
The Ruby syntax was a little hard at first, but I'm starting to like it. It's quite similar to Python, but seems more intuitive
It sounds like you could pick Ruby up rather quickly
I imagine you'd do well working with our senior TSE
Anything else?
 I look forward to working with you
I look forward to working with you, too. We'll be in touch soon
"""
Enter fullscreen mode Exit fullscreen mode

Worth noting, chomp and strip are often used with gets. strip removes leading and trailing white space as well as new lines. chomp only removes new lines at the end of a string.

A String v Raw String Error

When running the code above, I got an error that confused me for a moment. I noticed I had used '' instead of "". This would be fine in Python, but '' in Ruby make it a raw string. So, it wasn't pulling in the user input name.

Even or Odd

Is it even or odd? This algorithm is a commonly asked one that isn't as easy as it could be in Python. Yet again, Ruby has a simple answer for this.

# ruby
number = 2753

number.even?
Enter fullscreen mode Exit fullscreen mode
# python
number = 2753

if number % 2 == 0:
    print("is even")  # remainder == 0
else:
    print("is odd")  # remainder not 0
Enter fullscreen mode Exit fullscreen mode

What am I using to learn?

In order, so far, I have used the below sites


Reminder to self: do not work at places that think unpaid work is acceptable.


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

Top comments (7)

Collapse
 
yechielk profile image
Yechiel Kalmenson

Awesome post in an awesome series!

One point in the section on even numbers; you don't need to call .to_i if it's already a number.

I'm guessing you copy/pasted it from a sample where there number was received as user input using gets (in which case the input would come in as a string and would need to be converted to an integer explicitly), but in your example, you set the value of number directly as an integer, so calling to_i on it is unnecessary.

Collapse
 
vickilanger profile image
Vicki Langer

Oh dang! Youโ€™re right, when I took it out of my example, it didnโ€™t need that anymore. I definitely had this after a gets

Guess I shouldโ€™ve included the whole thing or at least test that line by itself. Thanks for bringing that up. Iโ€™d hate to reference back to this and not understand why it doesnโ€™t work right.

Collapse
 
yechielk profile image
Yechiel Kalmenson

It still works with the to_i, it's just unnecessary.

All it does is convert an integer to an integer, no harm done ๐Ÿ™ƒ

Thread Thread
 
vickilanger profile image
Vicki Langer

Fixed it up anyway. It goes to show my point of how succinct Ruby is in this case.

And my posts contain no copying and pasting from samples. Iโ€™d never learn anything that way. For this, I always make up my own stuff and use that.

Collapse
 
miriamtocino profile image
Miriam Tocino

Hey Vicki, I'm enjoying your series and saving them for later when I'm back at learning Ruby.

I also loved this part "Reminder to self: do not work at places that think unpaid work is acceptable." ๐Ÿ’™

What are you learning next?

Collapse
 
vickilanger profile image
Vicki Langer

Yay! I hope it helps you. Really, this will just be a great reference for me to look back at. I'm always excited when it helps more than just me.

Today, I'm working on learning how to .map hashes. Python has an equivalent, but I haven't used it.

Collapse
 
miriamtocino profile image
Miriam Tocino

It's nice to be making things for your future self! Keep them coming, Vicki. ๐ŸŒŸ