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
"""
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?
# python
number = 2753
if number % 2 == 0:
print("is even") # remainder == 0
else:
print("is odd") # remainder not 0
What am I using to learn?
In order, so far, I have used the below sites
- https://www.ruby-lang.org/en/documentation/quickstart/
- https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/to-ruby-from-python/
- https://try.ruby-lang.org/
- http://ruby-for-beginners.rubymonstas.org/
- https://ruby-doc.org/core-2.2.2/
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)
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 ofnumber
directly as an integer, so callingto_i
on it is unnecessary.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.
It still works with the
to_i
, it's just unnecessary.All it does is convert an integer to an integer, no harm done ๐
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.
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?
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.It's nice to be making things for your future self! Keep them coming, Vicki. ๐