An intro to ruby from a guy who only started it a week ago himself
As you can tell from the title I recently started learning ruby. For a bit of background, ruby is a high-level language written in C. Wait so it's a coding language written in another coding language what does even that mean. Great question me. C is a lower-level language which means it has less background processing and built-in functionality which leaves us with an incredibly powerful and less resource-intensive language with the downside of it taking longer to write learn and has a higher potential to cause damage if used incorrectly. Ruby on the other hand, while having less raw power, has is easier to pick up, has a lot of built-in functionality, safety protocols, and due to its prevalence, huge community that has made many libraries that allow users to quickly write efficient and safe code. Now let's jump into basic conventions and tools.
With the basics out of the way, the rest of my example will be done through IRB an interactive ruby terminal in the console which is accessible by typing “irb” in your console.
I highly recommend you get a ruby environment setup and try this all for yourself by the way. Now let's talk a bit about some useful operators. All of which you can write out irb.
First are some math operators.
/
: Divison*
: Multiplication+
: Addition-
: Subtraction**
: Exponents%
: Modulus or Divison with the remainder
None of these need an explanation except for the last. basically its division but instead of returning how many times one number goes into another like
49/7 = 7
It returns what's left over after the division so
49%7 = 0
50%7 = 1
52%7 = 3
second the comparison operators. They compare two values and return a boolean, which just means a true value or a false value here's a quick list.
>
: greater than>=
: greater than or equal to<
: less than<=
: less than or equal to==
: equal to!=
: not equal to
Unlike some languages, ruby only has a strict equals operator which just means that a string of one will not equal an integer of one.
Next up we have the logical operators.
&&
: AND returns true if both values truthy||
: OR returns true if at least one value is truthy!
: Not returns the opposite of the given value (!true returns false)
some slightly more complex examples are
Let's jump straight into some variables. This might sound intimidating but if you've even taken algebra 1 you should catch on really quick.
A variable is just a name that we've declared should give you something else. A variable can be defined using another variable and all operators can be used on them
If you’ve written in other languages you might’ve heard of functions before ruby has methods and though they may have a different structure and name they're essentially the same concept
As you can see above methods are first initiated using a def keyword, this lets ruby know that what comes after is the name of the method, in this example, it is “example”. Then the functionality, in this case, ‘puts “testing”’ puts is a built-in method that prints to the console, and finally the “end” keyword which denotes the end of the method.
As you can see after the end I have written “example” this is called an invocation that tells the program to run the method without this the method would be skipped over and in this case, nothing would happen.
At this point If you’ve downloaded ruby or an extension for it onto your code editor, you should be able to run a file by first using the ruby keyword which runs the file that follows it. As you can see after running the file it has put testing into the console line.
After running the file we get a return value of testing. now if we hadn't put puts then by default a method returns the value of the last line in a method. Which you can see in examples 2 and 3 below using methods we made in irb. We can still print multiple things however if you call puts on something it will still print it.
With all of this you have all the basic tools you need to start experimenting and making some cool programs let this be the first stepping stone in you’re coding journey have fun and good luck.