Category:I704 Ruby: Difference between revisions

From ICO wiki
Jump to navigationJump to search
(Show detailed exception handling)
(Add information about making HTTP requests)
Line 178: Line 178:
   end
   end
end
end
</syntaxhighlight>
= Reference material: making HTTP requests in Ruby =
1. Require the "net/http" library in your program
2. Use that library to make HTTP requests for fetching the HTML code behind a website
3. See https://ruby-doc.org/stdlib-2.3.0/libdoc/net/http/rdoc/Net/HTTP.html for more examples of how to use this library
<syntaxhighlight lang="ruby">
require 'net/http'
google_html = Net::HTTP.get(URI('http://google.com'))
# "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n<TITLE>302 Moved</TITLE></HEAD><BODY>\n<H1>302 Moved</H1>\nThe document has moved\n<A HREF=\"http://www.google.ee/?gfe_rd=cr&amp;ei=o6KlWKCGA9Oq8wfoq5OQAg\">here</A>.\r\n</BODY></HTML>\r\n"
# Now you can work with the HTML returned by the server


</syntaxhighlight>
</syntaxhighlight>

Revision as of 16:09, 16 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.

Lecture Recordings

You can find recordings of the lectures here:

https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd

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'
require 'minitest/autorun'

class BankAccount
  class Error < StandardError; end
  class BalanceNegative < Error; end

  def initialize
    @balance = 0
  end

  def deposit!(amount)
    increse_balance!(amount)

    self
  end

  def withdraw!(amount)
    if enough_money?(amount)
      reduce_balance!(amount)
    else
      raise StandardError, 'Not enough money!'
    end

    self
  end

  def balance
    @balance
  end

  private

  def enough_money?(amount)
    balance >= amount
  end

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

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

end

class BankAccountTest < Minitest::Test
  def setup
    @account = BankAccount.new
  end

  def test_if_decreses_the_balance_when_withdrawing_money
    @account.deposit!(101).withdraw!(50)
    assert_equal 51, @account.balance
  end

  def test_if_increses_the_balance_when_depositing_money
    @account.deposit!(196583)
    assert_equal 196583, @account.balance
  end

  def test_if_account_does_not_go_in_to_negative
    assert_raises BankAccount::BalanceNegative do
      @account.deposit!(1).withdraw!(20)
    end
  end

  def test_it_raises_bank_account_error_when_withdrawing_too_much_money
    @account.deposit!(1).withdraw!(20)
  rescue BankAccount::Error => err
    assert true, 'everything is ok'
  end
end

Reference material: making HTTP requests in Ruby

1. Require the "net/http" library in your program 2. Use that library to make HTTP requests for fetching the HTML code behind a website 3. See https://ruby-doc.org/stdlib-2.3.0/libdoc/net/http/rdoc/Net/HTTP.html for more examples of how to use this library

require 'net/http'
google_html = Net::HTTP.get(URI('http://google.com'))
# "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n<TITLE>302 Moved</TITLE></HEAD><BODY>\n<H1>302 Moved</H1>\nThe document has moved\n<A HREF=\"http://www.google.ee/?gfe_rd=cr&amp;ei=o6KlWKCGA9Oq8wfoq5OQAg\">here</A>.\r\n</BODY></HTML>\r\n"

# Now you can work with the HTML returned by the server

This category currently contains no pages or media.