Becoming A Rubyist Notes Day 1 Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

How do you do implicit returns?

A

By making what you want to return the last executed expression

def get_avg(num_1, num_2)
    return (num_1 + num_2) / 2
end

Preferred by a Rubyist
def get_avg(num_1, num_2)
(num_1 + num_2) / 2
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

should you omit parentheses for method calls with no arguments?

A

Yes

def say_hi
puts “hi”
end

# Less preferred 
say_hi()
# Preferred by a Rubyist
say_hi
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

should you ever use single line conditionals?

A
when possible 
raining = true
#less preferred
if raining
    puts "don't forget an umbrella!"
end
# Preferred by a Rubyist
puts "don't forget an umbrella!" if raining
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how do you Use built-in methods?

A

people = [“Joey”, “Bex”, “Andrew”]

# Less preferred
p people[people.length - 1]

Preferred by a Rubyist
p people[-1]
p people.last

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how do you use enumerables to iterate?

A
# Less preferred
def repeat_hi(num)
    i = 0
    while i < num
        puts "hi"
        i += 1
    end
end
# Preferred by a Rubyist
def repeat_hi(num)
    num.times { puts "hi" }
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Are some methods better than enumerables?

A
#Enumerable is less preferred
def all_numbers_even?(nums)
    nums.each do |num|
        return false if num % 2 != 0
    end
true end
# Preferred by a Rubyist
def all_numbers_even?(nums)
    nums.all? { |num| num.even? }
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly