Category:I704 Ruby: Difference between revisions

From ICO wiki
Jump to navigationJump to search
Dhamidi (talk | contribs)
Created page with "= About this course = This course teaches the Ruby programming language. By the end of the course you'll hopefully have a good understanding of: * the basics of Ruby, * too..."
 
Dhamidi (talk | contribs)
Share bank account example
Line 92: Line 92:
   end
   end
   puts debit_total
   puts debit_total
end
</syntaxhighlight>
= 2017-02-09 Lecture
<syntaxhighlight lang="ruby" line="line">
require 'minitest'
class BankAccount
  def initialize
    @balance = 0
  end
  def deposit!(amount)
    increase_balance!(amount)
    self
  end
  def withdraw!(amount)
    if enough_money?(amount)
      reduce_balance!(amount)
    else
      raise StandardError, "not enough money on account"
    end
    self
  end
  def balance
    @balance
  end
  private
  def enough_money?(amount)
    balance >= amount
  end
  def increase_balance!(amount)
    @balance = @balance + amount
  end
  def reduce_balance!(amount)
    @balance = @balance - amount
  end
end
end
</syntaxhighlight>
</syntaxhighlight>

Revision as of 13:14, 9 February 2017

About this course

This course teaches the Ruby programming language. By the end of the course you'll hopefully have a good understanding of:

  • the basics of Ruby,
  • tools commonly used in the Ruby ecosystem,
  • written a few small Ruby applications,
  • know about unit testing,
  • know how to use third-party code (Ruby gems),
  • know how to write web applications using Ruby.

About yourself

To help me make this course interesting for you and meet your expectations, please fill out this survey if you haven't done so already:

http://bit.ly/2jxwrs8

Reference material

Here you find a list of useful links to things that have been mentioned or discussed during the lectures:

Editors/IDEs

Grading

Students develop several small projects during the lectures and independent study. At the end of the course students pick one of their projects and are assigned a feature request to implement in their project and a set of questions about the code in their project.

Points are awarded for the following:

  • 50 points for a working implementation of the feature request
  • 40 points for an implementation of the feature request that works only for expected inputs
  • 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)
  • 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).
  • 20 points for providing automated tests for their implementation
  • 10 points for proper use of version control
  • 10 points for adhering to common Ruby coding standards
  • 20 points for answering 80% of the questions correctly

The student needs at least 60 points in order to pass the course.

2017-02-02: Lecture and Lab

Analyzing bank statements

Given the following contents of a file called input.csv

   transaction_id,date,amount,credit
   1,2017-02-02 12:40,1.30,debit
   2,2017-02-02 12:55,2.50,debit
   3,2017-02-02 13:00,1.00,credit

Goal: find the amount of money left on your bank account.

Steps:

  1. read the data line by line
  2. analyze each line to find out whether it's credit or debit and the amount of money
  3. add all the debit transaction amounts (money lost)
  4. add all the credit transaction amounts (money gained)
  5. Output money gained - money lost

Our code so far:

File.open('input.csv', 'r') do |the_file|
  lines = the_file.readlines.map do |line|
    line.chomp.split(',')
  end
  lines = lines[1..-1]
  debit_total = ''
  lines.each do |line|
    debit_total = debit_total + line[2]
  end
  puts debit_total
end

= 2017-02-09 Lecture

require 'minitest'

class BankAccount
  def initialize
    @balance = 0
  end

  def deposit!(amount)
    increase_balance!(amount)

    self
  end

  def withdraw!(amount)
    if enough_money?(amount)
      reduce_balance!(amount)
    else
      raise StandardError, "not enough money on account"
    end

    self
  end

  def balance
    @balance
  end

  private

  def enough_money?(amount)
    balance >= amount
  end

  def increase_balance!(amount)
    @balance = @balance + amount
  end

  def reduce_balance!(amount)
    @balance = @balance - amount
  end
end

This category currently contains no pages or media.