As most of our participants are not familiar with Ruby, we have created
some exercises to learn and practice Ruby syntax - covering conditional
statements (if
, else
, elsif
), different ways of iteration (each
,
select
, map
) and basic object oriented code (defining and using
class, instance functions).
If you have studied any other programming language - the exercises are about translating known concepts to Ruby and should be easy.
If concepts like variable
, function
and class
are new to you, you
might require a gentler introduction to programming before learning
Rails. You can refer to books on programming or websites like
TryRuby and
Learn Ruby the Hard Way.
Read the installation guide and setting up local workspace to get started.
Fizz Buzz is a word game for children to teach them about division.
We will use it to learn about transforming traditional C-like for loops into ruby code and a bit of error handling.
The rules of the game are as follows:
- The first player says the number "1".
- Each subsequent player counts upwards in turn.
- Numbers divisible by 3 are replaced by the word "Fizz"
- Numbers divisible by 5 are replaced by the word "Buzz"
- Numbers divisible by both 3 and 5 are replaced by the word "FizzBuzz"
For example, a round of Fizz Buzz would go as follows:
1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz,
16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz, 28, 29,
FizzBuzz, 31, 32, Fizz, 34, Buzz, Fizz, ...
We will take a look at generalization of Fizz Buzz - instead of numbers
divisible by 3 or 5, we will let the user decide. They are denoted by
keyword arguments x
and y
.
The first keyword argument n
represents the number of turns.
We will be returning an array of strings, containing what each player says on their turn.
For example, fizz_buzz(n: 6, x: 2, y: 3)
should return the following:
["1", "Fizz", "Buzz", "Fizz", "5", "FizzBuzz"]
What happens if the function is called by x (or y) = 0 or negative?
The divisiblity by zero is not defined and neither do the little children know how to divide by negtive numbers.
We will raise an ArgumentError
exception to let the caller know that
their arguments were incorrect.
Implement the function
fizz_buzz
in the filefizz_buzz.rb
and test your code withruby fizz_buzz_test.rb
.
- Fizz Buzz - Wikipedia
- Ruby - if...else, case, unless - TutorialPoint
- Ruby Keyword Arguments
- How to Use Ruby Conversion Methods
- A Beginner's Guide to Exceptions in Ruby
You are writing for a group that uses Machine Learning techniques to read and summarize books and articles into smaller texts while retaining the meaning.
Since the technique works only on lower case words, you want to write
two functions lower_case(words)
and lower_case!(words)
which
transform an array of words (representing contents of an article) into
all lower case characters. The function lower_case
returns an copy of
the original array, transforming upper case to lower case characters
whereas the function lower_case!
modifies the array in-place.
words = ['', 'HELLO', 'WoRlD', 'nice']
lower_case(words) # returns ['', 'hello', 'world', 'nice']
puts words # ['', 'HELLO', 'WoRlD', 'nice']
lower_case!(words) # returns nil
puts words # ['', 'hello', 'world', nice']
The technique relies on prefix of words, you want to implement
words_with_prefix(prefix, words)
which returns an array containing
only words that have the same prefix.
words_with_prefix('apple', ['apple', 'ball', 'applesauce']) # returns ['apple', 'applesauce']
To eliminate redundant words (and thus compressing the text), you define
similarity score between two words as the length of the largest common
prefix between the words. The function similarity_score
takes two
words and returns the similarity score.
similarity_score('bike', 'bite') # 2, as 'bi' is the largest common prefix.
similarity_score('apple', 'bite') # 0, as there are no common letters in the prefixes.
To find the best possible match for a word from a given list of words
(and retaining the meaning as far as possible), you choose the word with
the maximum similarity score. The function
most_similar_words(chosen_word, words)
find the best possible match(s)
for the chosen word.
most_similar_words('apple', ['ball', 'applesauce', 'bike']) # returns ['applesauce']
most_similar_words('apple', ['ball', 'applesauce', 'bike', 'apple']) # returns ['applesauce', 'apple]
Implement the functions described above in the file
word_processing.rb
and test your code withruby word_processing_test.rb
.
- Substring - Wikipedia
- downcase (String) - APIdock
- start_with? (String)
- Ruby Arrays and Common Array Methods
You are working for an e-commerce company Buy-n-Sell and working on their inventory functionality.
The items sold by the company are represented by the class Item
. An
Item
stores the following information:
- Name of the item.
- Price of the item.
- The category item belongs to.
- The discount rate if the item is on sale.
- The time when the sale ends.
You want to store quantity
of the item too, so the company can keep
track of items which need to be re-stocked.
To help the company in analyzing their inventory, you come up with the following metric:
- An item is discounted if the discount deadline has not been yet crossed.
- The current price of a discounted item is
price * (100 - discount rate ) /100
. - The current price of an regular item is same as
price
. - The stock price of item is defined as the product of current price and quantity.
- The stock price of a category is sum of individual stock prices of item belonging to the category.
The function stock_price_by_category(items)
takes an array of items
and calculates the stock price of all categories.
Implement the class
Item
above in the fileitem.rb
and test your code withruby item_test.rb
.
- How to use attr_accessor, attr_writer and attr_reader
- Ruby - Classes and Objects
- Class: Time
- Class and Instance Methods in Ruby
- group_by (Enumerable)
- reduce (Enumerable)
You can use the interactive ruby (irb
) to help understand and debug
your code. irb
is similar to python's interactive console and lets you
run any ruby code.
For example, to debug fizz_buzz.rb
do the following:
- Open a ruby console using
irb
. - Include the ruby program using
require_relative 'fizz_buzz'
. - Execute the function (or any valid ruby statement using the interpreter).