Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save clockworkpc/0f907af5bb67efa5c9e55dc6e3688b81 to your computer and use it in GitHub Desktop.
Save clockworkpc/0f907af5bb67efa5c9e55dc6e3688b81 to your computer and use it in GitHub Desktop.
Things I Learned from 'Learn To Code with Ruby' by Boris Paskhaver
  1. I have learned a lot since I took up programming. Working through the course consolidated my knowledge and helped me to grasp some things I understand loosely. Accordingly, the rest of this list is a mix of reminders and revelations.

#2. #print does not include a carriage return.

#3. Parallel variable assignment.

a = 10 b = 20 c = 30

a, b, c = 10, 20, 30

#4. Ruby used to have Fixnum and Bignum classes.

#5. Boolean methods are called predicate methods.

#6. #upto and #downto

5.downto(1) #=> 5,4,3,2,1 1.upto(5) #=> 1,2,3,4,5

#7. #step

1.step(20, 5) { |n| puts n } #=> 1, 6, 11, 16

#8. Multiline strings words = <<HEREDOC multi- line string HEREDOC

#9. Single quotes do not recognise escape characters or string interpolation p 'hello\n #{world}' #=> hello\n #{world}

#10. Capital letters have a smaller numeric value in Ruby "A" < "a" #=> true

#11. #concat, #prepend, and << in string concatenation are destructive a = "hello" a.concat(" world") a #=> "hello world" a << " today" a #=> "hello world today" "again".prepend("hello ") a #=> "hello hello world today"

#12. case methods "hello world".capitalize "hello world".upcase "HELLO WORLD".downcase "HeLlO wOrLd".swapcase

#13. A non-existent string is nil, an empty string is not. "Donald"[100, 4] #=> nil "".empty? #=> true "".nil? #=> false

#14. 'false' and 'nil' are falsey, everything else is truthy.

#15. a :symbol is essentially a light-weight string, instantiated without most of the String methods, which means it takes up less memory. Although in Ruby 2.5 there doesn't seem to be much difference between them.

"my_string".class.methods - :my_symbol.class.methods #=> [:try_convert, :new]

#16. The ternary operator returns its findings. Still not confident using it..

p 1 == 1 ? true : false #=> true

#17. Put optional paramameters at the end of a method definition: they can't be skipeed try_convert

def make_phone_call(number, area_code=03) "#{area_code}-#{number}" end

#18. Case method: multiple options can be grouped on a single line

def rate_my_food(food) case food when "steak" "Delicious" when "Tacos", "Burritos", "Quesadillas" "Still pretty delicious" end end

#19. Case method: condition and return value can be returned on a single line

def calculate_school_grade(grade) case grade when 90..100 then "A" when 80..89 then "B" when 70..79 then "C" else "F" end end

#20. Double negation

!true #=> false !!true #=> true

#21. How to use 'unless' properly

password = 'dominoes'

unless password.include?('a') puts "It does not include the letter 'a'" end

#22. How to use 'until' properly

i = 0

until i > 9 puts i i += 1 end

#23. Inline modifiers

n = 5000 puts "Huge number" if n > 2500 puts "Huge number" unless n < 2500

#24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment