Category: Ruby
-
Keyword (Named) Parameters
Introduced in Ruby 2.0, keyword parameters provide a way to pass arguments to a method by explicitly specifying parameter names and their corresponding values. This feature proves valuable, especially in scenarios involving methods with numerous parameters, as it significantly enhances code readability and clarity. Example Usage: Definition: When defining a method, keyword parameters can be…
-
Arrays in Ruby
Arrays are ordered collections of objects. They can hold any type of Ruby object: numbers, strings, other arrays, hashes, classes and methods. Here is what a basic array looks like and is used for: Arrays are created by placing a list of objects between square brackets. Accessing arrays The data in these arrays can be…
-
Looping in Ruby
Looping is one of the basic programming concepts that you will find in Ruby. It lets you to execute a block of code repeatedly. Ruby has a few ways to implement loops. First up: `.while`. The `.while` loop repeats a block of code over and over while the given condition remains true. When the condition…
-
Ranges with 2 or 3 dots
Ruby ranges with either two or three periods are valid ranges. However, the 2 dot and 3 dot formats produce different ranges. The 3 dot ranges exclude the end value (eg. they are not inclusive). While the 2 dot ranges are inclusive (they include the highest number). Here is a simple example:
-
Ruby Constants
Constants store values that cannot be changed during execution. For example: In this example, `PI` is a constant representing the value of Pi. Once assigned, the value of `PI` remains the same throughout the execution of the program. This leads to code clarity and prevents an inadvertent alteration of the value of PI in another…
-
Ruby String Basics
Strings are a core part of the Ruby language and are constantly used when programming in Ruby. They can be either single or double quoted: You can print out strings: Strings are commonly stored in a variable: Strings are concatenated like this: Ruby strings are commonly concatenated using the `+` operator. Ruby can also concatenate…
-
Ruby off Rails
So, Ruby is well known for its hugely popular gem Rails which greatly simplifies building websites. But what are some other programs built-in Ruby? Here are a few very popular examples. RubyMotion – Develop native apps for iOS (both iPhone and iPad) and OS X in Ruby. It is based on MacRuby which was developed…
-
Naming Ruby Gems
When it comes to naming a Ruby gem, the process involves a thoughtful consideration of community conventions, creativity, and uniqueness. In this context, let’s delve into the rationale behind naming my gem “tiedye.” Community Conventions Adhering to community conventions is a crucial first step in gem naming. The convention I followed involves using underscores in…