CronDose | Daily Dev Guides

Daily Snippets for the Developer Life

Build a User Class that Can Be Passed a Block to Set Its Values in Ruby

In this Ruby coding exercise your knowledge of blocks will be tested. The requirement is to build a User class that can be created and passed a block to set its values in addition to the traditional process for setting an object’s data. Summary Build a User Class that Can Be Passed a Block to Set Its Values. Exercise File Code File Exercise Description In order to properly pass this coding exercise you’ll need to create a Ruby User class that is flexible enough to take a block along with the traditional data setting process.. […]

Create and Deploy a RubyGem from Scratch

Taking a different approach than normal, in this coding exercise we’re going to create and a deploy a RubyGem to rubygems.org. Process Generate the Gem bundle gem tippy Edit the Gemspec File # coding: utf-8 lib = File.expand_path(‘../lib’, __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require ‘tippy/version’ Gem::Specification.new do |spec| spec.name = “tippy” spec.version = Tippy::VERSION spec.authors = [“Jordan Hudgens”] spec.email = [“[email protected]”] spec.summary = %q{A gem for generating tip values based on a number of inputs.} spec.homepage = “https://github.com/jordanhudgens/tippy” spec.license = “MIT” spec.files = `git ls-files -z`.split(“\x0”).reject do |f| f.match(%r{^(test|spec|features)/}) end spec.bindir = “exe” spec.executables = spec.files.grep(%r{^exe/}) […]

Build a Tip Calculator in Ruby that Can Accept Multiple Data Types as Input

In this coding exercise we’ll build out an extensive tip calculator that can accept integers, string based numbers and even words to dynamically generate a tip. Summary Build a Ruby module that can accurately process tip amounts based on input with multiple data type options. Exercise File Code File Exercise Description The key to this exercise is understanding how to properly manage multiple data type inputs and then to process the tip calculations. Example Behavior Tippy::Builder.new(total: 100, gratuity: ‘23.5’).generate # 123.5 Tippy::Builder.new(total: 100, gratuity: ‘high’).generate # 125.0 Tippy::Builder.new(total: 100, gratuity: ‘LOW’).generate # 115.0 Tippy::Builder.new(total: 100, […]

Build a Multiplication Table Generator in Ruby

This coding exercise will test your ability to work with various data structures while performing mathematical calculations as you build a dynamic multiplication table generator in Ruby. Summary Build a program that dynamically generates a hash based multiplication table in Ruby. Exercise File Code File Exercise Description In order to pass this exercise you’ll need to build a method that returns a hash based multiplication table, where the key is the base number, and the value are the products of the key and the numbers 1 through 10. Example multiplication_table 3 { 1=>[1, 2, 3, […]

How to Check if a Value Exists in a Set of Nested Hashes in Ruby

This coding exercise extends our knowledge on the Hash data structure and walks through how to check and see if a value exists in any number of Hash collections nested inside an array. Summary Build a method that looks through an array of hashes to see if a value exists anywhere inside of one of the hashes. Exercise File Code File Exercise Description In order to properly pass this coding exercise you’ll need to open up Ruby’s Array class and add a method called value_included?that iterates through the array of hashes and returns true if […]

Rebuild the include? Method in Ruby to Check for Array Values

In this coding exercise we’ll walk through how to rebuild Ruby’s include? method to see if a value exists inside of an array. Summary Rebuild Ruby’s include? method and add a method to the Array class that checks to see if a value exists within an array. Exercise File Code File Exercise Description In order to properly pass this coding exercise you’ll need to open up Ruby’s Array class and add a method called does_it_have? that takes in an argument and then have the method return true or false depending on whether the element passed […]

Code Interview Question: Build a High-Low Game in Ruby

This Ruby coding problem tests your ability to implement conditionals, work with user input, and generate random numbers. Summary Build the High/Low guessing game in Ruby where a user attempts to guess a number and the program lets them know if they need to guess higher or lower. Exercise File Code File Exercise Description This needs to be a terminal game that initially prompts the user to guess a number, you will also need to generate a random number so the answer will be different for each game. The start of the game should look […]

Generating a Hash from Two Arrays in Ruby

This coding exercise examines how to build a method that takes in two arrays and outputs a hash where one array makes up the keys and the other array makes up the values. Summary Build a Hash generating method that creates a key/value Hash based on the data from two arrays. Exercise File Code File Exercise Description Build a method that takes in two arrays as arguments and returns a hash where one array makes up the keys and the other array makes up the values. Sample Input arr_1 = [‘title’, ‘description’, ‘rating’] arr_2 = […]

How to Rotate Elements in an Array in Ruby

In this coding exercise we’ll walk through how to easily rotate the items inside a Ruby array and change the index values for each element. Summary Build a method that dynamically rotates the indices for each of the values in a Ruby array. Exercise File Code File Exercise Description Build a method that takes two arguments: an array, and the number of indices that the array elements should be rotated. Sample Use array_rotation([1, 2, 3], 2) # [3, 1, 2] Real World Usage There are many times when you want to quickly rotate the elements […]

Reverse the Letters in a String Without Using the Reverse Method in Ruby

In this coding exercise you’ll re-create the reverse method in Ruby. You are required to build a method that reverses the characters in a string and returns the reversed values. Summary Build a method that reverses the characters in a string without using the reverse method. Exercise File Code File Exercise Description You will need to perform monkey patching and open up the String class in Ruby. From there create a method called alt_reversethat reverses the characters in a string without calling the reverse method provided by Ruby. Sample Use “Hi there”.alt_reverse # “ereht iH” […]

By Daniele Zedda • 18 February

← PREV POST

By Daniele Zedda • 18 February

NEXT POST → 34
Share on