Fun Ruby tricks I learned in codewars

I have been playing around with codewars a lot lately. (www.codewars.com) I like working with code challenges they are pretty fun. One thing that is great about playing with codewars is that I got to use a lot of tools and methods I would have never el…


This content originally appeared on DEV Community and was authored by Ashley

I have been playing around with codewars a lot lately. (www.codewars.com) I like working with code challenges they are pretty fun. One thing that is great about playing with codewars is that I got to use a lot of tools and methods I would have never else thought of for example

delete

This comes in handy for challenges that ask you to remove a series of elements from a string.
For example, if you need to remove some vowels from a string

 my_string = "This is such a wonderful method"
 my_string.delete("aeiou") 

 => Ths s sch  wndrfl mthd

This is much easier than using gsub!

to_str(base)

So most everyone knows how to_s works, you are converting a non string object to a string representation of that object. But one thing a lot of people forget is that you can pass in an argument to to_s when its placed on a Fixnum. This argument represents a numerical base which by default is 10. Therefore, this method can be used to convert numbers to different bases

  35.to_s(2)

  => "100011"

partition

This is a great way to split up some data. It accepts a block and creates 2 arrays. For the elements that are true, they are first array, while the rest are placed in the second array. Imagine we have a problem where we need to seperate some evens and odds

  my_array = [1,2,3,4,5,6]
    my_array.partition{|i| i.even?}

    => [[2, 4, 6], [1, 3, 5]] 

even? odd?

Which reminds me, I have a tendency to forget about these ruby methods. They work just as you expect. The question mark signify's this method returns some sort of boolean.

  4.even?
  => true

    4.odd?
    => false

Its a shame how often I resort to modulo to check for evens and odds knowing this exists

chars

chars will split a string into an array of chars

 "This is my string".chars
 => ["T", "h", "i", "s", " ", "i", "s", " ", "m", "y", " ", "s", "t", "r", "i", "n", "g"] 

of course split('') would do the same, but I like this better

Code challenges are super fun, and its great to learn new tricks. I recomment trying codwars sometime or even hacker rank or leetcode. I think Im going to go do some SQL challenges now!


This content originally appeared on DEV Community and was authored by Ashley


Print Share Comment Cite Upload Translate Updates
APA

Ashley | Sciencx (2021-08-09T02:08:44+00:00) Fun Ruby tricks I learned in codewars. Retrieved from https://www.scien.cx/2021/08/09/fun-ruby-tricks-i-learned-in-codewars/

MLA
" » Fun Ruby tricks I learned in codewars." Ashley | Sciencx - Monday August 9, 2021, https://www.scien.cx/2021/08/09/fun-ruby-tricks-i-learned-in-codewars/
HARVARD
Ashley | Sciencx Monday August 9, 2021 » Fun Ruby tricks I learned in codewars., viewed ,<https://www.scien.cx/2021/08/09/fun-ruby-tricks-i-learned-in-codewars/>
VANCOUVER
Ashley | Sciencx - » Fun Ruby tricks I learned in codewars. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/09/fun-ruby-tricks-i-learned-in-codewars/
CHICAGO
" » Fun Ruby tricks I learned in codewars." Ashley | Sciencx - Accessed . https://www.scien.cx/2021/08/09/fun-ruby-tricks-i-learned-in-codewars/
IEEE
" » Fun Ruby tricks I learned in codewars." Ashley | Sciencx [Online]. Available: https://www.scien.cx/2021/08/09/fun-ruby-tricks-i-learned-in-codewars/. [Accessed: ]
rf:citation
» Fun Ruby tricks I learned in codewars | Ashley | Sciencx | https://www.scien.cx/2021/08/09/fun-ruby-tricks-i-learned-in-codewars/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.