To make you familar with ruby syntax, we have created some exercises to help you
learn and practice Ruby syntax - covering conditional statements (if, else, elsif),
different ways of iteration (each, map) and some object oriented code.
If you not familiar with programming concepts like variable, function and class,
you can refer to the following resources
Follow this guide to setup your workspace
- Make sure you are in the correct directory(week_2) while running commands
cd week_2
ls #pet.rb pet_test.rb ....
rvm gemset create week_2
rvm use 3.0.5@week_2
ruby <file_name>- Use the week2_<your_name> branch for your work
git checkout -b week2_<your_name>- Your Pull Request will be graded by the automated tests.
- Once you have completed the assignment, submit a Pull
Request with your work to the branch
week2_assignment(notmain).
This is a simple assignment to familiarize ourself with ruby. In this assignment we will learn about loops in ruby and a bit of error handling.
A prime number is a whole number greater than 1 that cannot be exactly divided by any whole number other than itself and 1 (e.g. 2, 3, 5, 7, 11).
The function below takes a keyword argument n and
returns an array of prime numbers less than or equal to n.
For example, prime_numbers(n: 20) should return the following:
[2, 3, 5, 7, 11, 13, 17, 19]
If the user gives a invalid input like -4.
We will raise an ArgumentError exception to let the caller know that
their function arguments were incorrect.
Note - Do not use use the inbuilt prime library
Implement the function
prime_numbersin the fileprime_numbers.rband test your code withruby prime_test.rb.
- 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 working at a pet store and want to build a system to let the pet owners know the maintenance cost of their pets.
The pet is represented using the Pet class. A Pet stores
the following information:
- Name of the pet (
name) - Type of animal(Mammals, Fish ...)(
animal_type_id) - Food consumed by the pet per day in Kg(
food_consumed_per_day)
Implement the class
Petabove in the filepet.rband test your code withruby pet_test.rb.
- attr_accessor, attr_writer and attr_reader
- map
- Ruby - Classes and Objects
- 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 prime_numbers.rb do the following:
- Open a ruby console using
irb. - Include the ruby program using
require_relative 'prime_numbers'. - Execute the function (or any valid ruby statement using the interpreter).
