More Ruby from a Python Dev
Tags: ruby, python
Published: 2020-11-20 19:50:59

*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
#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
# ruby
number = 2753

number.even?
```

```ruby
# 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.

{% twitter 1329127215993397252 %}

---
If you missed the first post in this series, I've heard it's a good read and there's cats.
{% post https://dev.to/vickilanger/python-to-ruby-1hc2 %}