This is a continuation of Picking up Ruby Fast, as a Python Dev. Again, expect examples with no math and no crappy names
Variable Assignment in an if
statement
In Ruby, you can assign variables in an if
statement. This can't be done in Python. Not sure why I'd ever want to do this, but now I know that I can. Seems like it would be a bad idea.
#ruby
if cat_age = 12
puts "The cat is #{cat_age}"
else
puts "The cat is not #{cat_age}"
end
Decisions with if
Making decisions with if
in Python is pretty much always the same. It seems there are several options in Ruby.
#python
cat_age = 12
dog_age = 5
if cat_age > dog_age:
print("Cat is older than dog")
else:
print("Dog is older than Cat")
#ruby
cat_age = 12
dog_age = 5
# option 1
if cat_age > dog_age
puts "Cat is older than dog"
elsif
puts "Dog is older than cat"
end
# option 2
puts "Cat is older than dog" if cat_age > dog_age
puts "Dog is older than cat" if dog_age > cat_age
Checking for Empty String
There are plenty of different ways to do the same thing. Yet, Ruby's empty?
method is really intriguing me more than it should. It just makes so much sense. In Python, you'd typically just compare things with the not
operator.
#python
username = "Vicki"
if not username.strip():
print('empty String: please enter a username')
else:
print("not empty: success")
#ruby
username = "Vicki"
if username.to_s.empty?
puts "empty String: please enter a username"
elsif
puts "not empty: success"
A Few More Quick Points of Difference
Ruby | Python |
---|---|
&& |
and |
end |
not 4 spaces or tabbed in |
Can use some characters in nameing. like meal_time?
|
Only underscores like is_it_meal_time
|
Interview Questions to Ask the interviewer(s)
Not sure if you caught this, but I'm learning Ruby for the sole purpose of a potential job. So, I'm working on some questions to ask the interviewer(s). Here's kinda what I plan to ask:
- Did I answer all of your questions?
- Ruby is new to me and I've heard it's expressive. How does that help the job?
- Did anything change at the start of the pandemic?
- Is there anything I've said that makes you doubt I would be a great fit for this position?
- Do you have any unpaid interns or fellows?
And, I was gonna ask something like:
- How would you score the company on current DEI initiatives
but I got a better suggestion from @robocel
If you have any suggestions on what to ask, I'd love to hear them. I'm super excited about this potential job and hoping they'll know I am very very willing and capable of learning.
If you missed the first post in this series, I've heard it's a good read and there's cats.
Top comments (3)
Here's a demonstration of why assigning a variable in an
if
statement could be dangerous:The robot apocolypse
Yechiel Kalmenson ・ Jul 3 '19 ・ 1 min read
I had a feeling that it could be a bad idea. It just didn't seem right. I hadn't thought about it leading the end of our species lmao
Neat. I’ve never seen that before.