An Interest In:
Web News this Week
- April 3, 2024
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
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.
#rubyputs 'What should we call you?'name = gets.stripputs "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.stripputs "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.stripputs "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.stripputs "#{confident_ending}, too. We'll be in touch soon"# output"""What should we call you?VickiWelcome, 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 projectsOkay, 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 intuitiveIt sounds like you could pick Ruby up rather quicklyI imagine you'd do well working with our senior TSEAnything else? I look forward to working with youI 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.
# rubynumber = 2753number.even?
# pythonnumber = 2753if number % 2 == 0: print("is even") # remainder == 0else: 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.
Original Link: https://dev.to/vickilanger/more-ruby-from-a-python-dev-2lc
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To