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!)
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.
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!❤️
Share this Scrolly Tale with your friends.
A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.