Khalid Benjelloun Profile picture
Feb 6 16 tweets 8 min read
The Ruby language is much intuitive, yesterday I've been doing a @codewars 7kyu-Kata in JavaScript, and I just did it again in Ruby, and I have decided to show everyone in here how I proceeded, starting to talk about this lovely programming language! a thread🧵

#coding
#ruby
I have not practised Ruby for a while, my rank for Ruby on @codewars is 5kyu, it's fine... the lowest level is 8kyu, then, the lower is that n-kyu number, the higher is the level, after level 1kyu, there are four mastery levels: 1dan, 2dan...etc.
I'll link to the Kata at the end of this thread, inside this box (see photo), you find a method definition, this method is called: high_and_low, it takes a string of space-separated numbers, the string is called: numbers, something like: "1 2 3 4 5" (including the quotes!) Caption from codewars.com
The method should return another string with only two numbers: the highest number in the string: numbers, and the lowest number, as shown in examples.
A method call: high_and_low("1 2 3 4 5") should return the string: "5 1" etc. That's the way we call a user-defined method. Caption from codewars.com
First I wrote this!, and I hit the test button.
In the next tweet, you could read the message error I have got.
You could see some built-in ruby methods (split, map, max...), they are applied on objects (like the string: numbers) and separated from the objects by a dot (.)...
They could have arguments or not.
Now let's read the error message, it said to me that the built-in ruby method "map" shouldn't have arguments, so I refactored the code for the first time, converting the argument (:to_i) into a block {:to_i}, let's see what it says now :)
Ok, it says now that the return value of a test should have been: "42 -9", while it is actually: "to_i to_i", let's fix that!
One second: If you are new to coding, don't feel overwhelmed! you will read more info about those three lines, and feel free to ask me about any bit! :)
Worked!
Let's have a long stop with those three lines with which I filled in my method definition, max and min are variable names given by me, while .max and .min are two ruby methods that return the highest number and the lowest number, respectively, from an array of numbers.
Worked!
Let's have a long stop with those three lines with which I filled in my method definition, max and min are variable names given by me, while .max and .min are two ruby methods that return the highest number and the lowest number, respectively, from an array of numbers.
An array is a Ruby Object like this: [1, 2, 3], it could contain any object, not only numbers.
To get the highest number, I split the string numbers by spaces.
So till now, I have got an array of our numbers, separated, each in string format.
The next method, map, will be applied to that array, this method gets every item inside an array and applies what's inside the block on it.
The block (ie. what's between "{}") gives the first Item of the array the name a, then it converts a from a string into an integer (number).
Now we have a new array with numbers rather than strings containing numbers.
Next, max method, will get that array and give us the highest number in it. min method in line 3 will give us the lowest number.
max and min variable names will save those two final results for us.
The third line is the last line in our method definition, it forms the value that our method will return, you could use the keyword "return" at the beginning of the line, rather, you could just omit it since it's the last line.
Method to_s will convert our max and min back to the string format, the + method, when applied between strings, joins them in one string.
That line joins our max and min, in string format, and a space in the middle, as wanted in the assignment.
TEST button, in our problem, only ran one single test, later I hit ATTEMPT, it ran 12 different tests to ensure that the code works in all possible cases, then I hit SUBMIT button which appeared just after attempting.
You could then rate the Kata and see people's solutions...etc.
Hope you enjoyed this, all questions are welcome!
You could follow me on @codewars, and you could join my Facebook Group if you wish to join our Codewars clan and keep learning ruby and improving your logic with code kata, and get help.

Kata Link: codewars.com/kata/554b4ac87…

Bye!❤️

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Khalid Benjelloun

Khalid Benjelloun Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

:(