• Stars
    star
    104
  • Rank 330,604 (Top 7 %)
  • Language
    Ruby
  • Created over 9 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

CHIPS 2.5 Ruby Introduction Assignment for Agile Development using Ruby on Rails

Ruby Intro

This 3-part homework gives some basic practice in Ruby as well as getting you accustomed to making testing a regular part of your workflow.

NOTE: If you are working on a local computer, do not clone this repo to your workspace. Fork it first, then clone your fork.

Learning Goals

After completing this assignment, you will know how to:

  • Write simple code that uses basic constructs in the Ruby language, including methods and arguments, conditionals, string and array manipulation, regular expressions, and basic object-oriented programming mechanisms
  • Understand the Ruby project conventions for where code files and test files are located in a project's directory hierarchy
  • Run individual tests or suites of tests using the RSpec unit testing tool
  • Understand the basic syntax of RSpec unit tests

Overview

You may find the Ruby documentation at ruby-doc.org helpful to have on hand.

The repo for this assigment follows a fairly standard Ruby convention for codebases: the code files are stored in lib/ and the test files are stored in spec/. (We use the RSpec unit-testing framework; if we were using Ruby's default framework, known as Test::Unit, the test files would be under test/.)

We've placed "starter code" in lib/ruby_intro.rb; when you're all done, you can submit this single file to the autograder.

However, you can test each of the 3 parts separately. The files spec/part[123]_spec.rb contain RSpec tests for each of the three parts. For example, to test your answers to Part 1, say rspec spec/part1_spec.rb. rspec with no arguments runs the tests in all the files spec/*_spec.rb.

  • The line numbers in the RSpec error report will give you guidance as to which tests failed. (You can check the RSpec documentation to see how the .rspec file can be used to customize the output format.)

If you are working in Codio, you are ready to move on to the next part. If you are working on a local computer, proceed with the following steps.

Local computer setup
Ensure that Ruby 2.6.6 is installed.

To ensure you have the rspec gem installed you need bundler and can then run bundle install like so:

$ gem install bundler
$ cd assignment
$ bundle

When the above completes successfully you'll have RSpec installed and can run rspec from the command line to test your code.

1. Arrays, Hashes, and Enumerables

Check the Ruby 2.x documentation on Array, Hash and Enumerable as they could help tremendously with these exercises. Various Ruby cheatsheets are also helpful as a quick reference! Although Ruby supports looping constructs like 'for' and 'while', consider using block syntax with each for a more idiomatic use of Ruby. :-)

  1. Define a method sum(array) that takes an array of integers as an argument and returns the sum of its elements. For an empty array it should return zero. Run associated tests via: $ rspec -e '#sum ' spec/part1_spec.rb (Make sure you are in the correct directory: cd assignment and rspec is installed)

  2. Define a method max_2_sum(array) which takes an array of integers as an argument and returns the sum of its two largest elements. For an empty array it should return zero. For an array with just one element, it should return that element (Consider if the two largest elements are the same value as well). Run associated tests via: $ rspec -e '#max_2_sum' spec/part1_spec.rb

  3. Define a method sum_to_n?(array, n) that takes an array of integers and an additional integer, n, as arguments and returns true if any two elements in the array of integers sum to n. sum_to_n?([], n) should return false for any value of n, by definition. Run associated tests via: $ rspec -e '#sum_to_n' spec/part1_spec.rb

You can check your progress on all of the above by running $ rspec spec/part1_spec.rb.

2. Strings and Regular Expressions

Check the documentation on String and Regexp as they could help tremendously with these exercises. For future reference as well, check out https://rubular.com/ for regex related queries. :-)

  1. Define a method hello(name) that takes a string representing a name and returns the string "Hello, " concatenated with the name. Run associated tests via: $ rspec -e '#hello' spec/part2_spec.rb (Make sure you are in the correct directory: cd assignment)

  2. Define a method starts_with_consonant?(s) that takes a string and returns true if it starts with a consonant and false otherwise. (For our purposes, a consonant is any English letter other than A, E, I, O, U.) Make sure it works for both upper and lower case and for non-letters. Run associated tests via: $ rspec -e '#starts_with_consonant?' spec/part2_spec.rb

  3. Define a method binary_multiple_of_4?(s) that takes a string and returns true if the string represents a binary number that is a multiple of 4, such as '1000'. Make sure it returns false if the string is not a valid binary number. Run associated tests via: $ rspec -e '#binary_multiple_of_4?' spec/part2_spec.rb

You can check your progress on all of the above by running $ rspec spec/part2_spec.rb.

3. Object Oriented Basics

Define a class BookInStock which represents a book with an ISBN number, isbn, and price of the book as a floating-point number, price, as attributes. Run associated tests via: $ rspec -e 'getters and setters' spec/part3_spec.rb (Make sure you are in the correct directory: cd assignment)

The constructor should accept the ISBN number (a string, since in real life ISBN numbers can begin with zero and can include hyphens) as the first argument and price as second argument, and should raise ArgumentError (one of Ruby's built-in exception types) if the ISBN number is the empty string or if the price is less than or equal to zero. Include the proper getters and setters for these attributes. Run associated tests via: $ rspec -e 'constructor' spec/part3_spec.rb

Include a method price_as_string that returns the price of the book formatted with a leading dollar sign and two decimal places, that is, a price of 20 should format as $20.00 and a price of 33.8 should format as $33.80. Check out formatted string methods in Ruby. Run associated tests via: $ rspec -e '#price_as_string' spec/part3_spec.rb

You can check your progress on all of the above by running rspec spec/part3_spec.rb.

More Challenges

  • Try getting setup with an automated test framework such as guard or autotest. Guard or AutoTest can be set up so that they will run all the tests in spec/, but every time you edit and save your code file, the tests are automatically re-run, so you don't have to run them manually. As we'll see later, this is the "watch the test fail" part of the TDD or test-driven process of development: write the tests before you write the code, watch the test fail, fill in the code and save the code file, then watch the test pass!

  • Try pairing using the one-undermanship pair programming style

More Repositories

1

courseware

Courseware setup and information for instructors
Ruby
245
star
2

ruql

Ruby DSL based quiz maker (RUby Question Language)
Ruby
61
star
3

rag

Ruby Auto-grader
Ruby
44
star
4

rottenpotatoes-rails-intro

RottenPotatoes app skeleton for saasbook/hw-rails-intro
Ruby
33
star
5

hw-sinatra-saas-wordguesser

Wordguess: a scaffolded (!) ESaaS getting-started assignment using Sinatra
Ruby
32
star
6

hw-bdd-cucumber

CHIPS 7.7 Starter Code
Ruby
15
star
7

hw-rails-intro

Ruby
13
star
8

hw-activerecord-practice

Practice with ActiveRecord using RSpec
Ruby
8
star
9

hw-oracle-of-bacon

Ruby
8
star
10

hw-http-intro

CHIPS 3.3 Scaffolded activity to learn about HTTP, URIs, etc using curl and nc
6
star
11

hw-refactoring-legacy-code-bug-fix

Legacy Assignment involving a bug fix
6
star
12

hw-acceptance-unit-test-cycle

Ruby
5
star
13

rails-routing-practice

Simple app to help ESaaS students practice their understanding of Rails routing
Ruby
5
star
14

hw-rails-wordguesser

4.6 Rails version of hw-sinatra-saas-hangperson, to show differences between Sinatra and Rails
Ruby
4
star
15

enrollme

EnrollMe
Ruby
3
star
16

hw-indices-performance

Skeleton code for the Performance Assignment
Ruby
3
star
17

coursequestionbank

CS169 Course Question Bank
Ruby
3
star
18

hw-hello-rails

CHIPS 4.8 Create your first Rails up (companion to the intro rails chapter in the ESaaS textbook)
3
star
19

berkeley-reentry-student-program

Ruby
3
star
20

esaas-engagements

Track client orgs and app development engagements for "Built By ESaaS" apps
Ruby
3
star
21

simple-cookie-demo

Simple demo app for cookies, to accompany the repo 'hw-http-intro'
Ruby
3
star
22

hw2olx

Generate OLX (edX Open Learning XML) modules from properly-formatted homework README.md files.
Ruby
3
star
23

saasbook.info

SaaSbook public info site
SCSS
2
star
24

Berkeley-Student-Food-Collective

Ruby
2
star
25

lime

Project for Berkeley Innovation Resources API
Ruby
2
star
26

AV102

An open repo for the AV102 Managing Distributed Teams class
2
star
27

LEAD-Center

Ruby
2
star
28

edx-bundle

Modify edXML course definition so videos can be served locally rather than over the Interwebs.
Ruby
2
star
29

hw-tdd-rspec

Revised RSpec assignment for adding Director field to Movies model in RottenPotatoes app
Ruby
1
star
30

CNSC-Chinatown

169 Project
Ruby
1
star
31

baytrailbirding

Mobile-friendly version of SFBBO guide to birding hotspots along the San Francisco Bay Trail
Ruby
1
star
32

ruql-canvas

Canvas LMS API "renderer" for RuQL gem
Ruby
1
star
33

chinese-newcomers-service-center

Ruby
1
star
34

bernal-heights

Bernal Heights App
Ruby
1
star
35

screencast-transcripts

.srt files for ESaaS screencast transcripts in various languages
1
star
36

projectscope_mvp

CS 169 Spring 2016 Group 39
Ruby
1
star
37

rails-intro

Skeleton code and specs for the Rails Intro Assignment
Ruby
1
star
38

OpenHouse

Ruby
1
star
39

Hispanics-in-Computing

1
star
40

PrairieLearn-EECS

App to author assessments in Berkeley EECS's instance of PrairieLearn.
Ruby
1
star
41

cs370

HTML
1
star
42

Women-in-Chemistry-Initiative-WICI

Ruby
1
star
43

LEP

JavaScript
1
star
44

Practical-Resistance-Alliance-X

Ruby
1
star
45

hw-accessibility-audits

1
star
46

faludi-lca-app

HTML
1
star
47

berkeley-food-collective

Ruby
1
star
48

berkeley-showcase

HTML
1
star
49

CS169_Great_Course_Guide

A SaaS application made as a project for CS169 Software Engineering at the University of California, Berkeley.
CSS
1
star
50

BerkeleyMarketPlace

Ruby
1
star
51

Bail-Reform-at-Berkeley

Ruby
1
star
52

hw-acceptance-unit-test-cycle-lite

Lite version of hw-acceptance-unit-test-cycle, with some features and scaffolding already written
Ruby
1
star