<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.itcollege.ee/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Dhamidi</id>
	<title>ICO wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.itcollege.ee/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Dhamidi"/>
	<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php/Special:Contributions/Dhamidi"/>
	<updated>2026-06-21T12:42:16Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=119336</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=119336"/>
		<updated>2017-03-23T10:21:39Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= Lecture Recordings =&lt;br /&gt;
&lt;br /&gt;
You can find recordings of the lectures here:&lt;br /&gt;
&lt;br /&gt;
https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
* [https://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-select Ruby Enumerable]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for a having a README describing your project&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 10 points for structuring your project as a Rubygem&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
== Proper use of version control ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# you keep your code in a git repository&lt;br /&gt;
# a single commit represents a single logical step in the development process (e.g. adding a test and the code to make it pass, fixing a single bug, refactoring one area of the code, etc)&lt;br /&gt;
# most commit messages follow the guidelines for good commit messages (see reference material below)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://chris.beams.io/posts/git-commit/ How to write a Git Commit Message] (see &amp;quot;The Seven Rules&amp;quot;)&lt;br /&gt;
* [http://nuclearsquid.com/writings/git-add/ How to select a subset of all changes for your next commit]&lt;br /&gt;
&lt;br /&gt;
== Having a README ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# your project has a file called README.md in your project&#039;s root directory&lt;br /&gt;
# the README.md file contains a description of: what your program does, how to install it and how to run it&lt;br /&gt;
&lt;br /&gt;
== Adhering to common Ruby coding standards ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# running &amp;lt;code&amp;gt;rubocop&amp;lt;/code&amp;gt; with the default configuration in your project produces no errors&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [http://rubocop.readthedocs.io/en/latest/basic_usage/#basic-usage Rubocop Basic Usage].  &#039;&#039;&#039;Hint:&#039;&#039;&#039;  You can run Rubocop with the &amp;lt;code&amp;gt;--auto-correct&amp;lt;/code&amp;gt; option to automatically fix some of the problems Rubocop discovered.&lt;br /&gt;
&lt;br /&gt;
== Providing automated tests ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 20&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# running &amp;lt;code&amp;gt;bundle exec rake test&amp;lt;/code&amp;gt; runs your test suite&lt;br /&gt;
# your test suite has no failing tests&lt;br /&gt;
# your test suite contains at least one meaningful test, testing your program&#039;s logic&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://launchschool.com/blog/assert-yourself-an-introduction-to-minitest Introduction to MiniTest]&lt;br /&gt;
&lt;br /&gt;
== Structuring your project as a Rubygem ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# your project directory structure matches the structure created by &amp;lt;code&amp;gt;bundle gem NAME_OF_YOUR_PROJECT&amp;lt;/code&amp;gt;&lt;br /&gt;
# additional dependencies are specified in the &amp;lt;code&amp;gt;.gemspec&amp;lt;/code&amp;gt; file of your project&lt;br /&gt;
&lt;br /&gt;
If your project is using a framework with strong conventions, such as Ruby on Rails, these points are awarded if you place your code according to the conventions of the framework.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://code.tutsplus.com/tutorials/gem-creation-with-bundler--net-25281 Creating a gem with Bundler]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Answering 80% of the questions correctly ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 20&lt;br /&gt;
&lt;br /&gt;
You will be asked 3 to 5 questions about your project, testing your understanding of what is happening in the code.&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# you answer 80% or more of the questions correctly&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  class Error &amp;lt; StandardError; end&lt;br /&gt;
  class BalanceNegative &amp;lt; Error; end&lt;br /&gt;
&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increse_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &#039;Not enough money!&#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increse_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def setup&lt;br /&gt;
    @account = BankAccount.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_decreses_the_balance_when_withdrawing_money&lt;br /&gt;
    @account.deposit!(101).withdraw!(50)&lt;br /&gt;
    assert_equal 51, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_increses_the_balance_when_depositing_money&lt;br /&gt;
    @account.deposit!(196583)&lt;br /&gt;
    assert_equal 196583, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_account_does_not_go_in_to_negative&lt;br /&gt;
    assert_raises BankAccount::BalanceNegative do&lt;br /&gt;
      @account.deposit!(1).withdraw!(20)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_it_raises_bank_account_error_when_withdrawing_too_much_money&lt;br /&gt;
    @account.deposit!(1).withdraw!(20)&lt;br /&gt;
  rescue BankAccount::Error =&amp;gt; err&lt;br /&gt;
    assert true, &#039;everything is ok&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-16 Lecture and Lab: using modules and bundler =&lt;br /&gt;
&lt;br /&gt;
== Modules in Ruby ==&lt;br /&gt;
&lt;br /&gt;
* Modules are a collection of methods that can be included in another class&lt;br /&gt;
* By including the module in a class, objects belonging to that class get access to the methods defined in the module&lt;br /&gt;
* If multiple included modules define the same method, the method from the module that was included last counts&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
module ModuleExample&lt;br /&gt;
  def foo&lt;br /&gt;
    &#039;foo&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Including modules binds their methods to the class instances&lt;br /&gt;
# Extending modules binds their methods to the class itself&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  include ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Book&lt;br /&gt;
  extend ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Person.foo     # =&amp;gt; NoMethodError: undefined method `foo&#039; for Person:Class&lt;br /&gt;
Person.new.foo # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.foo       # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.new.foo   # =&amp;gt; NoMethodError: undefined method `foo&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
== Structuring a project with bundler ==&lt;br /&gt;
&lt;br /&gt;
* bundler is a Ruby program for managing dependencies in your ruby project&lt;br /&gt;
* bundler also helps you with creating a template for a project&lt;br /&gt;
* redistributable projects/libraries are called &amp;quot;gems&amp;quot; in ruby&lt;br /&gt;
* You can start your own project by running &amp;lt;code&amp;gt;bundle gem YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt;.  Replace &amp;lt;code&amp;gt;YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt; with the lower-cased name of your project.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example structure of a project, as generated by bundler&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ tree&lt;br /&gt;
.&lt;br /&gt;
├── bin&lt;br /&gt;
│   ├── console&lt;br /&gt;
│   └── setup&lt;br /&gt;
├── exe&lt;br /&gt;
│   └── hello-world&lt;br /&gt;
├── Gemfile&lt;br /&gt;
├── Gemfile.lock&lt;br /&gt;
├── I704.gemspec&lt;br /&gt;
├── lib&lt;br /&gt;
│   ├── I704&lt;br /&gt;
│   │   ├── bank_account.rb&lt;br /&gt;
│   │   └── version.rb&lt;br /&gt;
│   └── I704.rb&lt;br /&gt;
├── LICENSE.txt&lt;br /&gt;
├── Rakefile&lt;br /&gt;
├── README.md&lt;br /&gt;
└── test&lt;br /&gt;
    ├── I704&lt;br /&gt;
    │   └── bank_account_test.rb&lt;br /&gt;
    ├── I704_test.rb&lt;br /&gt;
    └── test_helper.rb&lt;br /&gt;
&lt;br /&gt;
6 directories, 15 files&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* exectuables that users of your gem should be able to run go into the &amp;lt;code&amp;gt;exe&amp;lt;/code&amp;gt; directory&lt;br /&gt;
* add dependencies in your &amp;lt;code&amp;gt;.gemspec&amp;lt;/code&amp;gt; file.  After adding a dependency, run &amp;lt;code&amp;gt;bundle install&amp;lt;/code&amp;gt; to install the dependency on your computer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rule of thumb&#039;&#039;&#039;: put your code into the &amp;lt;code&amp;gt;lib/YOUR_PROJECT_NAME&amp;lt;/code&amp;gt; directory, one file per class (like &amp;lt;code&amp;gt;bank_account.rb&amp;lt;/code&amp;gt; in the example above)&lt;br /&gt;
&lt;br /&gt;
= 2017-03-02 Lecture &amp;amp; Lab =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Word count example&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Burning Rangers is a 1998 3D action video game developed by Sonic Team and published by Sega for the Sega Saturn. The game is set in a futuristic society threatened by frequent fires. Players control one of an elite group of firefighters, the Burning Rangers, who extinguish the fires and rescue civilians from burning buildings. Most of the tasks the players complete are centred around collecting energy crystals used to transport civilians to safety. Development began shortly after the release of Christmas Nights in November 1996, when Yuji Naka started working on a game focused on saving people rather than killing them. Sonic Team chose the themes of firefighting and heroism. Burning Rangers received mostly positive reviews, especially for the game&#039;s soundtrack and audio. Responses to the graphics were mixed; while some critics asserted that the game had the best visuals on the Saturn, others faulted its poor collision detection and occasional glitching. The game was among the final five Saturn titles released in America.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
words = text.split(&#039; &#039;)&lt;br /&gt;
&lt;br /&gt;
words_without_punctuation = words.map do |word|&lt;br /&gt;
  word.sub(/[.,;?!:]/, &#039;&#039;).downcase&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
word_count = words_without_punctuation.reduce({}) do |result, word|&lt;br /&gt;
  if result.key?(word)&lt;br /&gt;
    result[word] = result[word] + 1&lt;br /&gt;
    # also possible: result[word] += 1&lt;br /&gt;
  else&lt;br /&gt;
    result[word] = 1&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  result&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts word_count&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-03-09 lecture =&lt;br /&gt;
&lt;br /&gt;
== Using locks ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;thread&#039;&lt;br /&gt;
&lt;br /&gt;
# [1, 2, ..., 100]&lt;br /&gt;
# [t1, t2, ..., t100]&lt;br /&gt;
&lt;br /&gt;
# Lock / Mutex (mutual exclusion)&lt;br /&gt;
# Queue&lt;br /&gt;
&lt;br /&gt;
lock = Mutex.new&lt;br /&gt;
threads = (1..100).map do |number|&lt;br /&gt;
  Thread.new do&lt;br /&gt;
    sleep 0.5&lt;br /&gt;
&lt;br /&gt;
    lock.lock&lt;br /&gt;
    puts number&lt;br /&gt;
    lock.unlock&lt;br /&gt;
&lt;br /&gt;
    lock.synchronize do&lt;br /&gt;
      if number == 1&lt;br /&gt;
        sleep 5&lt;br /&gt;
      end&lt;br /&gt;
      puts number&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads.each do |thread|&lt;br /&gt;
  thread.join&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using a queue ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;thread&#039;&lt;br /&gt;
&lt;br /&gt;
# Queue&lt;br /&gt;
&lt;br /&gt;
queue = Queue.new&lt;br /&gt;
&lt;br /&gt;
printer = Thread.new do&lt;br /&gt;
  loop do&lt;br /&gt;
    number = queue.pop&lt;br /&gt;
    puts number&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads = (1..100).map do |number|&lt;br /&gt;
  Thread.new do&lt;br /&gt;
    sleep 0.5&lt;br /&gt;
    queue.push number&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads.each do |thread|&lt;br /&gt;
  thread.join&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using fork ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
shared_variable = &#039;untouched&#039;&lt;br /&gt;
child_process_id = fork do&lt;br /&gt;
  puts &amp;quot;Running inside fork&amp;quot;&lt;br /&gt;
  puts &amp;quot;My process ID is #{$$}&amp;quot;&lt;br /&gt;
  shared_variable = &#039;touched&#039;&lt;br /&gt;
end&lt;br /&gt;
Process.wait(child_process_id)&lt;br /&gt;
puts &amp;quot;In the parent #{$$}: shared_variable is #{shared_variable}&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: making HTTP requests in Ruby =&lt;br /&gt;
&lt;br /&gt;
# Require the &amp;quot;net/http&amp;quot; library in your program&lt;br /&gt;
# Use that library to make HTTP requests for fetching the HTML code behind a website&lt;br /&gt;
# 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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;net/http&#039;&lt;br /&gt;
google_html = Net::HTTP.get(URI(&#039;http://google.com&#039;))&lt;br /&gt;
# &amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=\&amp;quot;content-type\&amp;quot; content=\&amp;quot;text/html;charset=utf-8\&amp;quot;&amp;gt;\n&amp;lt;TITLE&amp;gt;302 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;\n&amp;lt;H1&amp;gt;302 Moved&amp;lt;/H1&amp;gt;\nThe document has moved\n&amp;lt;A HREF=\&amp;quot;http://www.google.ee/?gfe_rd=cr&amp;amp;amp;ei=o6KlWKCGA9Oq8wfoq5OQAg\&amp;quot;&amp;gt;here&amp;lt;/A&amp;gt;.\r\n&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;\r\n&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Now you can work with the HTML returned by the server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-03-16 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;pry&#039;&lt;br /&gt;
&lt;br /&gt;
##&lt;br /&gt;
#&lt;br /&gt;
#  NEVER RESCUE FROM Exception&lt;br /&gt;
#&lt;br /&gt;
#  HAVE AN ERROR CLASS FOR APPLICATION AND HAVE ALL APPLICATION ERRORS&lt;br /&gt;
#  INHERIT FROM IT.&lt;br /&gt;
##&lt;br /&gt;
&lt;br /&gt;
# Options for error handling:&lt;br /&gt;
#&lt;br /&gt;
# 1. raise an exception&lt;br /&gt;
# 2. return a value describing the error&lt;br /&gt;
module ErrorHandling&lt;br /&gt;
  class Error &amp;lt; StandardError&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class InvalidNumberError &amp;lt; Error&lt;br /&gt;
    def initialize(input)&lt;br /&gt;
      @input = input&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    attr_reader :input&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class DivisionByZeroAttempted &amp;lt; Error&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class WrongDayOfTheWeek &amp;lt; Error&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Application&lt;br /&gt;
    def run&lt;br /&gt;
      raise WrongDayOfTheWeek unless Date.today.monday?&lt;br /&gt;
&lt;br /&gt;
      print(&amp;quot;Enter a number: &amp;quot;)&lt;br /&gt;
      a = read_number&lt;br /&gt;
      print(&amp;quot;Enter another number: &amp;quot;)&lt;br /&gt;
      b = read_number&lt;br /&gt;
      if b.zero?&lt;br /&gt;
        raise DivisionByZeroAttempted&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;a / b = #{a / b}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def read_number&lt;br /&gt;
      input = $stdin.gets.chomp&lt;br /&gt;
      begin&lt;br /&gt;
        Float(input)&lt;br /&gt;
      rescue ArgumentError&lt;br /&gt;
        raise InvalidNumberError.new(input)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using return values for indicating errors ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;pry&#039;&lt;br /&gt;
module ErrorHandling&lt;br /&gt;
  class Application&lt;br /&gt;
    def run&lt;br /&gt;
      print(&amp;quot;Enter a number: &amp;quot;)&lt;br /&gt;
      a = read_number&lt;br /&gt;
      until a != :invalid_input&lt;br /&gt;
        if a == :invalid_input&lt;br /&gt;
          puts &amp;quot;Not a number!&amp;quot;&lt;br /&gt;
        end&lt;br /&gt;
        a = read_number&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      print(&amp;quot;Enter another number: &amp;quot;)&lt;br /&gt;
      b = read_number&lt;br /&gt;
      if b == :invalid_input&lt;br /&gt;
        puts &amp;quot;Not a number!&amp;quot;&lt;br /&gt;
        return&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      if b.zero?&lt;br /&gt;
        puts &amp;quot;You cannot divide by zero!&amp;quot;&lt;br /&gt;
        return&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;a / b = #{a/b}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def read_number&lt;br /&gt;
      input = $stdin.gets.chomp&lt;br /&gt;
      begin&lt;br /&gt;
        Float(input)&lt;br /&gt;
      rescue ArgumentError&lt;br /&gt;
        :invalid_input&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-03-23 Lecture =&lt;br /&gt;
&lt;br /&gt;
== Using Struct ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
module Blog&lt;br /&gt;
  Post = Struct.new(:id, :title, :body_text) do&lt;br /&gt;
    def initialize(id:, title: &#039;no title&#039;, body_text: &#039;no body&#039;)&lt;br /&gt;
      super(id, title, body_text)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def validate&lt;br /&gt;
      errors = {}&lt;br /&gt;
      errors[:title] = :empty if title.empty?&lt;br /&gt;
      errors[:id] = :empty if id.empty?&lt;br /&gt;
      errors&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
first_article = Blog::Post.new(&lt;br /&gt;
  id: &#039;another-post&#039;&lt;br /&gt;
)&lt;br /&gt;
puts first_article&lt;br /&gt;
puts first_article.validate&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: parsing HTML using Nokogiri =&lt;br /&gt;
&lt;br /&gt;
Once you have received some HTML from a web server, you most likely want to analyze it.  Use the library nokogiri for this.  You can find installation instructions and tutorials here: http://www.nokogiri.org/tutorials/&lt;br /&gt;
&lt;br /&gt;
= Reference material: map and reduce =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# map: takes a list and a block&lt;br /&gt;
#    returns: a list, where each element of the original list has been&lt;br /&gt;
#    passed to block&lt;br /&gt;
#&lt;br /&gt;
# reduce: takes list, an initial value and a block&lt;br /&gt;
#    returns: block applied to initial value and each element of the list&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = numbers.reduce(0) do |current_sum, number|&lt;br /&gt;
  current_sum + number&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;Sum: #{sum}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
####&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = 0&lt;br /&gt;
numbers.each do |number|&lt;br /&gt;
  sum = sum + number&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118603</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118603"/>
		<updated>2017-03-16T11:11:05Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= Lecture Recordings =&lt;br /&gt;
&lt;br /&gt;
You can find recordings of the lectures here:&lt;br /&gt;
&lt;br /&gt;
https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
* [https://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-select Ruby Enumerable]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for a having a README describing your project&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 10 points for structuring your project as a Rubygem&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
== Proper use of version control ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# you keep your code in a git repository&lt;br /&gt;
# a single commit represents a single logical step in the development process (e.g. adding a test and the code to make it pass, fixing a single bug, refactoring one area of the code, etc)&lt;br /&gt;
# most commit messages follow the guidelines for good commit messages (see reference material below)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://chris.beams.io/posts/git-commit/ How to write a Git Commit Message] (see &amp;quot;The Seven Rules&amp;quot;)&lt;br /&gt;
* [http://nuclearsquid.com/writings/git-add/ How to select a subset of all changes for your next commit]&lt;br /&gt;
&lt;br /&gt;
== Having a README ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# your project has a file called README.md in your project&#039;s root directory&lt;br /&gt;
# the README.md file contains a description of: what your program does, how to install it and how to run it&lt;br /&gt;
&lt;br /&gt;
== Adhering to common Ruby coding standards ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# running &amp;lt;code&amp;gt;rubocop&amp;lt;/code&amp;gt; with the default configuration in your project produces no errors&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [http://rubocop.readthedocs.io/en/latest/basic_usage/#basic-usage Rubocop Basic Usage].  &#039;&#039;&#039;Hint:&#039;&#039;&#039;  You can run Rubocop with the &amp;lt;code&amp;gt;--auto-correct&amp;lt;/code&amp;gt; option to automatically fix some of the problems Rubocop discovered.&lt;br /&gt;
&lt;br /&gt;
== Providing automated tests ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 20&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# running &amp;lt;code&amp;gt;bundle exec rake test&amp;lt;/code&amp;gt; runs your test suite&lt;br /&gt;
# your test suite has no failing tests&lt;br /&gt;
# your test suite contains at least one meaningful test, testing your program&#039;s logic&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://launchschool.com/blog/assert-yourself-an-introduction-to-minitest Introduction to MiniTest]&lt;br /&gt;
&lt;br /&gt;
== Structuring your project as a Rubygem ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# your project directory structure matches the structure created by &amp;lt;code&amp;gt;bundle gem NAME_OF_YOUR_PROJECT&amp;lt;/code&amp;gt;&lt;br /&gt;
# additional dependencies are specified in the &amp;lt;code&amp;gt;.gemspec&amp;lt;/code&amp;gt; file of your project&lt;br /&gt;
&lt;br /&gt;
If your project is using a framework with strong conventions, such as Ruby on Rails, these points are awarded if you place your code according to the conventions of the framework.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://code.tutsplus.com/tutorials/gem-creation-with-bundler--net-25281 Creating a gem with Bundler]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Answering 80% of the questions correctly ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 20&lt;br /&gt;
&lt;br /&gt;
You will be asked 3 to 5 questions about your project, testing your understanding of what is happening in the code.&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# you answer 80% or more of the questions correctly&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  class Error &amp;lt; StandardError; end&lt;br /&gt;
  class BalanceNegative &amp;lt; Error; end&lt;br /&gt;
&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increse_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &#039;Not enough money!&#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increse_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def setup&lt;br /&gt;
    @account = BankAccount.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_decreses_the_balance_when_withdrawing_money&lt;br /&gt;
    @account.deposit!(101).withdraw!(50)&lt;br /&gt;
    assert_equal 51, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_increses_the_balance_when_depositing_money&lt;br /&gt;
    @account.deposit!(196583)&lt;br /&gt;
    assert_equal 196583, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_account_does_not_go_in_to_negative&lt;br /&gt;
    assert_raises BankAccount::BalanceNegative do&lt;br /&gt;
      @account.deposit!(1).withdraw!(20)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_it_raises_bank_account_error_when_withdrawing_too_much_money&lt;br /&gt;
    @account.deposit!(1).withdraw!(20)&lt;br /&gt;
  rescue BankAccount::Error =&amp;gt; err&lt;br /&gt;
    assert true, &#039;everything is ok&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-16 Lecture and Lab: using modules and bundler =&lt;br /&gt;
&lt;br /&gt;
== Modules in Ruby ==&lt;br /&gt;
&lt;br /&gt;
* Modules are a collection of methods that can be included in another class&lt;br /&gt;
* By including the module in a class, objects belonging to that class get access to the methods defined in the module&lt;br /&gt;
* If multiple included modules define the same method, the method from the module that was included last counts&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
module ModuleExample&lt;br /&gt;
  def foo&lt;br /&gt;
    &#039;foo&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Including modules binds their methods to the class instances&lt;br /&gt;
# Extending modules binds their methods to the class itself&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  include ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Book&lt;br /&gt;
  extend ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Person.foo     # =&amp;gt; NoMethodError: undefined method `foo&#039; for Person:Class&lt;br /&gt;
Person.new.foo # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.foo       # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.new.foo   # =&amp;gt; NoMethodError: undefined method `foo&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
== Structuring a project with bundler ==&lt;br /&gt;
&lt;br /&gt;
* bundler is a Ruby program for managing dependencies in your ruby project&lt;br /&gt;
* bundler also helps you with creating a template for a project&lt;br /&gt;
* redistributable projects/libraries are called &amp;quot;gems&amp;quot; in ruby&lt;br /&gt;
* You can start your own project by running &amp;lt;code&amp;gt;bundle gem YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt;.  Replace &amp;lt;code&amp;gt;YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt; with the lower-cased name of your project.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example structure of a project, as generated by bundler&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ tree&lt;br /&gt;
.&lt;br /&gt;
├── bin&lt;br /&gt;
│   ├── console&lt;br /&gt;
│   └── setup&lt;br /&gt;
├── exe&lt;br /&gt;
│   └── hello-world&lt;br /&gt;
├── Gemfile&lt;br /&gt;
├── Gemfile.lock&lt;br /&gt;
├── I704.gemspec&lt;br /&gt;
├── lib&lt;br /&gt;
│   ├── I704&lt;br /&gt;
│   │   ├── bank_account.rb&lt;br /&gt;
│   │   └── version.rb&lt;br /&gt;
│   └── I704.rb&lt;br /&gt;
├── LICENSE.txt&lt;br /&gt;
├── Rakefile&lt;br /&gt;
├── README.md&lt;br /&gt;
└── test&lt;br /&gt;
    ├── I704&lt;br /&gt;
    │   └── bank_account_test.rb&lt;br /&gt;
    ├── I704_test.rb&lt;br /&gt;
    └── test_helper.rb&lt;br /&gt;
&lt;br /&gt;
6 directories, 15 files&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* exectuables that users of your gem should be able to run go into the &amp;lt;code&amp;gt;exe&amp;lt;/code&amp;gt; directory&lt;br /&gt;
* add dependencies in your &amp;lt;code&amp;gt;.gemspec&amp;lt;/code&amp;gt; file.  After adding a dependency, run &amp;lt;code&amp;gt;bundle install&amp;lt;/code&amp;gt; to install the dependency on your computer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rule of thumb&#039;&#039;&#039;: put your code into the &amp;lt;code&amp;gt;lib/YOUR_PROJECT_NAME&amp;lt;/code&amp;gt; directory, one file per class (like &amp;lt;code&amp;gt;bank_account.rb&amp;lt;/code&amp;gt; in the example above)&lt;br /&gt;
&lt;br /&gt;
= 2017-03-02 Lecture &amp;amp; Lab =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Word count example&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Burning Rangers is a 1998 3D action video game developed by Sonic Team and published by Sega for the Sega Saturn. The game is set in a futuristic society threatened by frequent fires. Players control one of an elite group of firefighters, the Burning Rangers, who extinguish the fires and rescue civilians from burning buildings. Most of the tasks the players complete are centred around collecting energy crystals used to transport civilians to safety. Development began shortly after the release of Christmas Nights in November 1996, when Yuji Naka started working on a game focused on saving people rather than killing them. Sonic Team chose the themes of firefighting and heroism. Burning Rangers received mostly positive reviews, especially for the game&#039;s soundtrack and audio. Responses to the graphics were mixed; while some critics asserted that the game had the best visuals on the Saturn, others faulted its poor collision detection and occasional glitching. The game was among the final five Saturn titles released in America.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
words = text.split(&#039; &#039;)&lt;br /&gt;
&lt;br /&gt;
words_without_punctuation = words.map do |word|&lt;br /&gt;
  word.sub(/[.,;?!:]/, &#039;&#039;).downcase&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
word_count = words_without_punctuation.reduce({}) do |result, word|&lt;br /&gt;
  if result.key?(word)&lt;br /&gt;
    result[word] = result[word] + 1&lt;br /&gt;
    # also possible: result[word] += 1&lt;br /&gt;
  else&lt;br /&gt;
    result[word] = 1&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  result&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts word_count&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-03-09 lecture =&lt;br /&gt;
&lt;br /&gt;
== Using locks ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;thread&#039;&lt;br /&gt;
&lt;br /&gt;
# [1, 2, ..., 100]&lt;br /&gt;
# [t1, t2, ..., t100]&lt;br /&gt;
&lt;br /&gt;
# Lock / Mutex (mutual exclusion)&lt;br /&gt;
# Queue&lt;br /&gt;
&lt;br /&gt;
lock = Mutex.new&lt;br /&gt;
threads = (1..100).map do |number|&lt;br /&gt;
  Thread.new do&lt;br /&gt;
    sleep 0.5&lt;br /&gt;
&lt;br /&gt;
    lock.lock&lt;br /&gt;
    puts number&lt;br /&gt;
    lock.unlock&lt;br /&gt;
&lt;br /&gt;
    lock.synchronize do&lt;br /&gt;
      if number == 1&lt;br /&gt;
        sleep 5&lt;br /&gt;
      end&lt;br /&gt;
      puts number&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads.each do |thread|&lt;br /&gt;
  thread.join&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using a queue ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;thread&#039;&lt;br /&gt;
&lt;br /&gt;
# Queue&lt;br /&gt;
&lt;br /&gt;
queue = Queue.new&lt;br /&gt;
&lt;br /&gt;
printer = Thread.new do&lt;br /&gt;
  loop do&lt;br /&gt;
    number = queue.pop&lt;br /&gt;
    puts number&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads = (1..100).map do |number|&lt;br /&gt;
  Thread.new do&lt;br /&gt;
    sleep 0.5&lt;br /&gt;
    queue.push number&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads.each do |thread|&lt;br /&gt;
  thread.join&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using fork ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
shared_variable = &#039;untouched&#039;&lt;br /&gt;
child_process_id = fork do&lt;br /&gt;
  puts &amp;quot;Running inside fork&amp;quot;&lt;br /&gt;
  puts &amp;quot;My process ID is #{$$}&amp;quot;&lt;br /&gt;
  shared_variable = &#039;touched&#039;&lt;br /&gt;
end&lt;br /&gt;
Process.wait(child_process_id)&lt;br /&gt;
puts &amp;quot;In the parent #{$$}: shared_variable is #{shared_variable}&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: making HTTP requests in Ruby =&lt;br /&gt;
&lt;br /&gt;
# Require the &amp;quot;net/http&amp;quot; library in your program&lt;br /&gt;
# Use that library to make HTTP requests for fetching the HTML code behind a website&lt;br /&gt;
# 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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;net/http&#039;&lt;br /&gt;
google_html = Net::HTTP.get(URI(&#039;http://google.com&#039;))&lt;br /&gt;
# &amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=\&amp;quot;content-type\&amp;quot; content=\&amp;quot;text/html;charset=utf-8\&amp;quot;&amp;gt;\n&amp;lt;TITLE&amp;gt;302 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;\n&amp;lt;H1&amp;gt;302 Moved&amp;lt;/H1&amp;gt;\nThe document has moved\n&amp;lt;A HREF=\&amp;quot;http://www.google.ee/?gfe_rd=cr&amp;amp;amp;ei=o6KlWKCGA9Oq8wfoq5OQAg\&amp;quot;&amp;gt;here&amp;lt;/A&amp;gt;.\r\n&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;\r\n&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Now you can work with the HTML returned by the server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-03-16 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;pry&#039;&lt;br /&gt;
&lt;br /&gt;
##&lt;br /&gt;
#&lt;br /&gt;
#  NEVER RESCUE FROM Exception&lt;br /&gt;
#&lt;br /&gt;
#  HAVE AN ERROR CLASS FOR APPLICATION AND HAVE ALL APPLICATION ERRORS&lt;br /&gt;
#  INHERIT FROM IT.&lt;br /&gt;
##&lt;br /&gt;
&lt;br /&gt;
# Options for error handling:&lt;br /&gt;
#&lt;br /&gt;
# 1. raise an exception&lt;br /&gt;
# 2. return a value describing the error&lt;br /&gt;
module ErrorHandling&lt;br /&gt;
  class Error &amp;lt; StandardError&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class InvalidNumberError &amp;lt; Error&lt;br /&gt;
    def initialize(input)&lt;br /&gt;
      @input = input&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    attr_reader :input&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class DivisionByZeroAttempted &amp;lt; Error&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class WrongDayOfTheWeek &amp;lt; Error&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Application&lt;br /&gt;
    def run&lt;br /&gt;
      raise WrongDayOfTheWeek unless Date.today.monday?&lt;br /&gt;
&lt;br /&gt;
      print(&amp;quot;Enter a number: &amp;quot;)&lt;br /&gt;
      a = read_number&lt;br /&gt;
      print(&amp;quot;Enter another number: &amp;quot;)&lt;br /&gt;
      b = read_number&lt;br /&gt;
      if b.zero?&lt;br /&gt;
        raise DivisionByZeroAttempted&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;a / b = #{a / b}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def read_number&lt;br /&gt;
      input = $stdin.gets.chomp&lt;br /&gt;
      begin&lt;br /&gt;
        Float(input)&lt;br /&gt;
      rescue ArgumentError&lt;br /&gt;
        raise InvalidNumberError.new(input)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using return values for indicating errors ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;pry&#039;&lt;br /&gt;
module ErrorHandling&lt;br /&gt;
  class Application&lt;br /&gt;
    def run&lt;br /&gt;
      print(&amp;quot;Enter a number: &amp;quot;)&lt;br /&gt;
      a = read_number&lt;br /&gt;
      until a != :invalid_input&lt;br /&gt;
        if a == :invalid_input&lt;br /&gt;
          puts &amp;quot;Not a number!&amp;quot;&lt;br /&gt;
        end&lt;br /&gt;
        a = read_number&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      print(&amp;quot;Enter another number: &amp;quot;)&lt;br /&gt;
      b = read_number&lt;br /&gt;
      if b == :invalid_input&lt;br /&gt;
        puts &amp;quot;Not a number!&amp;quot;&lt;br /&gt;
        return&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      if b.zero?&lt;br /&gt;
        puts &amp;quot;You cannot divide by zero!&amp;quot;&lt;br /&gt;
        return&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      puts &amp;quot;a / b = #{a/b}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def read_number&lt;br /&gt;
      input = $stdin.gets.chomp&lt;br /&gt;
      begin&lt;br /&gt;
        Float(input)&lt;br /&gt;
      rescue ArgumentError&lt;br /&gt;
        :invalid_input&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: parsing HTML using Nokogiri =&lt;br /&gt;
&lt;br /&gt;
Once you have received some HTML from a web server, you most likely want to analyze it.  Use the library nokogiri for this.  You can find installation instructions and tutorials here: http://www.nokogiri.org/tutorials/&lt;br /&gt;
&lt;br /&gt;
= Reference material: map and reduce =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# map: takes a list and a block&lt;br /&gt;
#    returns: a list, where each element of the original list has been&lt;br /&gt;
#    passed to block&lt;br /&gt;
#&lt;br /&gt;
# reduce: takes list, an initial value and a block&lt;br /&gt;
#    returns: block applied to initial value and each element of the list&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = numbers.reduce(0) do |current_sum, number|&lt;br /&gt;
  current_sum + number&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;Sum: #{sum}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
####&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = 0&lt;br /&gt;
numbers.each do |number|&lt;br /&gt;
  sum = sum + number&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118602</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118602"/>
		<updated>2017-03-16T10:58:37Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= Lecture Recordings =&lt;br /&gt;
&lt;br /&gt;
You can find recordings of the lectures here:&lt;br /&gt;
&lt;br /&gt;
https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
* [https://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-select Ruby Enumerable]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for a having a README describing your project&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 10 points for structuring your project as a Rubygem&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
== Proper use of version control ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# you keep your code in a git repository&lt;br /&gt;
# a single commit represents a single logical step in the development process (e.g. adding a test and the code to make it pass, fixing a single bug, refactoring one area of the code, etc)&lt;br /&gt;
# most commit messages follow the guidelines for good commit messages (see reference material below)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://chris.beams.io/posts/git-commit/ How to write a Git Commit Message] (see &amp;quot;The Seven Rules&amp;quot;)&lt;br /&gt;
* [http://nuclearsquid.com/writings/git-add/ How to select a subset of all changes for your next commit]&lt;br /&gt;
&lt;br /&gt;
== Having a README ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# your project has a file called README.md in your project&#039;s root directory&lt;br /&gt;
# the README.md file contains a description of: what your program does, how to install it and how to run it&lt;br /&gt;
&lt;br /&gt;
== Adhering to common Ruby coding standards ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# running &amp;lt;code&amp;gt;rubocop&amp;lt;/code&amp;gt; with the default configuration in your project produces no errors&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [http://rubocop.readthedocs.io/en/latest/basic_usage/#basic-usage Rubocop Basic Usage].  &#039;&#039;&#039;Hint:&#039;&#039;&#039;  You can run Rubocop with the &amp;lt;code&amp;gt;--auto-correct&amp;lt;/code&amp;gt; option to automatically fix some of the problems Rubocop discovered.&lt;br /&gt;
&lt;br /&gt;
== Providing automated tests ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 20&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# running &amp;lt;code&amp;gt;bundle exec rake test&amp;lt;/code&amp;gt; runs your test suite&lt;br /&gt;
# your test suite has no failing tests&lt;br /&gt;
# your test suite contains at least one meaningful test, testing your program&#039;s logic&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://launchschool.com/blog/assert-yourself-an-introduction-to-minitest Introduction to MiniTest]&lt;br /&gt;
&lt;br /&gt;
== Structuring your project as a Rubygem ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# your project directory structure matches the structure created by &amp;lt;code&amp;gt;bundle gem NAME_OF_YOUR_PROJECT&amp;lt;/code&amp;gt;&lt;br /&gt;
# additional dependencies are specified in the &amp;lt;code&amp;gt;.gemspec&amp;lt;/code&amp;gt; file of your project&lt;br /&gt;
&lt;br /&gt;
If your project is using a framework with strong conventions, such as Ruby on Rails, these points are awarded if you place your code according to the conventions of the framework.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://code.tutsplus.com/tutorials/gem-creation-with-bundler--net-25281 Creating a gem with Bundler]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Answering 80% of the questions correctly ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 20&lt;br /&gt;
&lt;br /&gt;
You will be asked 3 to 5 questions about your project, testing your understanding of what is happening in the code.&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# you answer 80% or more of the questions correctly&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  class Error &amp;lt; StandardError; end&lt;br /&gt;
  class BalanceNegative &amp;lt; Error; end&lt;br /&gt;
&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increse_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &#039;Not enough money!&#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increse_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def setup&lt;br /&gt;
    @account = BankAccount.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_decreses_the_balance_when_withdrawing_money&lt;br /&gt;
    @account.deposit!(101).withdraw!(50)&lt;br /&gt;
    assert_equal 51, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_increses_the_balance_when_depositing_money&lt;br /&gt;
    @account.deposit!(196583)&lt;br /&gt;
    assert_equal 196583, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_account_does_not_go_in_to_negative&lt;br /&gt;
    assert_raises BankAccount::BalanceNegative do&lt;br /&gt;
      @account.deposit!(1).withdraw!(20)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_it_raises_bank_account_error_when_withdrawing_too_much_money&lt;br /&gt;
    @account.deposit!(1).withdraw!(20)&lt;br /&gt;
  rescue BankAccount::Error =&amp;gt; err&lt;br /&gt;
    assert true, &#039;everything is ok&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-16 Lecture and Lab: using modules and bundler =&lt;br /&gt;
&lt;br /&gt;
== Modules in Ruby ==&lt;br /&gt;
&lt;br /&gt;
* Modules are a collection of methods that can be included in another class&lt;br /&gt;
* By including the module in a class, objects belonging to that class get access to the methods defined in the module&lt;br /&gt;
* If multiple included modules define the same method, the method from the module that was included last counts&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
module ModuleExample&lt;br /&gt;
  def foo&lt;br /&gt;
    &#039;foo&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Including modules binds their methods to the class instances&lt;br /&gt;
# Extending modules binds their methods to the class itself&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  include ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Book&lt;br /&gt;
  extend ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Person.foo     # =&amp;gt; NoMethodError: undefined method `foo&#039; for Person:Class&lt;br /&gt;
Person.new.foo # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.foo       # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.new.foo   # =&amp;gt; NoMethodError: undefined method `foo&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
== Structuring a project with bundler ==&lt;br /&gt;
&lt;br /&gt;
* bundler is a Ruby program for managing dependencies in your ruby project&lt;br /&gt;
* bundler also helps you with creating a template for a project&lt;br /&gt;
* redistributable projects/libraries are called &amp;quot;gems&amp;quot; in ruby&lt;br /&gt;
* You can start your own project by running &amp;lt;code&amp;gt;bundle gem YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt;.  Replace &amp;lt;code&amp;gt;YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt; with the lower-cased name of your project.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example structure of a project, as generated by bundler&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ tree&lt;br /&gt;
.&lt;br /&gt;
├── bin&lt;br /&gt;
│   ├── console&lt;br /&gt;
│   └── setup&lt;br /&gt;
├── exe&lt;br /&gt;
│   └── hello-world&lt;br /&gt;
├── Gemfile&lt;br /&gt;
├── Gemfile.lock&lt;br /&gt;
├── I704.gemspec&lt;br /&gt;
├── lib&lt;br /&gt;
│   ├── I704&lt;br /&gt;
│   │   ├── bank_account.rb&lt;br /&gt;
│   │   └── version.rb&lt;br /&gt;
│   └── I704.rb&lt;br /&gt;
├── LICENSE.txt&lt;br /&gt;
├── Rakefile&lt;br /&gt;
├── README.md&lt;br /&gt;
└── test&lt;br /&gt;
    ├── I704&lt;br /&gt;
    │   └── bank_account_test.rb&lt;br /&gt;
    ├── I704_test.rb&lt;br /&gt;
    └── test_helper.rb&lt;br /&gt;
&lt;br /&gt;
6 directories, 15 files&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* exectuables that users of your gem should be able to run go into the &amp;lt;code&amp;gt;exe&amp;lt;/code&amp;gt; directory&lt;br /&gt;
* add dependencies in your &amp;lt;code&amp;gt;.gemspec&amp;lt;/code&amp;gt; file.  After adding a dependency, run &amp;lt;code&amp;gt;bundle install&amp;lt;/code&amp;gt; to install the dependency on your computer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rule of thumb&#039;&#039;&#039;: put your code into the &amp;lt;code&amp;gt;lib/YOUR_PROJECT_NAME&amp;lt;/code&amp;gt; directory, one file per class (like &amp;lt;code&amp;gt;bank_account.rb&amp;lt;/code&amp;gt; in the example above)&lt;br /&gt;
&lt;br /&gt;
= 2017-03-02 Lecture &amp;amp; Lab =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Word count example&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Burning Rangers is a 1998 3D action video game developed by Sonic Team and published by Sega for the Sega Saturn. The game is set in a futuristic society threatened by frequent fires. Players control one of an elite group of firefighters, the Burning Rangers, who extinguish the fires and rescue civilians from burning buildings. Most of the tasks the players complete are centred around collecting energy crystals used to transport civilians to safety. Development began shortly after the release of Christmas Nights in November 1996, when Yuji Naka started working on a game focused on saving people rather than killing them. Sonic Team chose the themes of firefighting and heroism. Burning Rangers received mostly positive reviews, especially for the game&#039;s soundtrack and audio. Responses to the graphics were mixed; while some critics asserted that the game had the best visuals on the Saturn, others faulted its poor collision detection and occasional glitching. The game was among the final five Saturn titles released in America.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
words = text.split(&#039; &#039;)&lt;br /&gt;
&lt;br /&gt;
words_without_punctuation = words.map do |word|&lt;br /&gt;
  word.sub(/[.,;?!:]/, &#039;&#039;).downcase&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
word_count = words_without_punctuation.reduce({}) do |result, word|&lt;br /&gt;
  if result.key?(word)&lt;br /&gt;
    result[word] = result[word] + 1&lt;br /&gt;
    # also possible: result[word] += 1&lt;br /&gt;
  else&lt;br /&gt;
    result[word] = 1&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  result&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts word_count&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-03-09 lecture =&lt;br /&gt;
&lt;br /&gt;
== Using locks ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;thread&#039;&lt;br /&gt;
&lt;br /&gt;
# [1, 2, ..., 100]&lt;br /&gt;
# [t1, t2, ..., t100]&lt;br /&gt;
&lt;br /&gt;
# Lock / Mutex (mutual exclusion)&lt;br /&gt;
# Queue&lt;br /&gt;
&lt;br /&gt;
lock = Mutex.new&lt;br /&gt;
threads = (1..100).map do |number|&lt;br /&gt;
  Thread.new do&lt;br /&gt;
    sleep 0.5&lt;br /&gt;
&lt;br /&gt;
    lock.lock&lt;br /&gt;
    puts number&lt;br /&gt;
    lock.unlock&lt;br /&gt;
&lt;br /&gt;
    lock.synchronize do&lt;br /&gt;
      if number == 1&lt;br /&gt;
        sleep 5&lt;br /&gt;
      end&lt;br /&gt;
      puts number&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads.each do |thread|&lt;br /&gt;
  thread.join&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using a queue ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;thread&#039;&lt;br /&gt;
&lt;br /&gt;
# Queue&lt;br /&gt;
&lt;br /&gt;
queue = Queue.new&lt;br /&gt;
&lt;br /&gt;
printer = Thread.new do&lt;br /&gt;
  loop do&lt;br /&gt;
    number = queue.pop&lt;br /&gt;
    puts number&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads = (1..100).map do |number|&lt;br /&gt;
  Thread.new do&lt;br /&gt;
    sleep 0.5&lt;br /&gt;
    queue.push number&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads.each do |thread|&lt;br /&gt;
  thread.join&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using fork ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
shared_variable = &#039;untouched&#039;&lt;br /&gt;
child_process_id = fork do&lt;br /&gt;
  puts &amp;quot;Running inside fork&amp;quot;&lt;br /&gt;
  puts &amp;quot;My process ID is #{$$}&amp;quot;&lt;br /&gt;
  shared_variable = &#039;touched&#039;&lt;br /&gt;
end&lt;br /&gt;
Process.wait(child_process_id)&lt;br /&gt;
puts &amp;quot;In the parent #{$$}: shared_variable is #{shared_variable}&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: making HTTP requests in Ruby =&lt;br /&gt;
&lt;br /&gt;
# Require the &amp;quot;net/http&amp;quot; library in your program&lt;br /&gt;
# Use that library to make HTTP requests for fetching the HTML code behind a website&lt;br /&gt;
# 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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;net/http&#039;&lt;br /&gt;
google_html = Net::HTTP.get(URI(&#039;http://google.com&#039;))&lt;br /&gt;
# &amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=\&amp;quot;content-type\&amp;quot; content=\&amp;quot;text/html;charset=utf-8\&amp;quot;&amp;gt;\n&amp;lt;TITLE&amp;gt;302 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;\n&amp;lt;H1&amp;gt;302 Moved&amp;lt;/H1&amp;gt;\nThe document has moved\n&amp;lt;A HREF=\&amp;quot;http://www.google.ee/?gfe_rd=cr&amp;amp;amp;ei=o6KlWKCGA9Oq8wfoq5OQAg\&amp;quot;&amp;gt;here&amp;lt;/A&amp;gt;.\r\n&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;\r\n&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Now you can work with the HTML returned by the server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-03-16 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;pry&#039;&lt;br /&gt;
&lt;br /&gt;
##&lt;br /&gt;
#&lt;br /&gt;
#  NEVER RESCUE FROM Exception&lt;br /&gt;
#&lt;br /&gt;
#  HAVE AN ERROR CLASS FOR APPLICATION AND HAVE ALL APPLICATION ERRORS&lt;br /&gt;
#  INHERIT FROM IT.&lt;br /&gt;
##&lt;br /&gt;
&lt;br /&gt;
# Options for error handling:&lt;br /&gt;
#&lt;br /&gt;
# 1. raise an exception&lt;br /&gt;
# 2. return a value describing the error&lt;br /&gt;
module ErrorHandling&lt;br /&gt;
  class Error &amp;lt; StandardError&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class InvalidNumberError &amp;lt; Error&lt;br /&gt;
    def initialize(input)&lt;br /&gt;
      @input = input&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    attr_reader :input&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class DivisionByZeroAttempted &amp;lt; Error&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class WrongDayOfTheWeek &amp;lt; Error&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Application&lt;br /&gt;
    def run&lt;br /&gt;
      raise WrongDayOfTheWeek unless Date.today.monday?&lt;br /&gt;
&lt;br /&gt;
      print(&amp;quot;Enter a number: &amp;quot;)&lt;br /&gt;
      a = read_number&lt;br /&gt;
      print(&amp;quot;Enter another number: &amp;quot;)&lt;br /&gt;
      b = read_number&lt;br /&gt;
      if b.zero?&lt;br /&gt;
        raise DivisionByZeroAttempted&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;a / b = #{a / b}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def read_number&lt;br /&gt;
      input = $stdin.gets.chomp&lt;br /&gt;
      begin&lt;br /&gt;
        Float(input)&lt;br /&gt;
      rescue ArgumentError&lt;br /&gt;
        raise InvalidNumberError.new(input)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: parsing HTML using Nokogiri =&lt;br /&gt;
&lt;br /&gt;
Once you have received some HTML from a web server, you most likely want to analyze it.  Use the library nokogiri for this.  You can find installation instructions and tutorials here: http://www.nokogiri.org/tutorials/&lt;br /&gt;
&lt;br /&gt;
= Reference material: map and reduce =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# map: takes a list and a block&lt;br /&gt;
#    returns: a list, where each element of the original list has been&lt;br /&gt;
#    passed to block&lt;br /&gt;
#&lt;br /&gt;
# reduce: takes list, an initial value and a block&lt;br /&gt;
#    returns: block applied to initial value and each element of the list&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = numbers.reduce(0) do |current_sum, number|&lt;br /&gt;
  current_sum + number&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;Sum: #{sum}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
####&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = 0&lt;br /&gt;
numbers.each do |number|&lt;br /&gt;
  sum = sum + number&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118463</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118463"/>
		<updated>2017-03-09T11:26:48Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: /* Answering 80% of the questions correctly */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= Lecture Recordings =&lt;br /&gt;
&lt;br /&gt;
You can find recordings of the lectures here:&lt;br /&gt;
&lt;br /&gt;
https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
* [https://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-select Ruby Enumerable]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for a having a README describing your project&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 10 points for structuring your project as a Rubygem&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
== Proper use of version control ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# you keep your code in a git repository&lt;br /&gt;
# a single commit represents a single logical step in the development process (e.g. adding a test and the code to make it pass, fixing a single bug, refactoring one area of the code, etc)&lt;br /&gt;
# most commit messages follow the guidelines for good commit messages (see reference material below)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://chris.beams.io/posts/git-commit/ How to write a Git Commit Message] (see &amp;quot;The Seven Rules&amp;quot;)&lt;br /&gt;
* [http://nuclearsquid.com/writings/git-add/ How to select a subset of all changes for your next commit]&lt;br /&gt;
&lt;br /&gt;
== Having a README ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# your project has a file called README.md in your project&#039;s root directory&lt;br /&gt;
# the README.md file contains a description of: what your program does, how to install it and how to run it&lt;br /&gt;
&lt;br /&gt;
== Adhering to common Ruby coding standards ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# running &amp;lt;code&amp;gt;rubocop&amp;lt;/code&amp;gt; with the default configuration in your project produces no errors&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [http://rubocop.readthedocs.io/en/latest/basic_usage/#basic-usage Rubocop Basic Usage].  &#039;&#039;&#039;Hint:&#039;&#039;&#039;  You can run Rubocop with the &amp;lt;code&amp;gt;--auto-correct&amp;lt;/code&amp;gt; option to automatically fix some of the problems Rubocop discovered.&lt;br /&gt;
&lt;br /&gt;
== Providing automated tests ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 20&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# running &amp;lt;code&amp;gt;bundle exec rake test&amp;lt;/code&amp;gt; runs your test suite&lt;br /&gt;
# your test suite has no failing tests&lt;br /&gt;
# your test suite contains at least one meaningful test, testing your program&#039;s logic&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://launchschool.com/blog/assert-yourself-an-introduction-to-minitest Introduction to MiniTest]&lt;br /&gt;
&lt;br /&gt;
== Structuring your project as a Rubygem ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# your project directory structure matches the structure created by &amp;lt;code&amp;gt;bundle gem NAME_OF_YOUR_PROJECT&amp;lt;/code&amp;gt;&lt;br /&gt;
# additional dependencies are specified in the &amp;lt;code&amp;gt;.gemspec&amp;lt;/code&amp;gt; file of your project&lt;br /&gt;
&lt;br /&gt;
If your project is using a framework with strong conventions, such as Ruby on Rails, these points are awarded if you place your code according to the conventions of the framework.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://code.tutsplus.com/tutorials/gem-creation-with-bundler--net-25281 Creating a gem with Bundler]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Answering 80% of the questions correctly ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 20&lt;br /&gt;
&lt;br /&gt;
You will be asked 3 to 5 questions about your project, testing your understanding of what is happening in the code.&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# you answer 80% or more of the questions correctly&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  class Error &amp;lt; StandardError; end&lt;br /&gt;
  class BalanceNegative &amp;lt; Error; end&lt;br /&gt;
&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increse_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &#039;Not enough money!&#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increse_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def setup&lt;br /&gt;
    @account = BankAccount.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_decreses_the_balance_when_withdrawing_money&lt;br /&gt;
    @account.deposit!(101).withdraw!(50)&lt;br /&gt;
    assert_equal 51, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_increses_the_balance_when_depositing_money&lt;br /&gt;
    @account.deposit!(196583)&lt;br /&gt;
    assert_equal 196583, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_account_does_not_go_in_to_negative&lt;br /&gt;
    assert_raises BankAccount::BalanceNegative do&lt;br /&gt;
      @account.deposit!(1).withdraw!(20)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_it_raises_bank_account_error_when_withdrawing_too_much_money&lt;br /&gt;
    @account.deposit!(1).withdraw!(20)&lt;br /&gt;
  rescue BankAccount::Error =&amp;gt; err&lt;br /&gt;
    assert true, &#039;everything is ok&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-16 Lecture and Lab: using modules and bundler =&lt;br /&gt;
&lt;br /&gt;
== Modules in Ruby ==&lt;br /&gt;
&lt;br /&gt;
* Modules are a collection of methods that can be included in another class&lt;br /&gt;
* By including the module in a class, objects belonging to that class get access to the methods defined in the module&lt;br /&gt;
* If multiple included modules define the same method, the method from the module that was included last counts&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
module ModuleExample&lt;br /&gt;
  def foo&lt;br /&gt;
    &#039;foo&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Including modules binds their methods to the class instances&lt;br /&gt;
# Extending modules binds their methods to the class itself&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  include ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Book&lt;br /&gt;
  extend ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Person.foo     # =&amp;gt; NoMethodError: undefined method `foo&#039; for Person:Class&lt;br /&gt;
Person.new.foo # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.foo       # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.new.foo   # =&amp;gt; NoMethodError: undefined method `foo&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
== Structuring a project with bundler ==&lt;br /&gt;
&lt;br /&gt;
* bundler is a Ruby program for managing dependencies in your ruby project&lt;br /&gt;
* bundler also helps you with creating a template for a project&lt;br /&gt;
* redistributable projects/libraries are called &amp;quot;gems&amp;quot; in ruby&lt;br /&gt;
* You can start your own project by running &amp;lt;code&amp;gt;bundle gem YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt;.  Replace &amp;lt;code&amp;gt;YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt; with the lower-cased name of your project.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example structure of a project, as generated by bundler&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ tree&lt;br /&gt;
.&lt;br /&gt;
├── bin&lt;br /&gt;
│   ├── console&lt;br /&gt;
│   └── setup&lt;br /&gt;
├── exe&lt;br /&gt;
│   └── hello-world&lt;br /&gt;
├── Gemfile&lt;br /&gt;
├── Gemfile.lock&lt;br /&gt;
├── I704.gemspec&lt;br /&gt;
├── lib&lt;br /&gt;
│   ├── I704&lt;br /&gt;
│   │   ├── bank_account.rb&lt;br /&gt;
│   │   └── version.rb&lt;br /&gt;
│   └── I704.rb&lt;br /&gt;
├── LICENSE.txt&lt;br /&gt;
├── Rakefile&lt;br /&gt;
├── README.md&lt;br /&gt;
└── test&lt;br /&gt;
    ├── I704&lt;br /&gt;
    │   └── bank_account_test.rb&lt;br /&gt;
    ├── I704_test.rb&lt;br /&gt;
    └── test_helper.rb&lt;br /&gt;
&lt;br /&gt;
6 directories, 15 files&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* exectuables that users of your gem should be able to run go into the &amp;lt;code&amp;gt;exe&amp;lt;/code&amp;gt; directory&lt;br /&gt;
* add dependencies in your &amp;lt;code&amp;gt;.gemspec&amp;lt;/code&amp;gt; file.  After adding a dependency, run &amp;lt;code&amp;gt;bundle install&amp;lt;/code&amp;gt; to install the dependency on your computer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rule of thumb&#039;&#039;&#039;: put your code into the &amp;lt;code&amp;gt;lib/YOUR_PROJECT_NAME&amp;lt;/code&amp;gt; directory, one file per class (like &amp;lt;code&amp;gt;bank_account.rb&amp;lt;/code&amp;gt; in the example above)&lt;br /&gt;
&lt;br /&gt;
= 2017-03-02 Lecture &amp;amp; Lab =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Word count example&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Burning Rangers is a 1998 3D action video game developed by Sonic Team and published by Sega for the Sega Saturn. The game is set in a futuristic society threatened by frequent fires. Players control one of an elite group of firefighters, the Burning Rangers, who extinguish the fires and rescue civilians from burning buildings. Most of the tasks the players complete are centred around collecting energy crystals used to transport civilians to safety. Development began shortly after the release of Christmas Nights in November 1996, when Yuji Naka started working on a game focused on saving people rather than killing them. Sonic Team chose the themes of firefighting and heroism. Burning Rangers received mostly positive reviews, especially for the game&#039;s soundtrack and audio. Responses to the graphics were mixed; while some critics asserted that the game had the best visuals on the Saturn, others faulted its poor collision detection and occasional glitching. The game was among the final five Saturn titles released in America.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
words = text.split(&#039; &#039;)&lt;br /&gt;
&lt;br /&gt;
words_without_punctuation = words.map do |word|&lt;br /&gt;
  word.sub(/[.,;?!:]/, &#039;&#039;).downcase&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
word_count = words_without_punctuation.reduce({}) do |result, word|&lt;br /&gt;
  if result.key?(word)&lt;br /&gt;
    result[word] = result[word] + 1&lt;br /&gt;
    # also possible: result[word] += 1&lt;br /&gt;
  else&lt;br /&gt;
    result[word] = 1&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  result&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts word_count&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-03-09 lecture =&lt;br /&gt;
&lt;br /&gt;
== Using locks ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;thread&#039;&lt;br /&gt;
&lt;br /&gt;
# [1, 2, ..., 100]&lt;br /&gt;
# [t1, t2, ..., t100]&lt;br /&gt;
&lt;br /&gt;
# Lock / Mutex (mutual exclusion)&lt;br /&gt;
# Queue&lt;br /&gt;
&lt;br /&gt;
lock = Mutex.new&lt;br /&gt;
threads = (1..100).map do |number|&lt;br /&gt;
  Thread.new do&lt;br /&gt;
    sleep 0.5&lt;br /&gt;
&lt;br /&gt;
    lock.lock&lt;br /&gt;
    puts number&lt;br /&gt;
    lock.unlock&lt;br /&gt;
&lt;br /&gt;
    lock.synchronize do&lt;br /&gt;
      if number == 1&lt;br /&gt;
        sleep 5&lt;br /&gt;
      end&lt;br /&gt;
      puts number&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads.each do |thread|&lt;br /&gt;
  thread.join&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using a queue ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;thread&#039;&lt;br /&gt;
&lt;br /&gt;
# Queue&lt;br /&gt;
&lt;br /&gt;
queue = Queue.new&lt;br /&gt;
&lt;br /&gt;
printer = Thread.new do&lt;br /&gt;
  loop do&lt;br /&gt;
    number = queue.pop&lt;br /&gt;
    puts number&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads = (1..100).map do |number|&lt;br /&gt;
  Thread.new do&lt;br /&gt;
    sleep 0.5&lt;br /&gt;
    queue.push number&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads.each do |thread|&lt;br /&gt;
  thread.join&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using fork ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
shared_variable = &#039;untouched&#039;&lt;br /&gt;
child_process_id = fork do&lt;br /&gt;
  puts &amp;quot;Running inside fork&amp;quot;&lt;br /&gt;
  puts &amp;quot;My process ID is #{$$}&amp;quot;&lt;br /&gt;
  shared_variable = &#039;touched&#039;&lt;br /&gt;
end&lt;br /&gt;
Process.wait(child_process_id)&lt;br /&gt;
puts &amp;quot;In the parent #{$$}: shared_variable is #{shared_variable}&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: making HTTP requests in Ruby =&lt;br /&gt;
&lt;br /&gt;
# Require the &amp;quot;net/http&amp;quot; library in your program&lt;br /&gt;
# Use that library to make HTTP requests for fetching the HTML code behind a website&lt;br /&gt;
# 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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;net/http&#039;&lt;br /&gt;
google_html = Net::HTTP.get(URI(&#039;http://google.com&#039;))&lt;br /&gt;
# &amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=\&amp;quot;content-type\&amp;quot; content=\&amp;quot;text/html;charset=utf-8\&amp;quot;&amp;gt;\n&amp;lt;TITLE&amp;gt;302 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;\n&amp;lt;H1&amp;gt;302 Moved&amp;lt;/H1&amp;gt;\nThe document has moved\n&amp;lt;A HREF=\&amp;quot;http://www.google.ee/?gfe_rd=cr&amp;amp;amp;ei=o6KlWKCGA9Oq8wfoq5OQAg\&amp;quot;&amp;gt;here&amp;lt;/A&amp;gt;.\r\n&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;\r\n&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Now you can work with the HTML returned by the server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: parsing HTML using Nokogiri =&lt;br /&gt;
&lt;br /&gt;
Once you have received some HTML from a web server, you most likely want to analyze it.  Use the library nokogiri for this.  You can find installation instructions and tutorials here: http://www.nokogiri.org/tutorials/&lt;br /&gt;
&lt;br /&gt;
= Reference material: map and reduce =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# map: takes a list and a block&lt;br /&gt;
#    returns: a list, where each element of the original list has been&lt;br /&gt;
#    passed to block&lt;br /&gt;
#&lt;br /&gt;
# reduce: takes list, an initial value and a block&lt;br /&gt;
#    returns: block applied to initial value and each element of the list&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = numbers.reduce(0) do |current_sum, number|&lt;br /&gt;
  current_sum + number&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;Sum: #{sum}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
####&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = 0&lt;br /&gt;
numbers.each do |number|&lt;br /&gt;
  sum = sum + number&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118462</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118462"/>
		<updated>2017-03-09T11:26:36Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: /* = Answering 80% of the questions correctly */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= Lecture Recordings =&lt;br /&gt;
&lt;br /&gt;
You can find recordings of the lectures here:&lt;br /&gt;
&lt;br /&gt;
https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
* [https://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-select Ruby Enumerable]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for a having a README describing your project&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 10 points for structuring your project as a Rubygem&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
== Proper use of version control ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# you keep your code in a git repository&lt;br /&gt;
# a single commit represents a single logical step in the development process (e.g. adding a test and the code to make it pass, fixing a single bug, refactoring one area of the code, etc)&lt;br /&gt;
# most commit messages follow the guidelines for good commit messages (see reference material below)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://chris.beams.io/posts/git-commit/ How to write a Git Commit Message] (see &amp;quot;The Seven Rules&amp;quot;)&lt;br /&gt;
* [http://nuclearsquid.com/writings/git-add/ How to select a subset of all changes for your next commit]&lt;br /&gt;
&lt;br /&gt;
== Having a README ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# your project has a file called README.md in your project&#039;s root directory&lt;br /&gt;
# the README.md file contains a description of: what your program does, how to install it and how to run it&lt;br /&gt;
&lt;br /&gt;
== Adhering to common Ruby coding standards ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# running &amp;lt;code&amp;gt;rubocop&amp;lt;/code&amp;gt; with the default configuration in your project produces no errors&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [http://rubocop.readthedocs.io/en/latest/basic_usage/#basic-usage Rubocop Basic Usage].  &#039;&#039;&#039;Hint:&#039;&#039;&#039;  You can run Rubocop with the &amp;lt;code&amp;gt;--auto-correct&amp;lt;/code&amp;gt; option to automatically fix some of the problems Rubocop discovered.&lt;br /&gt;
&lt;br /&gt;
== Providing automated tests ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 20&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# running &amp;lt;code&amp;gt;bundle exec rake test&amp;lt;/code&amp;gt; runs your test suite&lt;br /&gt;
# your test suite has no failing tests&lt;br /&gt;
# your test suite contains at least one meaningful test, testing your program&#039;s logic&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://launchschool.com/blog/assert-yourself-an-introduction-to-minitest Introduction to MiniTest]&lt;br /&gt;
&lt;br /&gt;
== Structuring your project as a Rubygem ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# your project directory structure matches the structure created by &amp;lt;code&amp;gt;bundle gem NAME_OF_YOUR_PROJECT&amp;lt;/code&amp;gt;&lt;br /&gt;
# additional dependencies are specified in the &amp;lt;code&amp;gt;.gemspec&amp;lt;/code&amp;gt; file of your project&lt;br /&gt;
&lt;br /&gt;
If your project is using a framework with strong conventions, such as Ruby on Rails, these points are awarded if you place your code according to the conventions of the framework.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://code.tutsplus.com/tutorials/gem-creation-with-bundler--net-25281 Creating a gem with Bundler]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Answering 80% of the questions correctly ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 20&lt;br /&gt;
&lt;br /&gt;
You will be asked 3 to 5 questions about your project, testing your understanding of what is happening in the code.&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# you answer 80% or more of the questions correctly&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  class Error &amp;lt; StandardError; end&lt;br /&gt;
  class BalanceNegative &amp;lt; Error; end&lt;br /&gt;
&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increse_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &#039;Not enough money!&#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increse_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def setup&lt;br /&gt;
    @account = BankAccount.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_decreses_the_balance_when_withdrawing_money&lt;br /&gt;
    @account.deposit!(101).withdraw!(50)&lt;br /&gt;
    assert_equal 51, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_increses_the_balance_when_depositing_money&lt;br /&gt;
    @account.deposit!(196583)&lt;br /&gt;
    assert_equal 196583, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_account_does_not_go_in_to_negative&lt;br /&gt;
    assert_raises BankAccount::BalanceNegative do&lt;br /&gt;
      @account.deposit!(1).withdraw!(20)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_it_raises_bank_account_error_when_withdrawing_too_much_money&lt;br /&gt;
    @account.deposit!(1).withdraw!(20)&lt;br /&gt;
  rescue BankAccount::Error =&amp;gt; err&lt;br /&gt;
    assert true, &#039;everything is ok&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-16 Lecture and Lab: using modules and bundler =&lt;br /&gt;
&lt;br /&gt;
== Modules in Ruby ==&lt;br /&gt;
&lt;br /&gt;
* Modules are a collection of methods that can be included in another class&lt;br /&gt;
* By including the module in a class, objects belonging to that class get access to the methods defined in the module&lt;br /&gt;
* If multiple included modules define the same method, the method from the module that was included last counts&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
module ModuleExample&lt;br /&gt;
  def foo&lt;br /&gt;
    &#039;foo&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Including modules binds their methods to the class instances&lt;br /&gt;
# Extending modules binds their methods to the class itself&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  include ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Book&lt;br /&gt;
  extend ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Person.foo     # =&amp;gt; NoMethodError: undefined method `foo&#039; for Person:Class&lt;br /&gt;
Person.new.foo # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.foo       # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.new.foo   # =&amp;gt; NoMethodError: undefined method `foo&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
== Structuring a project with bundler ==&lt;br /&gt;
&lt;br /&gt;
* bundler is a Ruby program for managing dependencies in your ruby project&lt;br /&gt;
* bundler also helps you with creating a template for a project&lt;br /&gt;
* redistributable projects/libraries are called &amp;quot;gems&amp;quot; in ruby&lt;br /&gt;
* You can start your own project by running &amp;lt;code&amp;gt;bundle gem YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt;.  Replace &amp;lt;code&amp;gt;YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt; with the lower-cased name of your project.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example structure of a project, as generated by bundler&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ tree&lt;br /&gt;
.&lt;br /&gt;
├── bin&lt;br /&gt;
│   ├── console&lt;br /&gt;
│   └── setup&lt;br /&gt;
├── exe&lt;br /&gt;
│   └── hello-world&lt;br /&gt;
├── Gemfile&lt;br /&gt;
├── Gemfile.lock&lt;br /&gt;
├── I704.gemspec&lt;br /&gt;
├── lib&lt;br /&gt;
│   ├── I704&lt;br /&gt;
│   │   ├── bank_account.rb&lt;br /&gt;
│   │   └── version.rb&lt;br /&gt;
│   └── I704.rb&lt;br /&gt;
├── LICENSE.txt&lt;br /&gt;
├── Rakefile&lt;br /&gt;
├── README.md&lt;br /&gt;
└── test&lt;br /&gt;
    ├── I704&lt;br /&gt;
    │   └── bank_account_test.rb&lt;br /&gt;
    ├── I704_test.rb&lt;br /&gt;
    └── test_helper.rb&lt;br /&gt;
&lt;br /&gt;
6 directories, 15 files&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* exectuables that users of your gem should be able to run go into the &amp;lt;code&amp;gt;exe&amp;lt;/code&amp;gt; directory&lt;br /&gt;
* add dependencies in your &amp;lt;code&amp;gt;.gemspec&amp;lt;/code&amp;gt; file.  After adding a dependency, run &amp;lt;code&amp;gt;bundle install&amp;lt;/code&amp;gt; to install the dependency on your computer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rule of thumb&#039;&#039;&#039;: put your code into the &amp;lt;code&amp;gt;lib/YOUR_PROJECT_NAME&amp;lt;/code&amp;gt; directory, one file per class (like &amp;lt;code&amp;gt;bank_account.rb&amp;lt;/code&amp;gt; in the example above)&lt;br /&gt;
&lt;br /&gt;
= 2017-03-02 Lecture &amp;amp; Lab =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Word count example&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Burning Rangers is a 1998 3D action video game developed by Sonic Team and published by Sega for the Sega Saturn. The game is set in a futuristic society threatened by frequent fires. Players control one of an elite group of firefighters, the Burning Rangers, who extinguish the fires and rescue civilians from burning buildings. Most of the tasks the players complete are centred around collecting energy crystals used to transport civilians to safety. Development began shortly after the release of Christmas Nights in November 1996, when Yuji Naka started working on a game focused on saving people rather than killing them. Sonic Team chose the themes of firefighting and heroism. Burning Rangers received mostly positive reviews, especially for the game&#039;s soundtrack and audio. Responses to the graphics were mixed; while some critics asserted that the game had the best visuals on the Saturn, others faulted its poor collision detection and occasional glitching. The game was among the final five Saturn titles released in America.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
words = text.split(&#039; &#039;)&lt;br /&gt;
&lt;br /&gt;
words_without_punctuation = words.map do |word|&lt;br /&gt;
  word.sub(/[.,;?!:]/, &#039;&#039;).downcase&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
word_count = words_without_punctuation.reduce({}) do |result, word|&lt;br /&gt;
  if result.key?(word)&lt;br /&gt;
    result[word] = result[word] + 1&lt;br /&gt;
    # also possible: result[word] += 1&lt;br /&gt;
  else&lt;br /&gt;
    result[word] = 1&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  result&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts word_count&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-03-09 lecture =&lt;br /&gt;
&lt;br /&gt;
== Using locks ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;thread&#039;&lt;br /&gt;
&lt;br /&gt;
# [1, 2, ..., 100]&lt;br /&gt;
# [t1, t2, ..., t100]&lt;br /&gt;
&lt;br /&gt;
# Lock / Mutex (mutual exclusion)&lt;br /&gt;
# Queue&lt;br /&gt;
&lt;br /&gt;
lock = Mutex.new&lt;br /&gt;
threads = (1..100).map do |number|&lt;br /&gt;
  Thread.new do&lt;br /&gt;
    sleep 0.5&lt;br /&gt;
&lt;br /&gt;
    lock.lock&lt;br /&gt;
    puts number&lt;br /&gt;
    lock.unlock&lt;br /&gt;
&lt;br /&gt;
    lock.synchronize do&lt;br /&gt;
      if number == 1&lt;br /&gt;
        sleep 5&lt;br /&gt;
      end&lt;br /&gt;
      puts number&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads.each do |thread|&lt;br /&gt;
  thread.join&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using a queue ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;thread&#039;&lt;br /&gt;
&lt;br /&gt;
# Queue&lt;br /&gt;
&lt;br /&gt;
queue = Queue.new&lt;br /&gt;
&lt;br /&gt;
printer = Thread.new do&lt;br /&gt;
  loop do&lt;br /&gt;
    number = queue.pop&lt;br /&gt;
    puts number&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads = (1..100).map do |number|&lt;br /&gt;
  Thread.new do&lt;br /&gt;
    sleep 0.5&lt;br /&gt;
    queue.push number&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads.each do |thread|&lt;br /&gt;
  thread.join&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using fork ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
shared_variable = &#039;untouched&#039;&lt;br /&gt;
child_process_id = fork do&lt;br /&gt;
  puts &amp;quot;Running inside fork&amp;quot;&lt;br /&gt;
  puts &amp;quot;My process ID is #{$$}&amp;quot;&lt;br /&gt;
  shared_variable = &#039;touched&#039;&lt;br /&gt;
end&lt;br /&gt;
Process.wait(child_process_id)&lt;br /&gt;
puts &amp;quot;In the parent #{$$}: shared_variable is #{shared_variable}&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: making HTTP requests in Ruby =&lt;br /&gt;
&lt;br /&gt;
# Require the &amp;quot;net/http&amp;quot; library in your program&lt;br /&gt;
# Use that library to make HTTP requests for fetching the HTML code behind a website&lt;br /&gt;
# 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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;net/http&#039;&lt;br /&gt;
google_html = Net::HTTP.get(URI(&#039;http://google.com&#039;))&lt;br /&gt;
# &amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=\&amp;quot;content-type\&amp;quot; content=\&amp;quot;text/html;charset=utf-8\&amp;quot;&amp;gt;\n&amp;lt;TITLE&amp;gt;302 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;\n&amp;lt;H1&amp;gt;302 Moved&amp;lt;/H1&amp;gt;\nThe document has moved\n&amp;lt;A HREF=\&amp;quot;http://www.google.ee/?gfe_rd=cr&amp;amp;amp;ei=o6KlWKCGA9Oq8wfoq5OQAg\&amp;quot;&amp;gt;here&amp;lt;/A&amp;gt;.\r\n&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;\r\n&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Now you can work with the HTML returned by the server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: parsing HTML using Nokogiri =&lt;br /&gt;
&lt;br /&gt;
Once you have received some HTML from a web server, you most likely want to analyze it.  Use the library nokogiri for this.  You can find installation instructions and tutorials here: http://www.nokogiri.org/tutorials/&lt;br /&gt;
&lt;br /&gt;
= Reference material: map and reduce =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# map: takes a list and a block&lt;br /&gt;
#    returns: a list, where each element of the original list has been&lt;br /&gt;
#    passed to block&lt;br /&gt;
#&lt;br /&gt;
# reduce: takes list, an initial value and a block&lt;br /&gt;
#    returns: block applied to initial value and each element of the list&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = numbers.reduce(0) do |current_sum, number|&lt;br /&gt;
  current_sum + number&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;Sum: #{sum}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
####&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = 0&lt;br /&gt;
numbers.each do |number|&lt;br /&gt;
  sum = sum + number&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118461</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118461"/>
		<updated>2017-03-09T10:56:09Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: /* 2017-03-09 lecture */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= Lecture Recordings =&lt;br /&gt;
&lt;br /&gt;
You can find recordings of the lectures here:&lt;br /&gt;
&lt;br /&gt;
https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
* [https://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-select Ruby Enumerable]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for a having a README describing your project&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 10 points for structuring your project as a Rubygem&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
== Proper use of version control ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# you keep your code in a git repository&lt;br /&gt;
# a single commit represents a single logical step in the development process (e.g. adding a test and the code to make it pass, fixing a single bug, refactoring one area of the code, etc)&lt;br /&gt;
# most commit messages follow the guidelines for good commit messages (see reference material below)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://chris.beams.io/posts/git-commit/ How to write a Git Commit Message] (see &amp;quot;The Seven Rules&amp;quot;)&lt;br /&gt;
* [http://nuclearsquid.com/writings/git-add/ How to select a subset of all changes for your next commit]&lt;br /&gt;
&lt;br /&gt;
== Having a README ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# your project has a file called README.md in your project&#039;s root directory&lt;br /&gt;
# the README.md file contains a description of: what your program does, how to install it and how to run it&lt;br /&gt;
&lt;br /&gt;
== Adhering to common Ruby coding standards ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# running &amp;lt;code&amp;gt;rubocop&amp;lt;/code&amp;gt; with the default configuration in your project produces no errors&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [http://rubocop.readthedocs.io/en/latest/basic_usage/#basic-usage Rubocop Basic Usage].  &#039;&#039;&#039;Hint:&#039;&#039;&#039;  You can run Rubocop with the &amp;lt;code&amp;gt;--auto-correct&amp;lt;/code&amp;gt; option to automatically fix some of the problems Rubocop discovered.&lt;br /&gt;
&lt;br /&gt;
== Providing automated tests ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 20&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# running &amp;lt;code&amp;gt;bundle exec rake test&amp;lt;/code&amp;gt; runs your test suite&lt;br /&gt;
# your test suite has no failing tests&lt;br /&gt;
# your test suite contains at least one meaningful test, testing your program&#039;s logic&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://launchschool.com/blog/assert-yourself-an-introduction-to-minitest Introduction to MiniTest]&lt;br /&gt;
&lt;br /&gt;
== Structuring your project as a Rubygem ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# your project directory structure matches the structure created by &amp;lt;code&amp;gt;bundle gem NAME_OF_YOUR_PROJECT&amp;lt;/code&amp;gt;&lt;br /&gt;
# additional dependencies are specified in the &amp;lt;code&amp;gt;.gemspec&amp;lt;/code&amp;gt; file of your project&lt;br /&gt;
&lt;br /&gt;
If your project is using a framework with strong conventions, such as Ruby on Rails, these points are awarded if you place your code according to the conventions of the framework.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://code.tutsplus.com/tutorials/gem-creation-with-bundler--net-25281 Creating a gem with Bundler]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Answering 80% of the questions correctly ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 20&lt;br /&gt;
&lt;br /&gt;
You will be asked 3 to 5 questions about your project, testing your understanding of what is happening in the code.&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# you answer 80% or more of the questions correctly&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  class Error &amp;lt; StandardError; end&lt;br /&gt;
  class BalanceNegative &amp;lt; Error; end&lt;br /&gt;
&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increse_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &#039;Not enough money!&#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increse_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def setup&lt;br /&gt;
    @account = BankAccount.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_decreses_the_balance_when_withdrawing_money&lt;br /&gt;
    @account.deposit!(101).withdraw!(50)&lt;br /&gt;
    assert_equal 51, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_increses_the_balance_when_depositing_money&lt;br /&gt;
    @account.deposit!(196583)&lt;br /&gt;
    assert_equal 196583, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_account_does_not_go_in_to_negative&lt;br /&gt;
    assert_raises BankAccount::BalanceNegative do&lt;br /&gt;
      @account.deposit!(1).withdraw!(20)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_it_raises_bank_account_error_when_withdrawing_too_much_money&lt;br /&gt;
    @account.deposit!(1).withdraw!(20)&lt;br /&gt;
  rescue BankAccount::Error =&amp;gt; err&lt;br /&gt;
    assert true, &#039;everything is ok&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-16 Lecture and Lab: using modules and bundler =&lt;br /&gt;
&lt;br /&gt;
== Modules in Ruby ==&lt;br /&gt;
&lt;br /&gt;
* Modules are a collection of methods that can be included in another class&lt;br /&gt;
* By including the module in a class, objects belonging to that class get access to the methods defined in the module&lt;br /&gt;
* If multiple included modules define the same method, the method from the module that was included last counts&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
module ModuleExample&lt;br /&gt;
  def foo&lt;br /&gt;
    &#039;foo&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Including modules binds their methods to the class instances&lt;br /&gt;
# Extending modules binds their methods to the class itself&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  include ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Book&lt;br /&gt;
  extend ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Person.foo     # =&amp;gt; NoMethodError: undefined method `foo&#039; for Person:Class&lt;br /&gt;
Person.new.foo # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.foo       # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.new.foo   # =&amp;gt; NoMethodError: undefined method `foo&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
== Structuring a project with bundler ==&lt;br /&gt;
&lt;br /&gt;
* bundler is a Ruby program for managing dependencies in your ruby project&lt;br /&gt;
* bundler also helps you with creating a template for a project&lt;br /&gt;
* redistributable projects/libraries are called &amp;quot;gems&amp;quot; in ruby&lt;br /&gt;
* You can start your own project by running &amp;lt;code&amp;gt;bundle gem YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt;.  Replace &amp;lt;code&amp;gt;YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt; with the lower-cased name of your project.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example structure of a project, as generated by bundler&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ tree&lt;br /&gt;
.&lt;br /&gt;
├── bin&lt;br /&gt;
│   ├── console&lt;br /&gt;
│   └── setup&lt;br /&gt;
├── exe&lt;br /&gt;
│   └── hello-world&lt;br /&gt;
├── Gemfile&lt;br /&gt;
├── Gemfile.lock&lt;br /&gt;
├── I704.gemspec&lt;br /&gt;
├── lib&lt;br /&gt;
│   ├── I704&lt;br /&gt;
│   │   ├── bank_account.rb&lt;br /&gt;
│   │   └── version.rb&lt;br /&gt;
│   └── I704.rb&lt;br /&gt;
├── LICENSE.txt&lt;br /&gt;
├── Rakefile&lt;br /&gt;
├── README.md&lt;br /&gt;
└── test&lt;br /&gt;
    ├── I704&lt;br /&gt;
    │   └── bank_account_test.rb&lt;br /&gt;
    ├── I704_test.rb&lt;br /&gt;
    └── test_helper.rb&lt;br /&gt;
&lt;br /&gt;
6 directories, 15 files&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* exectuables that users of your gem should be able to run go into the &amp;lt;code&amp;gt;exe&amp;lt;/code&amp;gt; directory&lt;br /&gt;
* add dependencies in your &amp;lt;code&amp;gt;.gemspec&amp;lt;/code&amp;gt; file.  After adding a dependency, run &amp;lt;code&amp;gt;bundle install&amp;lt;/code&amp;gt; to install the dependency on your computer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rule of thumb&#039;&#039;&#039;: put your code into the &amp;lt;code&amp;gt;lib/YOUR_PROJECT_NAME&amp;lt;/code&amp;gt; directory, one file per class (like &amp;lt;code&amp;gt;bank_account.rb&amp;lt;/code&amp;gt; in the example above)&lt;br /&gt;
&lt;br /&gt;
= 2017-03-02 Lecture &amp;amp; Lab =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Word count example&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Burning Rangers is a 1998 3D action video game developed by Sonic Team and published by Sega for the Sega Saturn. The game is set in a futuristic society threatened by frequent fires. Players control one of an elite group of firefighters, the Burning Rangers, who extinguish the fires and rescue civilians from burning buildings. Most of the tasks the players complete are centred around collecting energy crystals used to transport civilians to safety. Development began shortly after the release of Christmas Nights in November 1996, when Yuji Naka started working on a game focused on saving people rather than killing them. Sonic Team chose the themes of firefighting and heroism. Burning Rangers received mostly positive reviews, especially for the game&#039;s soundtrack and audio. Responses to the graphics were mixed; while some critics asserted that the game had the best visuals on the Saturn, others faulted its poor collision detection and occasional glitching. The game was among the final five Saturn titles released in America.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
words = text.split(&#039; &#039;)&lt;br /&gt;
&lt;br /&gt;
words_without_punctuation = words.map do |word|&lt;br /&gt;
  word.sub(/[.,;?!:]/, &#039;&#039;).downcase&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
word_count = words_without_punctuation.reduce({}) do |result, word|&lt;br /&gt;
  if result.key?(word)&lt;br /&gt;
    result[word] = result[word] + 1&lt;br /&gt;
    # also possible: result[word] += 1&lt;br /&gt;
  else&lt;br /&gt;
    result[word] = 1&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  result&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts word_count&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-03-09 lecture =&lt;br /&gt;
&lt;br /&gt;
== Using locks ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;thread&#039;&lt;br /&gt;
&lt;br /&gt;
# [1, 2, ..., 100]&lt;br /&gt;
# [t1, t2, ..., t100]&lt;br /&gt;
&lt;br /&gt;
# Lock / Mutex (mutual exclusion)&lt;br /&gt;
# Queue&lt;br /&gt;
&lt;br /&gt;
lock = Mutex.new&lt;br /&gt;
threads = (1..100).map do |number|&lt;br /&gt;
  Thread.new do&lt;br /&gt;
    sleep 0.5&lt;br /&gt;
&lt;br /&gt;
    lock.lock&lt;br /&gt;
    puts number&lt;br /&gt;
    lock.unlock&lt;br /&gt;
&lt;br /&gt;
    lock.synchronize do&lt;br /&gt;
      if number == 1&lt;br /&gt;
        sleep 5&lt;br /&gt;
      end&lt;br /&gt;
      puts number&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads.each do |thread|&lt;br /&gt;
  thread.join&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using a queue ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;thread&#039;&lt;br /&gt;
&lt;br /&gt;
# Queue&lt;br /&gt;
&lt;br /&gt;
queue = Queue.new&lt;br /&gt;
&lt;br /&gt;
printer = Thread.new do&lt;br /&gt;
  loop do&lt;br /&gt;
    number = queue.pop&lt;br /&gt;
    puts number&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads = (1..100).map do |number|&lt;br /&gt;
  Thread.new do&lt;br /&gt;
    sleep 0.5&lt;br /&gt;
    queue.push number&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads.each do |thread|&lt;br /&gt;
  thread.join&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using fork ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
shared_variable = &#039;untouched&#039;&lt;br /&gt;
child_process_id = fork do&lt;br /&gt;
  puts &amp;quot;Running inside fork&amp;quot;&lt;br /&gt;
  puts &amp;quot;My process ID is #{$$}&amp;quot;&lt;br /&gt;
  shared_variable = &#039;touched&#039;&lt;br /&gt;
end&lt;br /&gt;
Process.wait(child_process_id)&lt;br /&gt;
puts &amp;quot;In the parent #{$$}: shared_variable is #{shared_variable}&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: making HTTP requests in Ruby =&lt;br /&gt;
&lt;br /&gt;
# Require the &amp;quot;net/http&amp;quot; library in your program&lt;br /&gt;
# Use that library to make HTTP requests for fetching the HTML code behind a website&lt;br /&gt;
# 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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;net/http&#039;&lt;br /&gt;
google_html = Net::HTTP.get(URI(&#039;http://google.com&#039;))&lt;br /&gt;
# &amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=\&amp;quot;content-type\&amp;quot; content=\&amp;quot;text/html;charset=utf-8\&amp;quot;&amp;gt;\n&amp;lt;TITLE&amp;gt;302 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;\n&amp;lt;H1&amp;gt;302 Moved&amp;lt;/H1&amp;gt;\nThe document has moved\n&amp;lt;A HREF=\&amp;quot;http://www.google.ee/?gfe_rd=cr&amp;amp;amp;ei=o6KlWKCGA9Oq8wfoq5OQAg\&amp;quot;&amp;gt;here&amp;lt;/A&amp;gt;.\r\n&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;\r\n&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Now you can work with the HTML returned by the server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: parsing HTML using Nokogiri =&lt;br /&gt;
&lt;br /&gt;
Once you have received some HTML from a web server, you most likely want to analyze it.  Use the library nokogiri for this.  You can find installation instructions and tutorials here: http://www.nokogiri.org/tutorials/&lt;br /&gt;
&lt;br /&gt;
= Reference material: map and reduce =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# map: takes a list and a block&lt;br /&gt;
#    returns: a list, where each element of the original list has been&lt;br /&gt;
#    passed to block&lt;br /&gt;
#&lt;br /&gt;
# reduce: takes list, an initial value and a block&lt;br /&gt;
#    returns: block applied to initial value and each element of the list&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = numbers.reduce(0) do |current_sum, number|&lt;br /&gt;
  current_sum + number&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;Sum: #{sum}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
####&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = 0&lt;br /&gt;
numbers.each do |number|&lt;br /&gt;
  sum = sum + number&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118460</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118460"/>
		<updated>2017-03-09T10:45:20Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: /* 2017-03-09 lecture */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= Lecture Recordings =&lt;br /&gt;
&lt;br /&gt;
You can find recordings of the lectures here:&lt;br /&gt;
&lt;br /&gt;
https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
* [https://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-select Ruby Enumerable]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for a having a README describing your project&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 10 points for structuring your project as a Rubygem&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
== Proper use of version control ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# you keep your code in a git repository&lt;br /&gt;
# a single commit represents a single logical step in the development process (e.g. adding a test and the code to make it pass, fixing a single bug, refactoring one area of the code, etc)&lt;br /&gt;
# most commit messages follow the guidelines for good commit messages (see reference material below)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://chris.beams.io/posts/git-commit/ How to write a Git Commit Message] (see &amp;quot;The Seven Rules&amp;quot;)&lt;br /&gt;
* [http://nuclearsquid.com/writings/git-add/ How to select a subset of all changes for your next commit]&lt;br /&gt;
&lt;br /&gt;
== Having a README ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# your project has a file called README.md in your project&#039;s root directory&lt;br /&gt;
# the README.md file contains a description of: what your program does, how to install it and how to run it&lt;br /&gt;
&lt;br /&gt;
== Adhering to common Ruby coding standards ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# running &amp;lt;code&amp;gt;rubocop&amp;lt;/code&amp;gt; with the default configuration in your project produces no errors&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [http://rubocop.readthedocs.io/en/latest/basic_usage/#basic-usage Rubocop Basic Usage].  &#039;&#039;&#039;Hint:&#039;&#039;&#039;  You can run Rubocop with the &amp;lt;code&amp;gt;--auto-correct&amp;lt;/code&amp;gt; option to automatically fix some of the problems Rubocop discovered.&lt;br /&gt;
&lt;br /&gt;
== Providing automated tests ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 20&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# running &amp;lt;code&amp;gt;bundle exec rake test&amp;lt;/code&amp;gt; runs your test suite&lt;br /&gt;
# your test suite has no failing tests&lt;br /&gt;
# your test suite contains at least one meaningful test, testing your program&#039;s logic&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://launchschool.com/blog/assert-yourself-an-introduction-to-minitest Introduction to MiniTest]&lt;br /&gt;
&lt;br /&gt;
== Structuring your project as a Rubygem ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# your project directory structure matches the structure created by &amp;lt;code&amp;gt;bundle gem NAME_OF_YOUR_PROJECT&amp;lt;/code&amp;gt;&lt;br /&gt;
# additional dependencies are specified in the &amp;lt;code&amp;gt;.gemspec&amp;lt;/code&amp;gt; file of your project&lt;br /&gt;
&lt;br /&gt;
If your project is using a framework with strong conventions, such as Ruby on Rails, these points are awarded if you place your code according to the conventions of the framework.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://code.tutsplus.com/tutorials/gem-creation-with-bundler--net-25281 Creating a gem with Bundler]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Answering 80% of the questions correctly ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 20&lt;br /&gt;
&lt;br /&gt;
You will be asked 3 to 5 questions about your project, testing your understanding of what is happening in the code.&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# you answer 80% or more of the questions correctly&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  class Error &amp;lt; StandardError; end&lt;br /&gt;
  class BalanceNegative &amp;lt; Error; end&lt;br /&gt;
&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increse_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &#039;Not enough money!&#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increse_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def setup&lt;br /&gt;
    @account = BankAccount.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_decreses_the_balance_when_withdrawing_money&lt;br /&gt;
    @account.deposit!(101).withdraw!(50)&lt;br /&gt;
    assert_equal 51, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_increses_the_balance_when_depositing_money&lt;br /&gt;
    @account.deposit!(196583)&lt;br /&gt;
    assert_equal 196583, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_account_does_not_go_in_to_negative&lt;br /&gt;
    assert_raises BankAccount::BalanceNegative do&lt;br /&gt;
      @account.deposit!(1).withdraw!(20)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_it_raises_bank_account_error_when_withdrawing_too_much_money&lt;br /&gt;
    @account.deposit!(1).withdraw!(20)&lt;br /&gt;
  rescue BankAccount::Error =&amp;gt; err&lt;br /&gt;
    assert true, &#039;everything is ok&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-16 Lecture and Lab: using modules and bundler =&lt;br /&gt;
&lt;br /&gt;
== Modules in Ruby ==&lt;br /&gt;
&lt;br /&gt;
* Modules are a collection of methods that can be included in another class&lt;br /&gt;
* By including the module in a class, objects belonging to that class get access to the methods defined in the module&lt;br /&gt;
* If multiple included modules define the same method, the method from the module that was included last counts&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
module ModuleExample&lt;br /&gt;
  def foo&lt;br /&gt;
    &#039;foo&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Including modules binds their methods to the class instances&lt;br /&gt;
# Extending modules binds their methods to the class itself&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  include ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Book&lt;br /&gt;
  extend ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Person.foo     # =&amp;gt; NoMethodError: undefined method `foo&#039; for Person:Class&lt;br /&gt;
Person.new.foo # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.foo       # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.new.foo   # =&amp;gt; NoMethodError: undefined method `foo&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
== Structuring a project with bundler ==&lt;br /&gt;
&lt;br /&gt;
* bundler is a Ruby program for managing dependencies in your ruby project&lt;br /&gt;
* bundler also helps you with creating a template for a project&lt;br /&gt;
* redistributable projects/libraries are called &amp;quot;gems&amp;quot; in ruby&lt;br /&gt;
* You can start your own project by running &amp;lt;code&amp;gt;bundle gem YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt;.  Replace &amp;lt;code&amp;gt;YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt; with the lower-cased name of your project.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example structure of a project, as generated by bundler&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ tree&lt;br /&gt;
.&lt;br /&gt;
├── bin&lt;br /&gt;
│   ├── console&lt;br /&gt;
│   └── setup&lt;br /&gt;
├── exe&lt;br /&gt;
│   └── hello-world&lt;br /&gt;
├── Gemfile&lt;br /&gt;
├── Gemfile.lock&lt;br /&gt;
├── I704.gemspec&lt;br /&gt;
├── lib&lt;br /&gt;
│   ├── I704&lt;br /&gt;
│   │   ├── bank_account.rb&lt;br /&gt;
│   │   └── version.rb&lt;br /&gt;
│   └── I704.rb&lt;br /&gt;
├── LICENSE.txt&lt;br /&gt;
├── Rakefile&lt;br /&gt;
├── README.md&lt;br /&gt;
└── test&lt;br /&gt;
    ├── I704&lt;br /&gt;
    │   └── bank_account_test.rb&lt;br /&gt;
    ├── I704_test.rb&lt;br /&gt;
    └── test_helper.rb&lt;br /&gt;
&lt;br /&gt;
6 directories, 15 files&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* exectuables that users of your gem should be able to run go into the &amp;lt;code&amp;gt;exe&amp;lt;/code&amp;gt; directory&lt;br /&gt;
* add dependencies in your &amp;lt;code&amp;gt;.gemspec&amp;lt;/code&amp;gt; file.  After adding a dependency, run &amp;lt;code&amp;gt;bundle install&amp;lt;/code&amp;gt; to install the dependency on your computer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rule of thumb&#039;&#039;&#039;: put your code into the &amp;lt;code&amp;gt;lib/YOUR_PROJECT_NAME&amp;lt;/code&amp;gt; directory, one file per class (like &amp;lt;code&amp;gt;bank_account.rb&amp;lt;/code&amp;gt; in the example above)&lt;br /&gt;
&lt;br /&gt;
= 2017-03-02 Lecture &amp;amp; Lab =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Word count example&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Burning Rangers is a 1998 3D action video game developed by Sonic Team and published by Sega for the Sega Saturn. The game is set in a futuristic society threatened by frequent fires. Players control one of an elite group of firefighters, the Burning Rangers, who extinguish the fires and rescue civilians from burning buildings. Most of the tasks the players complete are centred around collecting energy crystals used to transport civilians to safety. Development began shortly after the release of Christmas Nights in November 1996, when Yuji Naka started working on a game focused on saving people rather than killing them. Sonic Team chose the themes of firefighting and heroism. Burning Rangers received mostly positive reviews, especially for the game&#039;s soundtrack and audio. Responses to the graphics were mixed; while some critics asserted that the game had the best visuals on the Saturn, others faulted its poor collision detection and occasional glitching. The game was among the final five Saturn titles released in America.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
words = text.split(&#039; &#039;)&lt;br /&gt;
&lt;br /&gt;
words_without_punctuation = words.map do |word|&lt;br /&gt;
  word.sub(/[.,;?!:]/, &#039;&#039;).downcase&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
word_count = words_without_punctuation.reduce({}) do |result, word|&lt;br /&gt;
  if result.key?(word)&lt;br /&gt;
    result[word] = result[word] + 1&lt;br /&gt;
    # also possible: result[word] += 1&lt;br /&gt;
  else&lt;br /&gt;
    result[word] = 1&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  result&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts word_count&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-03-09 lecture =&lt;br /&gt;
&lt;br /&gt;
== Using locks ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;thread&#039;&lt;br /&gt;
&lt;br /&gt;
# [1, 2, ..., 100]&lt;br /&gt;
# [t1, t2, ..., t100]&lt;br /&gt;
&lt;br /&gt;
# Lock / Mutex (mutual exclusion)&lt;br /&gt;
# Queue&lt;br /&gt;
&lt;br /&gt;
lock = Mutex.new&lt;br /&gt;
threads = (1..100).map do |number|&lt;br /&gt;
  Thread.new do&lt;br /&gt;
    sleep 0.5&lt;br /&gt;
&lt;br /&gt;
    lock.lock&lt;br /&gt;
    puts number&lt;br /&gt;
    lock.unlock&lt;br /&gt;
&lt;br /&gt;
    lock.synchronize do&lt;br /&gt;
      if number == 1&lt;br /&gt;
        sleep 5&lt;br /&gt;
      end&lt;br /&gt;
      puts number&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads.each do |thread|&lt;br /&gt;
  thread.join&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using a queue ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;thread&#039;&lt;br /&gt;
&lt;br /&gt;
# Queue&lt;br /&gt;
&lt;br /&gt;
queue = Queue.new&lt;br /&gt;
&lt;br /&gt;
printer = Thread.new do&lt;br /&gt;
  loop do&lt;br /&gt;
    number = queue.pop&lt;br /&gt;
    puts number&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads = (1..100).map do |number|&lt;br /&gt;
  Thread.new do&lt;br /&gt;
    sleep 0.5&lt;br /&gt;
    queue.push number&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads.each do |thread|&lt;br /&gt;
  thread.join&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: making HTTP requests in Ruby =&lt;br /&gt;
&lt;br /&gt;
# Require the &amp;quot;net/http&amp;quot; library in your program&lt;br /&gt;
# Use that library to make HTTP requests for fetching the HTML code behind a website&lt;br /&gt;
# 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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;net/http&#039;&lt;br /&gt;
google_html = Net::HTTP.get(URI(&#039;http://google.com&#039;))&lt;br /&gt;
# &amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=\&amp;quot;content-type\&amp;quot; content=\&amp;quot;text/html;charset=utf-8\&amp;quot;&amp;gt;\n&amp;lt;TITLE&amp;gt;302 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;\n&amp;lt;H1&amp;gt;302 Moved&amp;lt;/H1&amp;gt;\nThe document has moved\n&amp;lt;A HREF=\&amp;quot;http://www.google.ee/?gfe_rd=cr&amp;amp;amp;ei=o6KlWKCGA9Oq8wfoq5OQAg\&amp;quot;&amp;gt;here&amp;lt;/A&amp;gt;.\r\n&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;\r\n&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Now you can work with the HTML returned by the server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: parsing HTML using Nokogiri =&lt;br /&gt;
&lt;br /&gt;
Once you have received some HTML from a web server, you most likely want to analyze it.  Use the library nokogiri for this.  You can find installation instructions and tutorials here: http://www.nokogiri.org/tutorials/&lt;br /&gt;
&lt;br /&gt;
= Reference material: map and reduce =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# map: takes a list and a block&lt;br /&gt;
#    returns: a list, where each element of the original list has been&lt;br /&gt;
#    passed to block&lt;br /&gt;
#&lt;br /&gt;
# reduce: takes list, an initial value and a block&lt;br /&gt;
#    returns: block applied to initial value and each element of the list&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = numbers.reduce(0) do |current_sum, number|&lt;br /&gt;
  current_sum + number&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;Sum: #{sum}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
####&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = 0&lt;br /&gt;
numbers.each do |number|&lt;br /&gt;
  sum = sum + number&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118459</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118459"/>
		<updated>2017-03-09T10:31:03Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= Lecture Recordings =&lt;br /&gt;
&lt;br /&gt;
You can find recordings of the lectures here:&lt;br /&gt;
&lt;br /&gt;
https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
* [https://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-select Ruby Enumerable]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for a having a README describing your project&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 10 points for structuring your project as a Rubygem&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
== Proper use of version control ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# you keep your code in a git repository&lt;br /&gt;
# a single commit represents a single logical step in the development process (e.g. adding a test and the code to make it pass, fixing a single bug, refactoring one area of the code, etc)&lt;br /&gt;
# most commit messages follow the guidelines for good commit messages (see reference material below)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://chris.beams.io/posts/git-commit/ How to write a Git Commit Message] (see &amp;quot;The Seven Rules&amp;quot;)&lt;br /&gt;
* [http://nuclearsquid.com/writings/git-add/ How to select a subset of all changes for your next commit]&lt;br /&gt;
&lt;br /&gt;
== Having a README ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# your project has a file called README.md in your project&#039;s root directory&lt;br /&gt;
# the README.md file contains a description of: what your program does, how to install it and how to run it&lt;br /&gt;
&lt;br /&gt;
== Adhering to common Ruby coding standards ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# running &amp;lt;code&amp;gt;rubocop&amp;lt;/code&amp;gt; with the default configuration in your project produces no errors&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [http://rubocop.readthedocs.io/en/latest/basic_usage/#basic-usage Rubocop Basic Usage].  &#039;&#039;&#039;Hint:&#039;&#039;&#039;  You can run Rubocop with the &amp;lt;code&amp;gt;--auto-correct&amp;lt;/code&amp;gt; option to automatically fix some of the problems Rubocop discovered.&lt;br /&gt;
&lt;br /&gt;
== Providing automated tests ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 20&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# running &amp;lt;code&amp;gt;bundle exec rake test&amp;lt;/code&amp;gt; runs your test suite&lt;br /&gt;
# your test suite has no failing tests&lt;br /&gt;
# your test suite contains at least one meaningful test, testing your program&#039;s logic&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://launchschool.com/blog/assert-yourself-an-introduction-to-minitest Introduction to MiniTest]&lt;br /&gt;
&lt;br /&gt;
== Structuring your project as a Rubygem ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# your project directory structure matches the structure created by &amp;lt;code&amp;gt;bundle gem NAME_OF_YOUR_PROJECT&amp;lt;/code&amp;gt;&lt;br /&gt;
# additional dependencies are specified in the &amp;lt;code&amp;gt;.gemspec&amp;lt;/code&amp;gt; file of your project&lt;br /&gt;
&lt;br /&gt;
If your project is using a framework with strong conventions, such as Ruby on Rails, these points are awarded if you place your code according to the conventions of the framework.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://code.tutsplus.com/tutorials/gem-creation-with-bundler--net-25281 Creating a gem with Bundler]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Answering 80% of the questions correctly ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 20&lt;br /&gt;
&lt;br /&gt;
You will be asked 3 to 5 questions about your project, testing your understanding of what is happening in the code.&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# you answer 80% or more of the questions correctly&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  class Error &amp;lt; StandardError; end&lt;br /&gt;
  class BalanceNegative &amp;lt; Error; end&lt;br /&gt;
&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increse_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &#039;Not enough money!&#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increse_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def setup&lt;br /&gt;
    @account = BankAccount.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_decreses_the_balance_when_withdrawing_money&lt;br /&gt;
    @account.deposit!(101).withdraw!(50)&lt;br /&gt;
    assert_equal 51, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_increses_the_balance_when_depositing_money&lt;br /&gt;
    @account.deposit!(196583)&lt;br /&gt;
    assert_equal 196583, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_account_does_not_go_in_to_negative&lt;br /&gt;
    assert_raises BankAccount::BalanceNegative do&lt;br /&gt;
      @account.deposit!(1).withdraw!(20)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_it_raises_bank_account_error_when_withdrawing_too_much_money&lt;br /&gt;
    @account.deposit!(1).withdraw!(20)&lt;br /&gt;
  rescue BankAccount::Error =&amp;gt; err&lt;br /&gt;
    assert true, &#039;everything is ok&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-16 Lecture and Lab: using modules and bundler =&lt;br /&gt;
&lt;br /&gt;
== Modules in Ruby ==&lt;br /&gt;
&lt;br /&gt;
* Modules are a collection of methods that can be included in another class&lt;br /&gt;
* By including the module in a class, objects belonging to that class get access to the methods defined in the module&lt;br /&gt;
* If multiple included modules define the same method, the method from the module that was included last counts&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
module ModuleExample&lt;br /&gt;
  def foo&lt;br /&gt;
    &#039;foo&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Including modules binds their methods to the class instances&lt;br /&gt;
# Extending modules binds their methods to the class itself&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  include ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Book&lt;br /&gt;
  extend ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Person.foo     # =&amp;gt; NoMethodError: undefined method `foo&#039; for Person:Class&lt;br /&gt;
Person.new.foo # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.foo       # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.new.foo   # =&amp;gt; NoMethodError: undefined method `foo&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
== Structuring a project with bundler ==&lt;br /&gt;
&lt;br /&gt;
* bundler is a Ruby program for managing dependencies in your ruby project&lt;br /&gt;
* bundler also helps you with creating a template for a project&lt;br /&gt;
* redistributable projects/libraries are called &amp;quot;gems&amp;quot; in ruby&lt;br /&gt;
* You can start your own project by running &amp;lt;code&amp;gt;bundle gem YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt;.  Replace &amp;lt;code&amp;gt;YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt; with the lower-cased name of your project.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example structure of a project, as generated by bundler&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ tree&lt;br /&gt;
.&lt;br /&gt;
├── bin&lt;br /&gt;
│   ├── console&lt;br /&gt;
│   └── setup&lt;br /&gt;
├── exe&lt;br /&gt;
│   └── hello-world&lt;br /&gt;
├── Gemfile&lt;br /&gt;
├── Gemfile.lock&lt;br /&gt;
├── I704.gemspec&lt;br /&gt;
├── lib&lt;br /&gt;
│   ├── I704&lt;br /&gt;
│   │   ├── bank_account.rb&lt;br /&gt;
│   │   └── version.rb&lt;br /&gt;
│   └── I704.rb&lt;br /&gt;
├── LICENSE.txt&lt;br /&gt;
├── Rakefile&lt;br /&gt;
├── README.md&lt;br /&gt;
└── test&lt;br /&gt;
    ├── I704&lt;br /&gt;
    │   └── bank_account_test.rb&lt;br /&gt;
    ├── I704_test.rb&lt;br /&gt;
    └── test_helper.rb&lt;br /&gt;
&lt;br /&gt;
6 directories, 15 files&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* exectuables that users of your gem should be able to run go into the &amp;lt;code&amp;gt;exe&amp;lt;/code&amp;gt; directory&lt;br /&gt;
* add dependencies in your &amp;lt;code&amp;gt;.gemspec&amp;lt;/code&amp;gt; file.  After adding a dependency, run &amp;lt;code&amp;gt;bundle install&amp;lt;/code&amp;gt; to install the dependency on your computer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rule of thumb&#039;&#039;&#039;: put your code into the &amp;lt;code&amp;gt;lib/YOUR_PROJECT_NAME&amp;lt;/code&amp;gt; directory, one file per class (like &amp;lt;code&amp;gt;bank_account.rb&amp;lt;/code&amp;gt; in the example above)&lt;br /&gt;
&lt;br /&gt;
= 2017-03-02 Lecture &amp;amp; Lab =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Word count example&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Burning Rangers is a 1998 3D action video game developed by Sonic Team and published by Sega for the Sega Saturn. The game is set in a futuristic society threatened by frequent fires. Players control one of an elite group of firefighters, the Burning Rangers, who extinguish the fires and rescue civilians from burning buildings. Most of the tasks the players complete are centred around collecting energy crystals used to transport civilians to safety. Development began shortly after the release of Christmas Nights in November 1996, when Yuji Naka started working on a game focused on saving people rather than killing them. Sonic Team chose the themes of firefighting and heroism. Burning Rangers received mostly positive reviews, especially for the game&#039;s soundtrack and audio. Responses to the graphics were mixed; while some critics asserted that the game had the best visuals on the Saturn, others faulted its poor collision detection and occasional glitching. The game was among the final five Saturn titles released in America.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
words = text.split(&#039; &#039;)&lt;br /&gt;
&lt;br /&gt;
words_without_punctuation = words.map do |word|&lt;br /&gt;
  word.sub(/[.,;?!:]/, &#039;&#039;).downcase&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
word_count = words_without_punctuation.reduce({}) do |result, word|&lt;br /&gt;
  if result.key?(word)&lt;br /&gt;
    result[word] = result[word] + 1&lt;br /&gt;
    # also possible: result[word] += 1&lt;br /&gt;
  else&lt;br /&gt;
    result[word] = 1&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  result&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts word_count&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-03-09 lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;thread&#039;&lt;br /&gt;
&lt;br /&gt;
# [1, 2, ..., 100]&lt;br /&gt;
# [t1, t2, ..., t100]&lt;br /&gt;
&lt;br /&gt;
# Lock / Mutex (mutual exclusion)&lt;br /&gt;
# Queue&lt;br /&gt;
&lt;br /&gt;
lock = Mutex.new&lt;br /&gt;
threads = (1..100).map do |number|&lt;br /&gt;
  Thread.new do&lt;br /&gt;
    sleep 0.5&lt;br /&gt;
&lt;br /&gt;
    lock.lock&lt;br /&gt;
    puts number&lt;br /&gt;
    lock.unlock&lt;br /&gt;
&lt;br /&gt;
    lock.synchronize do&lt;br /&gt;
      if number == 1&lt;br /&gt;
        sleep 5&lt;br /&gt;
      end&lt;br /&gt;
      puts number&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
threads.each do |thread|&lt;br /&gt;
  thread.join&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: making HTTP requests in Ruby =&lt;br /&gt;
&lt;br /&gt;
# Require the &amp;quot;net/http&amp;quot; library in your program&lt;br /&gt;
# Use that library to make HTTP requests for fetching the HTML code behind a website&lt;br /&gt;
# 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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;net/http&#039;&lt;br /&gt;
google_html = Net::HTTP.get(URI(&#039;http://google.com&#039;))&lt;br /&gt;
# &amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=\&amp;quot;content-type\&amp;quot; content=\&amp;quot;text/html;charset=utf-8\&amp;quot;&amp;gt;\n&amp;lt;TITLE&amp;gt;302 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;\n&amp;lt;H1&amp;gt;302 Moved&amp;lt;/H1&amp;gt;\nThe document has moved\n&amp;lt;A HREF=\&amp;quot;http://www.google.ee/?gfe_rd=cr&amp;amp;amp;ei=o6KlWKCGA9Oq8wfoq5OQAg\&amp;quot;&amp;gt;here&amp;lt;/A&amp;gt;.\r\n&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;\r\n&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Now you can work with the HTML returned by the server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: parsing HTML using Nokogiri =&lt;br /&gt;
&lt;br /&gt;
Once you have received some HTML from a web server, you most likely want to analyze it.  Use the library nokogiri for this.  You can find installation instructions and tutorials here: http://www.nokogiri.org/tutorials/&lt;br /&gt;
&lt;br /&gt;
= Reference material: map and reduce =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# map: takes a list and a block&lt;br /&gt;
#    returns: a list, where each element of the original list has been&lt;br /&gt;
#    passed to block&lt;br /&gt;
#&lt;br /&gt;
# reduce: takes list, an initial value and a block&lt;br /&gt;
#    returns: block applied to initial value and each element of the list&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = numbers.reduce(0) do |current_sum, number|&lt;br /&gt;
  current_sum + number&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;Sum: #{sum}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
####&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = 0&lt;br /&gt;
numbers.each do |number|&lt;br /&gt;
  sum = sum + number&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118298</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118298"/>
		<updated>2017-03-02T19:39:49Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: /* Proper use of version control */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= Lecture Recordings =&lt;br /&gt;
&lt;br /&gt;
You can find recordings of the lectures here:&lt;br /&gt;
&lt;br /&gt;
https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
* [https://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-select Ruby Enumerable]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for a having a README describing your project&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 10 points for structuring your project as a Rubygem&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
== Proper use of version control ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# you keep your code in a git repository&lt;br /&gt;
# a single commit represents a single logical step in the development process (e.g. adding a test and the code to make it pass, fixing a single bug, refactoring one area of the code, etc)&lt;br /&gt;
# most commit messages follow the guidelines for good commit messages (see reference material below)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://chris.beams.io/posts/git-commit/ How to write a Git Commit Message] (see &amp;quot;The Seven Rules&amp;quot;)&lt;br /&gt;
* [http://nuclearsquid.com/writings/git-add/ How to select a subset of all changes for your next commit]&lt;br /&gt;
&lt;br /&gt;
== Having a README ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# your project has a file called README.md in your project&#039;s root directory&lt;br /&gt;
# the README.md file contains a description of: what your program does, how to install it and how to run it&lt;br /&gt;
&lt;br /&gt;
== Adhering to common Ruby coding standards ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# running &amp;lt;code&amp;gt;rubocop&amp;lt;/code&amp;gt; with the default configuration in your project produces no errors&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [http://rubocop.readthedocs.io/en/latest/basic_usage/#basic-usage Rubocop Basic Usage].  &#039;&#039;&#039;Hint:&#039;&#039;&#039;  You can run Rubocop with the &amp;lt;code&amp;gt;--auto-correct&amp;lt;/code&amp;gt; option to automatically fix some of the problems Rubocop discovered.&lt;br /&gt;
&lt;br /&gt;
== Providing automated tests ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 20&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# running &amp;lt;code&amp;gt;bundle exec rake test&amp;lt;/code&amp;gt; runs your test suite&lt;br /&gt;
# your test suite has no failing tests&lt;br /&gt;
# your test suite contains at least one meaningful test, testing your program&#039;s logic&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://launchschool.com/blog/assert-yourself-an-introduction-to-minitest Introduction to MiniTest]&lt;br /&gt;
&lt;br /&gt;
== Structuring your project as a Rubygem ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# your project directory structure matches the structure created by &amp;lt;code&amp;gt;bundle gem NAME_OF_YOUR_PROJECT&amp;lt;/code&amp;gt;&lt;br /&gt;
# additional dependencies are specified in the &amp;lt;code&amp;gt;.gemspec&amp;lt;/code&amp;gt; file of your project&lt;br /&gt;
&lt;br /&gt;
If your project is using a framework with strong conventions, such as Ruby on Rails, these points are awarded if you place your code according to the conventions of the framework.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://code.tutsplus.com/tutorials/gem-creation-with-bundler--net-25281 Creating a gem with Bundler]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Answering 80% of the questions correctly ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 20&lt;br /&gt;
&lt;br /&gt;
You will be asked 3 to 5 questions about your project, testing your understanding of what is happening in the code.&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# you answer 80% or more of the questions correctly&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  class Error &amp;lt; StandardError; end&lt;br /&gt;
  class BalanceNegative &amp;lt; Error; end&lt;br /&gt;
&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increse_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &#039;Not enough money!&#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increse_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def setup&lt;br /&gt;
    @account = BankAccount.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_decreses_the_balance_when_withdrawing_money&lt;br /&gt;
    @account.deposit!(101).withdraw!(50)&lt;br /&gt;
    assert_equal 51, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_increses_the_balance_when_depositing_money&lt;br /&gt;
    @account.deposit!(196583)&lt;br /&gt;
    assert_equal 196583, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_account_does_not_go_in_to_negative&lt;br /&gt;
    assert_raises BankAccount::BalanceNegative do&lt;br /&gt;
      @account.deposit!(1).withdraw!(20)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_it_raises_bank_account_error_when_withdrawing_too_much_money&lt;br /&gt;
    @account.deposit!(1).withdraw!(20)&lt;br /&gt;
  rescue BankAccount::Error =&amp;gt; err&lt;br /&gt;
    assert true, &#039;everything is ok&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-16 Lecture and Lab: using modules and bundler =&lt;br /&gt;
&lt;br /&gt;
== Modules in Ruby ==&lt;br /&gt;
&lt;br /&gt;
* Modules are a collection of methods that can be included in another class&lt;br /&gt;
* By including the module in a class, objects belonging to that class get access to the methods defined in the module&lt;br /&gt;
* If multiple included modules define the same method, the method from the module that was included last counts&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
module ModuleExample&lt;br /&gt;
  def foo&lt;br /&gt;
    &#039;foo&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Including modules binds their methods to the class instances&lt;br /&gt;
# Extending modules binds their methods to the class itself&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  include ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Book&lt;br /&gt;
  extend ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Person.foo     # =&amp;gt; NoMethodError: undefined method `foo&#039; for Person:Class&lt;br /&gt;
Person.new.foo # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.foo       # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.new.foo   # =&amp;gt; NoMethodError: undefined method `foo&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
== Structuring a project with bundler ==&lt;br /&gt;
&lt;br /&gt;
* bundler is a Ruby program for managing dependencies in your ruby project&lt;br /&gt;
* bundler also helps you with creating a template for a project&lt;br /&gt;
* redistributable projects/libraries are called &amp;quot;gems&amp;quot; in ruby&lt;br /&gt;
* You can start your own project by running &amp;lt;code&amp;gt;bundle gem YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt;.  Replace &amp;lt;code&amp;gt;YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt; with the lower-cased name of your project.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example structure of a project, as generated by bundler&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ tree&lt;br /&gt;
.&lt;br /&gt;
├── bin&lt;br /&gt;
│   ├── console&lt;br /&gt;
│   └── setup&lt;br /&gt;
├── exe&lt;br /&gt;
│   └── hello-world&lt;br /&gt;
├── Gemfile&lt;br /&gt;
├── Gemfile.lock&lt;br /&gt;
├── I704.gemspec&lt;br /&gt;
├── lib&lt;br /&gt;
│   ├── I704&lt;br /&gt;
│   │   ├── bank_account.rb&lt;br /&gt;
│   │   └── version.rb&lt;br /&gt;
│   └── I704.rb&lt;br /&gt;
├── LICENSE.txt&lt;br /&gt;
├── Rakefile&lt;br /&gt;
├── README.md&lt;br /&gt;
└── test&lt;br /&gt;
    ├── I704&lt;br /&gt;
    │   └── bank_account_test.rb&lt;br /&gt;
    ├── I704_test.rb&lt;br /&gt;
    └── test_helper.rb&lt;br /&gt;
&lt;br /&gt;
6 directories, 15 files&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* exectuables that users of your gem should be able to run go into the &amp;lt;code&amp;gt;exe&amp;lt;/code&amp;gt; directory&lt;br /&gt;
* add dependencies in your &amp;lt;code&amp;gt;.gemspec&amp;lt;/code&amp;gt; file.  After adding a dependency, run &amp;lt;code&amp;gt;bundle install&amp;lt;/code&amp;gt; to install the dependency on your computer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rule of thumb&#039;&#039;&#039;: put your code into the &amp;lt;code&amp;gt;lib/YOUR_PROJECT_NAME&amp;lt;/code&amp;gt; directory, one file per class (like &amp;lt;code&amp;gt;bank_account.rb&amp;lt;/code&amp;gt; in the example above)&lt;br /&gt;
&lt;br /&gt;
= 2017-03-02 Lecture &amp;amp; Lab =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Word count example&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Burning Rangers is a 1998 3D action video game developed by Sonic Team and published by Sega for the Sega Saturn. The game is set in a futuristic society threatened by frequent fires. Players control one of an elite group of firefighters, the Burning Rangers, who extinguish the fires and rescue civilians from burning buildings. Most of the tasks the players complete are centred around collecting energy crystals used to transport civilians to safety. Development began shortly after the release of Christmas Nights in November 1996, when Yuji Naka started working on a game focused on saving people rather than killing them. Sonic Team chose the themes of firefighting and heroism. Burning Rangers received mostly positive reviews, especially for the game&#039;s soundtrack and audio. Responses to the graphics were mixed; while some critics asserted that the game had the best visuals on the Saturn, others faulted its poor collision detection and occasional glitching. The game was among the final five Saturn titles released in America.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
words = text.split(&#039; &#039;)&lt;br /&gt;
&lt;br /&gt;
words_without_punctuation = words.map do |word|&lt;br /&gt;
  word.sub(/[.,;?!:]/, &#039;&#039;).downcase&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
word_count = words_without_punctuation.reduce({}) do |result, word|&lt;br /&gt;
  if result.key?(word)&lt;br /&gt;
    result[word] = result[word] + 1&lt;br /&gt;
    # also possible: result[word] += 1&lt;br /&gt;
  else&lt;br /&gt;
    result[word] = 1&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  result&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts word_count&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: making HTTP requests in Ruby =&lt;br /&gt;
&lt;br /&gt;
# Require the &amp;quot;net/http&amp;quot; library in your program&lt;br /&gt;
# Use that library to make HTTP requests for fetching the HTML code behind a website&lt;br /&gt;
# 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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;net/http&#039;&lt;br /&gt;
google_html = Net::HTTP.get(URI(&#039;http://google.com&#039;))&lt;br /&gt;
# &amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=\&amp;quot;content-type\&amp;quot; content=\&amp;quot;text/html;charset=utf-8\&amp;quot;&amp;gt;\n&amp;lt;TITLE&amp;gt;302 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;\n&amp;lt;H1&amp;gt;302 Moved&amp;lt;/H1&amp;gt;\nThe document has moved\n&amp;lt;A HREF=\&amp;quot;http://www.google.ee/?gfe_rd=cr&amp;amp;amp;ei=o6KlWKCGA9Oq8wfoq5OQAg\&amp;quot;&amp;gt;here&amp;lt;/A&amp;gt;.\r\n&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;\r\n&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Now you can work with the HTML returned by the server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: parsing HTML using Nokogiri =&lt;br /&gt;
&lt;br /&gt;
Once you have received some HTML from a web server, you most likely want to analyze it.  Use the library nokogiri for this.  You can find installation instructions and tutorials here: http://www.nokogiri.org/tutorials/&lt;br /&gt;
&lt;br /&gt;
= Reference material: map and reduce =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# map: takes a list and a block&lt;br /&gt;
#    returns: a list, where each element of the original list has been&lt;br /&gt;
#    passed to block&lt;br /&gt;
#&lt;br /&gt;
# reduce: takes list, an initial value and a block&lt;br /&gt;
#    returns: block applied to initial value and each element of the list&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = numbers.reduce(0) do |current_sum, number|&lt;br /&gt;
  current_sum + number&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;Sum: #{sum}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
####&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = 0&lt;br /&gt;
numbers.each do |number|&lt;br /&gt;
  sum = sum + number&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118297</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118297"/>
		<updated>2017-03-02T19:39:11Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: Provide a detailed description of the grading criteria&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= Lecture Recordings =&lt;br /&gt;
&lt;br /&gt;
You can find recordings of the lectures here:&lt;br /&gt;
&lt;br /&gt;
https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
* [https://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-select Ruby Enumerable]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for a having a README describing your project&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 10 points for structuring your project as a Rubygem&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
== Proper use of version control ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# you keep your code in a git repository&lt;br /&gt;
# a single commit represents a single logical step in the development&lt;br /&gt;
  process (e.g. adding a test and the code to make it pass, fixing a&lt;br /&gt;
  single bug, refactoring one area of the code, etc)&lt;br /&gt;
# most commit messages follow the guidelines for good commit messages (see reference material below)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://chris.beams.io/posts/git-commit/ How to write a Git Commit Message] (see &amp;quot;The Seven Rules&amp;quot;)&lt;br /&gt;
* [http://nuclearsquid.com/writings/git-add/ How to select a subset of all changes for your next commit] &lt;br /&gt;
&lt;br /&gt;
== Having a README ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# your project has a file called README.md in your project&#039;s root directory&lt;br /&gt;
# the README.md file contains a description of: what your program does, how to install it and how to run it&lt;br /&gt;
&lt;br /&gt;
== Adhering to common Ruby coding standards ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# running &amp;lt;code&amp;gt;rubocop&amp;lt;/code&amp;gt; with the default configuration in your project produces no errors&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [http://rubocop.readthedocs.io/en/latest/basic_usage/#basic-usage Rubocop Basic Usage].  &#039;&#039;&#039;Hint:&#039;&#039;&#039;  You can run Rubocop with the &amp;lt;code&amp;gt;--auto-correct&amp;lt;/code&amp;gt; option to automatically fix some of the problems Rubocop discovered.&lt;br /&gt;
&lt;br /&gt;
== Providing automated tests ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 20&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# running &amp;lt;code&amp;gt;bundle exec rake test&amp;lt;/code&amp;gt; runs your test suite&lt;br /&gt;
# your test suite has no failing tests&lt;br /&gt;
# your test suite contains at least one meaningful test, testing your program&#039;s logic&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://launchschool.com/blog/assert-yourself-an-introduction-to-minitest Introduction to MiniTest]&lt;br /&gt;
&lt;br /&gt;
== Structuring your project as a Rubygem ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 10&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# your project directory structure matches the structure created by &amp;lt;code&amp;gt;bundle gem NAME_OF_YOUR_PROJECT&amp;lt;/code&amp;gt;&lt;br /&gt;
# additional dependencies are specified in the &amp;lt;code&amp;gt;.gemspec&amp;lt;/code&amp;gt; file of your project&lt;br /&gt;
&lt;br /&gt;
If your project is using a framework with strong conventions, such as Ruby on Rails, these points are awarded if you place your code according to the conventions of the framework.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Reference material&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* [https://code.tutsplus.com/tutorials/gem-creation-with-bundler--net-25281 Creating a gem with Bundler]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Answering 80% of the questions correctly ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Point value:&#039;&#039;&#039; 20&lt;br /&gt;
&lt;br /&gt;
You will be asked 3 to 5 questions about your project, testing your understanding of what is happening in the code.&lt;br /&gt;
&lt;br /&gt;
These points are awarded if:&lt;br /&gt;
&lt;br /&gt;
# you answer 80% or more of the questions correctly&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  class Error &amp;lt; StandardError; end&lt;br /&gt;
  class BalanceNegative &amp;lt; Error; end&lt;br /&gt;
&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increse_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &#039;Not enough money!&#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increse_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def setup&lt;br /&gt;
    @account = BankAccount.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_decreses_the_balance_when_withdrawing_money&lt;br /&gt;
    @account.deposit!(101).withdraw!(50)&lt;br /&gt;
    assert_equal 51, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_increses_the_balance_when_depositing_money&lt;br /&gt;
    @account.deposit!(196583)&lt;br /&gt;
    assert_equal 196583, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_account_does_not_go_in_to_negative&lt;br /&gt;
    assert_raises BankAccount::BalanceNegative do&lt;br /&gt;
      @account.deposit!(1).withdraw!(20)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_it_raises_bank_account_error_when_withdrawing_too_much_money&lt;br /&gt;
    @account.deposit!(1).withdraw!(20)&lt;br /&gt;
  rescue BankAccount::Error =&amp;gt; err&lt;br /&gt;
    assert true, &#039;everything is ok&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-16 Lecture and Lab: using modules and bundler =&lt;br /&gt;
&lt;br /&gt;
== Modules in Ruby ==&lt;br /&gt;
&lt;br /&gt;
* Modules are a collection of methods that can be included in another class&lt;br /&gt;
* By including the module in a class, objects belonging to that class get access to the methods defined in the module&lt;br /&gt;
* If multiple included modules define the same method, the method from the module that was included last counts&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
module ModuleExample&lt;br /&gt;
  def foo&lt;br /&gt;
    &#039;foo&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Including modules binds their methods to the class instances&lt;br /&gt;
# Extending modules binds their methods to the class itself&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  include ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Book&lt;br /&gt;
  extend ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Person.foo     # =&amp;gt; NoMethodError: undefined method `foo&#039; for Person:Class&lt;br /&gt;
Person.new.foo # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.foo       # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.new.foo   # =&amp;gt; NoMethodError: undefined method `foo&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
== Structuring a project with bundler ==&lt;br /&gt;
&lt;br /&gt;
* bundler is a Ruby program for managing dependencies in your ruby project&lt;br /&gt;
* bundler also helps you with creating a template for a project&lt;br /&gt;
* redistributable projects/libraries are called &amp;quot;gems&amp;quot; in ruby&lt;br /&gt;
* You can start your own project by running &amp;lt;code&amp;gt;bundle gem YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt;.  Replace &amp;lt;code&amp;gt;YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt; with the lower-cased name of your project.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example structure of a project, as generated by bundler&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ tree&lt;br /&gt;
.&lt;br /&gt;
├── bin&lt;br /&gt;
│   ├── console&lt;br /&gt;
│   └── setup&lt;br /&gt;
├── exe&lt;br /&gt;
│   └── hello-world&lt;br /&gt;
├── Gemfile&lt;br /&gt;
├── Gemfile.lock&lt;br /&gt;
├── I704.gemspec&lt;br /&gt;
├── lib&lt;br /&gt;
│   ├── I704&lt;br /&gt;
│   │   ├── bank_account.rb&lt;br /&gt;
│   │   └── version.rb&lt;br /&gt;
│   └── I704.rb&lt;br /&gt;
├── LICENSE.txt&lt;br /&gt;
├── Rakefile&lt;br /&gt;
├── README.md&lt;br /&gt;
└── test&lt;br /&gt;
    ├── I704&lt;br /&gt;
    │   └── bank_account_test.rb&lt;br /&gt;
    ├── I704_test.rb&lt;br /&gt;
    └── test_helper.rb&lt;br /&gt;
&lt;br /&gt;
6 directories, 15 files&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* exectuables that users of your gem should be able to run go into the &amp;lt;code&amp;gt;exe&amp;lt;/code&amp;gt; directory&lt;br /&gt;
* add dependencies in your &amp;lt;code&amp;gt;.gemspec&amp;lt;/code&amp;gt; file.  After adding a dependency, run &amp;lt;code&amp;gt;bundle install&amp;lt;/code&amp;gt; to install the dependency on your computer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rule of thumb&#039;&#039;&#039;: put your code into the &amp;lt;code&amp;gt;lib/YOUR_PROJECT_NAME&amp;lt;/code&amp;gt; directory, one file per class (like &amp;lt;code&amp;gt;bank_account.rb&amp;lt;/code&amp;gt; in the example above)&lt;br /&gt;
&lt;br /&gt;
= 2017-03-02 Lecture &amp;amp; Lab =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Word count example&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Burning Rangers is a 1998 3D action video game developed by Sonic Team and published by Sega for the Sega Saturn. The game is set in a futuristic society threatened by frequent fires. Players control one of an elite group of firefighters, the Burning Rangers, who extinguish the fires and rescue civilians from burning buildings. Most of the tasks the players complete are centred around collecting energy crystals used to transport civilians to safety. Development began shortly after the release of Christmas Nights in November 1996, when Yuji Naka started working on a game focused on saving people rather than killing them. Sonic Team chose the themes of firefighting and heroism. Burning Rangers received mostly positive reviews, especially for the game&#039;s soundtrack and audio. Responses to the graphics were mixed; while some critics asserted that the game had the best visuals on the Saturn, others faulted its poor collision detection and occasional glitching. The game was among the final five Saturn titles released in America.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
words = text.split(&#039; &#039;)&lt;br /&gt;
&lt;br /&gt;
words_without_punctuation = words.map do |word|&lt;br /&gt;
  word.sub(/[.,;?!:]/, &#039;&#039;).downcase&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
word_count = words_without_punctuation.reduce({}) do |result, word|&lt;br /&gt;
  if result.key?(word)&lt;br /&gt;
    result[word] = result[word] + 1&lt;br /&gt;
    # also possible: result[word] += 1&lt;br /&gt;
  else&lt;br /&gt;
    result[word] = 1&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  result&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts word_count&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: making HTTP requests in Ruby =&lt;br /&gt;
&lt;br /&gt;
# Require the &amp;quot;net/http&amp;quot; library in your program&lt;br /&gt;
# Use that library to make HTTP requests for fetching the HTML code behind a website&lt;br /&gt;
# 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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;net/http&#039;&lt;br /&gt;
google_html = Net::HTTP.get(URI(&#039;http://google.com&#039;))&lt;br /&gt;
# &amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=\&amp;quot;content-type\&amp;quot; content=\&amp;quot;text/html;charset=utf-8\&amp;quot;&amp;gt;\n&amp;lt;TITLE&amp;gt;302 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;\n&amp;lt;H1&amp;gt;302 Moved&amp;lt;/H1&amp;gt;\nThe document has moved\n&amp;lt;A HREF=\&amp;quot;http://www.google.ee/?gfe_rd=cr&amp;amp;amp;ei=o6KlWKCGA9Oq8wfoq5OQAg\&amp;quot;&amp;gt;here&amp;lt;/A&amp;gt;.\r\n&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;\r\n&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Now you can work with the HTML returned by the server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: parsing HTML using Nokogiri =&lt;br /&gt;
&lt;br /&gt;
Once you have received some HTML from a web server, you most likely want to analyze it.  Use the library nokogiri for this.  You can find installation instructions and tutorials here: http://www.nokogiri.org/tutorials/&lt;br /&gt;
&lt;br /&gt;
= Reference material: map and reduce =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# map: takes a list and a block&lt;br /&gt;
#    returns: a list, where each element of the original list has been&lt;br /&gt;
#    passed to block&lt;br /&gt;
#&lt;br /&gt;
# reduce: takes list, an initial value and a block&lt;br /&gt;
#    returns: block applied to initial value and each element of the list&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = numbers.reduce(0) do |current_sum, number|&lt;br /&gt;
  current_sum + number&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;Sum: #{sum}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
####&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = 0&lt;br /&gt;
numbers.each do |number|&lt;br /&gt;
  sum = sum + number&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118296</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118296"/>
		<updated>2017-03-02T18:56:43Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: /* Structuring a project with bundler */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= Lecture Recordings =&lt;br /&gt;
&lt;br /&gt;
You can find recordings of the lectures here:&lt;br /&gt;
&lt;br /&gt;
https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
* [https://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-select Ruby Enumerable]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  class Error &amp;lt; StandardError; end&lt;br /&gt;
  class BalanceNegative &amp;lt; Error; end&lt;br /&gt;
&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increse_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &#039;Not enough money!&#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increse_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def setup&lt;br /&gt;
    @account = BankAccount.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_decreses_the_balance_when_withdrawing_money&lt;br /&gt;
    @account.deposit!(101).withdraw!(50)&lt;br /&gt;
    assert_equal 51, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_increses_the_balance_when_depositing_money&lt;br /&gt;
    @account.deposit!(196583)&lt;br /&gt;
    assert_equal 196583, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_account_does_not_go_in_to_negative&lt;br /&gt;
    assert_raises BankAccount::BalanceNegative do&lt;br /&gt;
      @account.deposit!(1).withdraw!(20)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_it_raises_bank_account_error_when_withdrawing_too_much_money&lt;br /&gt;
    @account.deposit!(1).withdraw!(20)&lt;br /&gt;
  rescue BankAccount::Error =&amp;gt; err&lt;br /&gt;
    assert true, &#039;everything is ok&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-16 Lecture and Lab: using modules and bundler =&lt;br /&gt;
&lt;br /&gt;
== Modules in Ruby ==&lt;br /&gt;
&lt;br /&gt;
* Modules are a collection of methods that can be included in another class&lt;br /&gt;
* By including the module in a class, objects belonging to that class get access to the methods defined in the module&lt;br /&gt;
* If multiple included modules define the same method, the method from the module that was included last counts&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
module ModuleExample&lt;br /&gt;
  def foo&lt;br /&gt;
    &#039;foo&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Including modules binds their methods to the class instances&lt;br /&gt;
# Extending modules binds their methods to the class itself&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  include ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Book&lt;br /&gt;
  extend ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Person.foo     # =&amp;gt; NoMethodError: undefined method `foo&#039; for Person:Class&lt;br /&gt;
Person.new.foo # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.foo       # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.new.foo   # =&amp;gt; NoMethodError: undefined method `foo&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
== Structuring a project with bundler ==&lt;br /&gt;
&lt;br /&gt;
* bundler is a Ruby program for managing dependencies in your ruby project&lt;br /&gt;
* bundler also helps you with creating a template for a project&lt;br /&gt;
* redistributable projects/libraries are called &amp;quot;gems&amp;quot; in ruby&lt;br /&gt;
* You can start your own project by running &amp;lt;code&amp;gt;bundle gem YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt;.  Replace &amp;lt;code&amp;gt;YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt; with the lower-cased name of your project.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example structure of a project, as generated by bundler&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ tree&lt;br /&gt;
.&lt;br /&gt;
├── bin&lt;br /&gt;
│   ├── console&lt;br /&gt;
│   └── setup&lt;br /&gt;
├── exe&lt;br /&gt;
│   └── hello-world&lt;br /&gt;
├── Gemfile&lt;br /&gt;
├── Gemfile.lock&lt;br /&gt;
├── I704.gemspec&lt;br /&gt;
├── lib&lt;br /&gt;
│   ├── I704&lt;br /&gt;
│   │   ├── bank_account.rb&lt;br /&gt;
│   │   └── version.rb&lt;br /&gt;
│   └── I704.rb&lt;br /&gt;
├── LICENSE.txt&lt;br /&gt;
├── Rakefile&lt;br /&gt;
├── README.md&lt;br /&gt;
└── test&lt;br /&gt;
    ├── I704&lt;br /&gt;
    │   └── bank_account_test.rb&lt;br /&gt;
    ├── I704_test.rb&lt;br /&gt;
    └── test_helper.rb&lt;br /&gt;
&lt;br /&gt;
6 directories, 15 files&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* exectuables that users of your gem should be able to run go into the &amp;lt;code&amp;gt;exe&amp;lt;/code&amp;gt; directory&lt;br /&gt;
* add dependencies in your &amp;lt;code&amp;gt;.gemspec&amp;lt;/code&amp;gt; file.  After adding a dependency, run &amp;lt;code&amp;gt;bundle install&amp;lt;/code&amp;gt; to install the dependency on your computer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rule of thumb&#039;&#039;&#039;: put your code into the &amp;lt;code&amp;gt;lib/YOUR_PROJECT_NAME&amp;lt;/code&amp;gt; directory, one file per class (like &amp;lt;code&amp;gt;bank_account.rb&amp;lt;/code&amp;gt; in the example above)&lt;br /&gt;
&lt;br /&gt;
= 2017-03-02 Lecture &amp;amp; Lab =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Word count example&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Burning Rangers is a 1998 3D action video game developed by Sonic Team and published by Sega for the Sega Saturn. The game is set in a futuristic society threatened by frequent fires. Players control one of an elite group of firefighters, the Burning Rangers, who extinguish the fires and rescue civilians from burning buildings. Most of the tasks the players complete are centred around collecting energy crystals used to transport civilians to safety. Development began shortly after the release of Christmas Nights in November 1996, when Yuji Naka started working on a game focused on saving people rather than killing them. Sonic Team chose the themes of firefighting and heroism. Burning Rangers received mostly positive reviews, especially for the game&#039;s soundtrack and audio. Responses to the graphics were mixed; while some critics asserted that the game had the best visuals on the Saturn, others faulted its poor collision detection and occasional glitching. The game was among the final five Saturn titles released in America.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
words = text.split(&#039; &#039;)&lt;br /&gt;
&lt;br /&gt;
words_without_punctuation = words.map do |word|&lt;br /&gt;
  word.sub(/[.,;?!:]/, &#039;&#039;).downcase&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
word_count = words_without_punctuation.reduce({}) do |result, word|&lt;br /&gt;
  if result.key?(word)&lt;br /&gt;
    result[word] = result[word] + 1&lt;br /&gt;
    # also possible: result[word] += 1&lt;br /&gt;
  else&lt;br /&gt;
    result[word] = 1&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  result&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts word_count&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: making HTTP requests in Ruby =&lt;br /&gt;
&lt;br /&gt;
# Require the &amp;quot;net/http&amp;quot; library in your program&lt;br /&gt;
# Use that library to make HTTP requests for fetching the HTML code behind a website&lt;br /&gt;
# 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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;net/http&#039;&lt;br /&gt;
google_html = Net::HTTP.get(URI(&#039;http://google.com&#039;))&lt;br /&gt;
# &amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=\&amp;quot;content-type\&amp;quot; content=\&amp;quot;text/html;charset=utf-8\&amp;quot;&amp;gt;\n&amp;lt;TITLE&amp;gt;302 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;\n&amp;lt;H1&amp;gt;302 Moved&amp;lt;/H1&amp;gt;\nThe document has moved\n&amp;lt;A HREF=\&amp;quot;http://www.google.ee/?gfe_rd=cr&amp;amp;amp;ei=o6KlWKCGA9Oq8wfoq5OQAg\&amp;quot;&amp;gt;here&amp;lt;/A&amp;gt;.\r\n&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;\r\n&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Now you can work with the HTML returned by the server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: parsing HTML using Nokogiri =&lt;br /&gt;
&lt;br /&gt;
Once you have received some HTML from a web server, you most likely want to analyze it.  Use the library nokogiri for this.  You can find installation instructions and tutorials here: http://www.nokogiri.org/tutorials/&lt;br /&gt;
&lt;br /&gt;
= Reference material: map and reduce =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# map: takes a list and a block&lt;br /&gt;
#    returns: a list, where each element of the original list has been&lt;br /&gt;
#    passed to block&lt;br /&gt;
#&lt;br /&gt;
# reduce: takes list, an initial value and a block&lt;br /&gt;
#    returns: block applied to initial value and each element of the list&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = numbers.reduce(0) do |current_sum, number|&lt;br /&gt;
  current_sum + number&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;Sum: #{sum}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
####&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = 0&lt;br /&gt;
numbers.each do |number|&lt;br /&gt;
  sum = sum + number&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118285</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118285"/>
		<updated>2017-03-02T10:47:40Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= Lecture Recordings =&lt;br /&gt;
&lt;br /&gt;
You can find recordings of the lectures here:&lt;br /&gt;
&lt;br /&gt;
https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
* [https://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-select Ruby Enumerable]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  class Error &amp;lt; StandardError; end&lt;br /&gt;
  class BalanceNegative &amp;lt; Error; end&lt;br /&gt;
&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increse_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &#039;Not enough money!&#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increse_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def setup&lt;br /&gt;
    @account = BankAccount.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_decreses_the_balance_when_withdrawing_money&lt;br /&gt;
    @account.deposit!(101).withdraw!(50)&lt;br /&gt;
    assert_equal 51, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_increses_the_balance_when_depositing_money&lt;br /&gt;
    @account.deposit!(196583)&lt;br /&gt;
    assert_equal 196583, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_account_does_not_go_in_to_negative&lt;br /&gt;
    assert_raises BankAccount::BalanceNegative do&lt;br /&gt;
      @account.deposit!(1).withdraw!(20)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_it_raises_bank_account_error_when_withdrawing_too_much_money&lt;br /&gt;
    @account.deposit!(1).withdraw!(20)&lt;br /&gt;
  rescue BankAccount::Error =&amp;gt; err&lt;br /&gt;
    assert true, &#039;everything is ok&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-16 Lecture and Lab: using modules and bundler =&lt;br /&gt;
&lt;br /&gt;
== Modules in Ruby ==&lt;br /&gt;
&lt;br /&gt;
* Modules are a collection of methods that can be included in another class&lt;br /&gt;
* By including the module in a class, objects belonging to that class get access to the methods defined in the module&lt;br /&gt;
* If multiple included modules define the same method, the method from the module that was included last counts&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
module ModuleExample&lt;br /&gt;
  def foo&lt;br /&gt;
    &#039;foo&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Including modules binds their methods to the class instances&lt;br /&gt;
# Extending modules binds their methods to the class itself&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  include ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Book&lt;br /&gt;
  extend ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Person.foo     # =&amp;gt; NoMethodError: undefined method `foo&#039; for Person:Class&lt;br /&gt;
Person.new.foo # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.foo       # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.new.foo   # =&amp;gt; NoMethodError: undefined method `foo&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
== Structuring a project with bundler ==&lt;br /&gt;
&lt;br /&gt;
* bundler is a Ruby program for managing dependencies in your ruby project&lt;br /&gt;
* bundler also helps you with creating a template for a project&lt;br /&gt;
* redistributable projects/libraries are called &amp;quot;gems&amp;quot; in ruby&lt;br /&gt;
* You can start your own project by running &amp;lt;code&amp;gt;bundle gem YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt;.  Replace &amp;lt;code&amp;gt;YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt; with the lower-cased name of your project.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example structure of a project, as generated by bundler&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ tree&lt;br /&gt;
.&lt;br /&gt;
├── bin&lt;br /&gt;
│   ├── console&lt;br /&gt;
│   └── setup&lt;br /&gt;
├── Gemfile&lt;br /&gt;
├── Gemfile.lock&lt;br /&gt;
├── I704.gemspec&lt;br /&gt;
├── lib&lt;br /&gt;
│   ├── I704&lt;br /&gt;
│   │   ├── bank_account.rb&lt;br /&gt;
│   │   └── version.rb&lt;br /&gt;
│   └── I704.rb&lt;br /&gt;
├── LICENSE.txt&lt;br /&gt;
├── Rakefile&lt;br /&gt;
├── README.md&lt;br /&gt;
└── test&lt;br /&gt;
    ├── I704&lt;br /&gt;
    │   └── bank_account_test.rb&lt;br /&gt;
    ├── I704_test.rb&lt;br /&gt;
    └── test_helper.rb&lt;br /&gt;
&lt;br /&gt;
5 directories, 14 files&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rule of thumb&#039;&#039;&#039;: put your code into the &amp;lt;code&amp;gt;lib/YOUR_PROJECT_NAME&amp;lt;/code&amp;gt; directory, one file per class (like &amp;lt;code&amp;gt;bank_account.rb&amp;lt;/code&amp;gt; in the example above)&lt;br /&gt;
&lt;br /&gt;
= 2017-03-02 Lecture &amp;amp; Lab =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Word count example&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
text = &amp;quot;Burning Rangers is a 1998 3D action video game developed by Sonic Team and published by Sega for the Sega Saturn. The game is set in a futuristic society threatened by frequent fires. Players control one of an elite group of firefighters, the Burning Rangers, who extinguish the fires and rescue civilians from burning buildings. Most of the tasks the players complete are centred around collecting energy crystals used to transport civilians to safety. Development began shortly after the release of Christmas Nights in November 1996, when Yuji Naka started working on a game focused on saving people rather than killing them. Sonic Team chose the themes of firefighting and heroism. Burning Rangers received mostly positive reviews, especially for the game&#039;s soundtrack and audio. Responses to the graphics were mixed; while some critics asserted that the game had the best visuals on the Saturn, others faulted its poor collision detection and occasional glitching. The game was among the final five Saturn titles released in America.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
words = text.split(&#039; &#039;)&lt;br /&gt;
&lt;br /&gt;
words_without_punctuation = words.map do |word|&lt;br /&gt;
  word.sub(/[.,;?!:]/, &#039;&#039;).downcase&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
word_count = words_without_punctuation.reduce({}) do |result, word|&lt;br /&gt;
  if result.key?(word)&lt;br /&gt;
    result[word] = result[word] + 1&lt;br /&gt;
    # also possible: result[word] += 1&lt;br /&gt;
  else&lt;br /&gt;
    result[word] = 1&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  result&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts word_count&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: making HTTP requests in Ruby =&lt;br /&gt;
&lt;br /&gt;
# Require the &amp;quot;net/http&amp;quot; library in your program&lt;br /&gt;
# Use that library to make HTTP requests for fetching the HTML code behind a website&lt;br /&gt;
# 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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;net/http&#039;&lt;br /&gt;
google_html = Net::HTTP.get(URI(&#039;http://google.com&#039;))&lt;br /&gt;
# &amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=\&amp;quot;content-type\&amp;quot; content=\&amp;quot;text/html;charset=utf-8\&amp;quot;&amp;gt;\n&amp;lt;TITLE&amp;gt;302 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;\n&amp;lt;H1&amp;gt;302 Moved&amp;lt;/H1&amp;gt;\nThe document has moved\n&amp;lt;A HREF=\&amp;quot;http://www.google.ee/?gfe_rd=cr&amp;amp;amp;ei=o6KlWKCGA9Oq8wfoq5OQAg\&amp;quot;&amp;gt;here&amp;lt;/A&amp;gt;.\r\n&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;\r\n&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Now you can work with the HTML returned by the server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: parsing HTML using Nokogiri =&lt;br /&gt;
&lt;br /&gt;
Once you have received some HTML from a web server, you most likely want to analyze it.  Use the library nokogiri for this.  You can find installation instructions and tutorials here: http://www.nokogiri.org/tutorials/&lt;br /&gt;
&lt;br /&gt;
= Reference material: map and reduce =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# map: takes a list and a block&lt;br /&gt;
#    returns: a list, where each element of the original list has been&lt;br /&gt;
#    passed to block&lt;br /&gt;
#&lt;br /&gt;
# reduce: takes list, an initial value and a block&lt;br /&gt;
#    returns: block applied to initial value and each element of the list&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = numbers.reduce(0) do |current_sum, number|&lt;br /&gt;
  current_sum + number&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;Sum: #{sum}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
####&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = 0&lt;br /&gt;
numbers.each do |number|&lt;br /&gt;
  sum = sum + number&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118284</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118284"/>
		<updated>2017-03-02T10:31:09Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= Lecture Recordings =&lt;br /&gt;
&lt;br /&gt;
You can find recordings of the lectures here:&lt;br /&gt;
&lt;br /&gt;
https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
* [https://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-select Ruby Enumerable]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  class Error &amp;lt; StandardError; end&lt;br /&gt;
  class BalanceNegative &amp;lt; Error; end&lt;br /&gt;
&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increse_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &#039;Not enough money!&#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increse_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def setup&lt;br /&gt;
    @account = BankAccount.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_decreses_the_balance_when_withdrawing_money&lt;br /&gt;
    @account.deposit!(101).withdraw!(50)&lt;br /&gt;
    assert_equal 51, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_increses_the_balance_when_depositing_money&lt;br /&gt;
    @account.deposit!(196583)&lt;br /&gt;
    assert_equal 196583, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_account_does_not_go_in_to_negative&lt;br /&gt;
    assert_raises BankAccount::BalanceNegative do&lt;br /&gt;
      @account.deposit!(1).withdraw!(20)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_it_raises_bank_account_error_when_withdrawing_too_much_money&lt;br /&gt;
    @account.deposit!(1).withdraw!(20)&lt;br /&gt;
  rescue BankAccount::Error =&amp;gt; err&lt;br /&gt;
    assert true, &#039;everything is ok&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-16 Lecture and Lab: using modules and bundler =&lt;br /&gt;
&lt;br /&gt;
== Modules in Ruby ==&lt;br /&gt;
&lt;br /&gt;
* Modules are a collection of methods that can be included in another class&lt;br /&gt;
* By including the module in a class, objects belonging to that class get access to the methods defined in the module&lt;br /&gt;
* If multiple included modules define the same method, the method from the module that was included last counts&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
module ModuleExample&lt;br /&gt;
  def foo&lt;br /&gt;
    &#039;foo&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Including modules binds their methods to the class instances&lt;br /&gt;
# Extending modules binds their methods to the class itself&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  include ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Book&lt;br /&gt;
  extend ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Person.foo     # =&amp;gt; NoMethodError: undefined method `foo&#039; for Person:Class&lt;br /&gt;
Person.new.foo # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.foo       # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.new.foo   # =&amp;gt; NoMethodError: undefined method `foo&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
== Structuring a project with bundler ==&lt;br /&gt;
&lt;br /&gt;
* bundler is a Ruby program for managing dependencies in your ruby project&lt;br /&gt;
* bundler also helps you with creating a template for a project&lt;br /&gt;
* redistributable projects/libraries are called &amp;quot;gems&amp;quot; in ruby&lt;br /&gt;
* You can start your own project by running &amp;lt;code&amp;gt;bundle gem YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt;.  Replace &amp;lt;code&amp;gt;YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt; with the lower-cased name of your project.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example structure of a project, as generated by bundler&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ tree&lt;br /&gt;
.&lt;br /&gt;
├── bin&lt;br /&gt;
│   ├── console&lt;br /&gt;
│   └── setup&lt;br /&gt;
├── Gemfile&lt;br /&gt;
├── Gemfile.lock&lt;br /&gt;
├── I704.gemspec&lt;br /&gt;
├── lib&lt;br /&gt;
│   ├── I704&lt;br /&gt;
│   │   ├── bank_account.rb&lt;br /&gt;
│   │   └── version.rb&lt;br /&gt;
│   └── I704.rb&lt;br /&gt;
├── LICENSE.txt&lt;br /&gt;
├── Rakefile&lt;br /&gt;
├── README.md&lt;br /&gt;
└── test&lt;br /&gt;
    ├── I704&lt;br /&gt;
    │   └── bank_account_test.rb&lt;br /&gt;
    ├── I704_test.rb&lt;br /&gt;
    └── test_helper.rb&lt;br /&gt;
&lt;br /&gt;
5 directories, 14 files&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rule of thumb&#039;&#039;&#039;: put your code into the &amp;lt;code&amp;gt;lib/YOUR_PROJECT_NAME&amp;lt;/code&amp;gt; directory, one file per class (like &amp;lt;code&amp;gt;bank_account.rb&amp;lt;/code&amp;gt; in the example above)&lt;br /&gt;
&lt;br /&gt;
= Reference material: making HTTP requests in Ruby =&lt;br /&gt;
&lt;br /&gt;
# Require the &amp;quot;net/http&amp;quot; library in your program&lt;br /&gt;
# Use that library to make HTTP requests for fetching the HTML code behind a website&lt;br /&gt;
# 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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;net/http&#039;&lt;br /&gt;
google_html = Net::HTTP.get(URI(&#039;http://google.com&#039;))&lt;br /&gt;
# &amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=\&amp;quot;content-type\&amp;quot; content=\&amp;quot;text/html;charset=utf-8\&amp;quot;&amp;gt;\n&amp;lt;TITLE&amp;gt;302 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;\n&amp;lt;H1&amp;gt;302 Moved&amp;lt;/H1&amp;gt;\nThe document has moved\n&amp;lt;A HREF=\&amp;quot;http://www.google.ee/?gfe_rd=cr&amp;amp;amp;ei=o6KlWKCGA9Oq8wfoq5OQAg\&amp;quot;&amp;gt;here&amp;lt;/A&amp;gt;.\r\n&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;\r\n&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Now you can work with the HTML returned by the server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: parsing HTML using Nokogiri =&lt;br /&gt;
&lt;br /&gt;
Once you have received some HTML from a web server, you most likely want to analyze it.  Use the library nokogiri for this.  You can find installation instructions and tutorials here: http://www.nokogiri.org/tutorials/&lt;br /&gt;
&lt;br /&gt;
= Reference material: map and reduce =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# map: takes a list and a block&lt;br /&gt;
#    returns: a list, where each element of the original list has been&lt;br /&gt;
#    passed to block&lt;br /&gt;
#&lt;br /&gt;
# reduce: takes list, an initial value and a block&lt;br /&gt;
#    returns: block applied to initial value and each element of the list&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = numbers.reduce(0) do |current_sum, number|&lt;br /&gt;
  current_sum + number&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;Sum: #{sum}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
####&lt;br /&gt;
&lt;br /&gt;
numbers = [1, 2, 3]&lt;br /&gt;
sum = 0&lt;br /&gt;
numbers.each do |number|&lt;br /&gt;
  sum = sum + number&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118283</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=118283"/>
		<updated>2017-03-02T10:10:33Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: /* Reference material */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= Lecture Recordings =&lt;br /&gt;
&lt;br /&gt;
You can find recordings of the lectures here:&lt;br /&gt;
&lt;br /&gt;
https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
* [https://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-select Ruby Enumerable]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  class Error &amp;lt; StandardError; end&lt;br /&gt;
  class BalanceNegative &amp;lt; Error; end&lt;br /&gt;
&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increse_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &#039;Not enough money!&#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increse_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def setup&lt;br /&gt;
    @account = BankAccount.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_decreses_the_balance_when_withdrawing_money&lt;br /&gt;
    @account.deposit!(101).withdraw!(50)&lt;br /&gt;
    assert_equal 51, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_increses_the_balance_when_depositing_money&lt;br /&gt;
    @account.deposit!(196583)&lt;br /&gt;
    assert_equal 196583, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_account_does_not_go_in_to_negative&lt;br /&gt;
    assert_raises BankAccount::BalanceNegative do&lt;br /&gt;
      @account.deposit!(1).withdraw!(20)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_it_raises_bank_account_error_when_withdrawing_too_much_money&lt;br /&gt;
    @account.deposit!(1).withdraw!(20)&lt;br /&gt;
  rescue BankAccount::Error =&amp;gt; err&lt;br /&gt;
    assert true, &#039;everything is ok&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-16 Lecture and Lab: using modules and bundler =&lt;br /&gt;
&lt;br /&gt;
== Modules in Ruby ==&lt;br /&gt;
&lt;br /&gt;
* Modules are a collection of methods that can be included in another class&lt;br /&gt;
* By including the module in a class, objects belonging to that class get access to the methods defined in the module&lt;br /&gt;
* If multiple included modules define the same method, the method from the module that was included last counts&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
module ModuleExample&lt;br /&gt;
  def foo&lt;br /&gt;
    &#039;foo&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Including modules binds their methods to the class instances&lt;br /&gt;
# Extending modules binds their methods to the class itself&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  include ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Book&lt;br /&gt;
  extend ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Person.foo     # =&amp;gt; NoMethodError: undefined method `foo&#039; for Person:Class&lt;br /&gt;
Person.new.foo # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.foo       # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.new.foo   # =&amp;gt; NoMethodError: undefined method `foo&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
== Structuring a project with bundler ==&lt;br /&gt;
&lt;br /&gt;
* bundler is a Ruby program for managing dependencies in your ruby project&lt;br /&gt;
* bundler also helps you with creating a template for a project&lt;br /&gt;
* redistributable projects/libraries are called &amp;quot;gems&amp;quot; in ruby&lt;br /&gt;
* You can start your own project by running &amp;lt;code&amp;gt;bundle gem YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt;.  Replace &amp;lt;code&amp;gt;YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt; with the lower-cased name of your project.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example structure of a project, as generated by bundler&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ tree&lt;br /&gt;
.&lt;br /&gt;
├── bin&lt;br /&gt;
│   ├── console&lt;br /&gt;
│   └── setup&lt;br /&gt;
├── Gemfile&lt;br /&gt;
├── Gemfile.lock&lt;br /&gt;
├── I704.gemspec&lt;br /&gt;
├── lib&lt;br /&gt;
│   ├── I704&lt;br /&gt;
│   │   ├── bank_account.rb&lt;br /&gt;
│   │   └── version.rb&lt;br /&gt;
│   └── I704.rb&lt;br /&gt;
├── LICENSE.txt&lt;br /&gt;
├── Rakefile&lt;br /&gt;
├── README.md&lt;br /&gt;
└── test&lt;br /&gt;
    ├── I704&lt;br /&gt;
    │   └── bank_account_test.rb&lt;br /&gt;
    ├── I704_test.rb&lt;br /&gt;
    └── test_helper.rb&lt;br /&gt;
&lt;br /&gt;
5 directories, 14 files&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rule of thumb&#039;&#039;&#039;: put your code into the &amp;lt;code&amp;gt;lib/YOUR_PROJECT_NAME&amp;lt;/code&amp;gt; directory, one file per class (like &amp;lt;code&amp;gt;bank_account.rb&amp;lt;/code&amp;gt; in the example above)&lt;br /&gt;
&lt;br /&gt;
= Reference material: making HTTP requests in Ruby =&lt;br /&gt;
&lt;br /&gt;
# Require the &amp;quot;net/http&amp;quot; library in your program&lt;br /&gt;
# Use that library to make HTTP requests for fetching the HTML code behind a website&lt;br /&gt;
# 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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;net/http&#039;&lt;br /&gt;
google_html = Net::HTTP.get(URI(&#039;http://google.com&#039;))&lt;br /&gt;
# &amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=\&amp;quot;content-type\&amp;quot; content=\&amp;quot;text/html;charset=utf-8\&amp;quot;&amp;gt;\n&amp;lt;TITLE&amp;gt;302 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;\n&amp;lt;H1&amp;gt;302 Moved&amp;lt;/H1&amp;gt;\nThe document has moved\n&amp;lt;A HREF=\&amp;quot;http://www.google.ee/?gfe_rd=cr&amp;amp;amp;ei=o6KlWKCGA9Oq8wfoq5OQAg\&amp;quot;&amp;gt;here&amp;lt;/A&amp;gt;.\r\n&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;\r\n&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Now you can work with the HTML returned by the server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: parsing HTML using Nokogiri =&lt;br /&gt;
&lt;br /&gt;
Once you have received some HTML from a web server, you most likely want to analyze it.  Use the library nokogiri for this.  You can find installation instructions and tutorials here: http://www.nokogiri.org/tutorials/&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117779</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117779"/>
		<updated>2017-02-16T13:27:11Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: Add note about nokogiri&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= Lecture Recordings =&lt;br /&gt;
&lt;br /&gt;
You can find recordings of the lectures here:&lt;br /&gt;
&lt;br /&gt;
https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  class Error &amp;lt; StandardError; end&lt;br /&gt;
  class BalanceNegative &amp;lt; Error; end&lt;br /&gt;
&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increse_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &#039;Not enough money!&#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increse_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def setup&lt;br /&gt;
    @account = BankAccount.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_decreses_the_balance_when_withdrawing_money&lt;br /&gt;
    @account.deposit!(101).withdraw!(50)&lt;br /&gt;
    assert_equal 51, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_increses_the_balance_when_depositing_money&lt;br /&gt;
    @account.deposit!(196583)&lt;br /&gt;
    assert_equal 196583, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_account_does_not_go_in_to_negative&lt;br /&gt;
    assert_raises BankAccount::BalanceNegative do&lt;br /&gt;
      @account.deposit!(1).withdraw!(20)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_it_raises_bank_account_error_when_withdrawing_too_much_money&lt;br /&gt;
    @account.deposit!(1).withdraw!(20)&lt;br /&gt;
  rescue BankAccount::Error =&amp;gt; err&lt;br /&gt;
    assert true, &#039;everything is ok&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-16 Lecture and Lab: using modules and bundler =&lt;br /&gt;
&lt;br /&gt;
== Modules in Ruby ==&lt;br /&gt;
&lt;br /&gt;
* Modules are a collection of methods that can be included in another class&lt;br /&gt;
* By including the module in a class, objects belonging to that class get access to the methods defined in the module&lt;br /&gt;
* If multiple included modules define the same method, the method from the module that was included last counts&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
module ModuleExample&lt;br /&gt;
  def foo&lt;br /&gt;
    &#039;foo&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Including modules binds their methods to the class instances&lt;br /&gt;
# Extending modules binds their methods to the class itself&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  include ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Book&lt;br /&gt;
  extend ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Person.foo     # =&amp;gt; NoMethodError: undefined method `foo&#039; for Person:Class&lt;br /&gt;
Person.new.foo # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.foo       # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.new.foo   # =&amp;gt; NoMethodError: undefined method `foo&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
== Structuring a project with bundler ==&lt;br /&gt;
&lt;br /&gt;
* bundler is a Ruby program for managing dependencies in your ruby project&lt;br /&gt;
* bundler also helps you with creating a template for a project&lt;br /&gt;
* redistributable projects/libraries are called &amp;quot;gems&amp;quot; in ruby&lt;br /&gt;
* You can start your own project by running &amp;lt;code&amp;gt;bundle gem YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt;.  Replace &amp;lt;code&amp;gt;YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt; with the lower-cased name of your project.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example structure of a project, as generated by bundler&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ tree&lt;br /&gt;
.&lt;br /&gt;
├── bin&lt;br /&gt;
│   ├── console&lt;br /&gt;
│   └── setup&lt;br /&gt;
├── Gemfile&lt;br /&gt;
├── Gemfile.lock&lt;br /&gt;
├── I704.gemspec&lt;br /&gt;
├── lib&lt;br /&gt;
│   ├── I704&lt;br /&gt;
│   │   ├── bank_account.rb&lt;br /&gt;
│   │   └── version.rb&lt;br /&gt;
│   └── I704.rb&lt;br /&gt;
├── LICENSE.txt&lt;br /&gt;
├── Rakefile&lt;br /&gt;
├── README.md&lt;br /&gt;
└── test&lt;br /&gt;
    ├── I704&lt;br /&gt;
    │   └── bank_account_test.rb&lt;br /&gt;
    ├── I704_test.rb&lt;br /&gt;
    └── test_helper.rb&lt;br /&gt;
&lt;br /&gt;
5 directories, 14 files&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rule of thumb&#039;&#039;&#039;: put your code into the &amp;lt;code&amp;gt;lib/YOUR_PROJECT_NAME&amp;lt;/code&amp;gt; directory, one file per class (like &amp;lt;code&amp;gt;bank_account.rb&amp;lt;/code&amp;gt; in the example above)&lt;br /&gt;
&lt;br /&gt;
= Reference material: making HTTP requests in Ruby =&lt;br /&gt;
&lt;br /&gt;
# Require the &amp;quot;net/http&amp;quot; library in your program&lt;br /&gt;
# Use that library to make HTTP requests for fetching the HTML code behind a website&lt;br /&gt;
# 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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;net/http&#039;&lt;br /&gt;
google_html = Net::HTTP.get(URI(&#039;http://google.com&#039;))&lt;br /&gt;
# &amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=\&amp;quot;content-type\&amp;quot; content=\&amp;quot;text/html;charset=utf-8\&amp;quot;&amp;gt;\n&amp;lt;TITLE&amp;gt;302 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;\n&amp;lt;H1&amp;gt;302 Moved&amp;lt;/H1&amp;gt;\nThe document has moved\n&amp;lt;A HREF=\&amp;quot;http://www.google.ee/?gfe_rd=cr&amp;amp;amp;ei=o6KlWKCGA9Oq8wfoq5OQAg\&amp;quot;&amp;gt;here&amp;lt;/A&amp;gt;.\r\n&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;\r\n&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Now you can work with the HTML returned by the server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: parsing HTML using Nokogiri =&lt;br /&gt;
&lt;br /&gt;
Once you have received some HTML from a web server, you most likely want to analyze it.  Use the library nokogiri for this.  You can find installation instructions and tutorials here: http://www.nokogiri.org/tutorials/&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117778</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117778"/>
		<updated>2017-02-16T13:21:50Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= Lecture Recordings =&lt;br /&gt;
&lt;br /&gt;
You can find recordings of the lectures here:&lt;br /&gt;
&lt;br /&gt;
https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  class Error &amp;lt; StandardError; end&lt;br /&gt;
  class BalanceNegative &amp;lt; Error; end&lt;br /&gt;
&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increse_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &#039;Not enough money!&#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increse_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def setup&lt;br /&gt;
    @account = BankAccount.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_decreses_the_balance_when_withdrawing_money&lt;br /&gt;
    @account.deposit!(101).withdraw!(50)&lt;br /&gt;
    assert_equal 51, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_increses_the_balance_when_depositing_money&lt;br /&gt;
    @account.deposit!(196583)&lt;br /&gt;
    assert_equal 196583, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_account_does_not_go_in_to_negative&lt;br /&gt;
    assert_raises BankAccount::BalanceNegative do&lt;br /&gt;
      @account.deposit!(1).withdraw!(20)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_it_raises_bank_account_error_when_withdrawing_too_much_money&lt;br /&gt;
    @account.deposit!(1).withdraw!(20)&lt;br /&gt;
  rescue BankAccount::Error =&amp;gt; err&lt;br /&gt;
    assert true, &#039;everything is ok&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-16 Lecture and Lab: using modules and bundler =&lt;br /&gt;
&lt;br /&gt;
== Modules in Ruby ==&lt;br /&gt;
&lt;br /&gt;
* Modules are a collection of methods that can be included in another class&lt;br /&gt;
* By including the module in a class, objects belonging to that class get access to the methods defined in the module&lt;br /&gt;
* If multiple included modules define the same method, the method from the module that was included last counts&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
module ModuleExample&lt;br /&gt;
  def foo&lt;br /&gt;
    &#039;foo&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Including modules binds their methods to the class instances&lt;br /&gt;
# Extending modules binds their methods to the class itself&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  include ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Book&lt;br /&gt;
  extend ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Person.foo     # =&amp;gt; NoMethodError: undefined method `foo&#039; for Person:Class&lt;br /&gt;
Person.new.foo # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.foo       # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.new.foo   # =&amp;gt; NoMethodError: undefined method `foo&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
== Structuring a project with bundler ==&lt;br /&gt;
&lt;br /&gt;
* bundler is a Ruby program for managing dependencies in your ruby project&lt;br /&gt;
* bundler also helps you with creating a template for a project&lt;br /&gt;
* redistributable projects/libraries are called &amp;quot;gems&amp;quot; in ruby&lt;br /&gt;
* You can start your own project by running &amp;lt;code&amp;gt;bundle gem YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt;.  Replace &amp;lt;code&amp;gt;YOUR_PROJECT_NAME_HERE&amp;lt;/code&amp;gt; with the lower-cased name of your project.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example structure of a project, as generated by bundler&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ tree&lt;br /&gt;
.&lt;br /&gt;
├── bin&lt;br /&gt;
│   ├── console&lt;br /&gt;
│   └── setup&lt;br /&gt;
├── Gemfile&lt;br /&gt;
├── Gemfile.lock&lt;br /&gt;
├── I704.gemspec&lt;br /&gt;
├── lib&lt;br /&gt;
│   ├── I704&lt;br /&gt;
│   │   ├── bank_account.rb&lt;br /&gt;
│   │   └── version.rb&lt;br /&gt;
│   └── I704.rb&lt;br /&gt;
├── LICENSE.txt&lt;br /&gt;
├── Rakefile&lt;br /&gt;
├── README.md&lt;br /&gt;
└── test&lt;br /&gt;
    ├── I704&lt;br /&gt;
    │   └── bank_account_test.rb&lt;br /&gt;
    ├── I704_test.rb&lt;br /&gt;
    └── test_helper.rb&lt;br /&gt;
&lt;br /&gt;
5 directories, 14 files&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rule of thumb&#039;&#039;&#039;: put your code into the &amp;lt;code&amp;gt;lib/YOUR_PROJECT_NAME&amp;lt;/code&amp;gt; directory, one file per class (like &amp;lt;code&amp;gt;bank_account.rb&amp;lt;/code&amp;gt; in the example above)&lt;br /&gt;
&lt;br /&gt;
= Reference material: making HTTP requests in Ruby =&lt;br /&gt;
&lt;br /&gt;
# Require the &amp;quot;net/http&amp;quot; library in your program&lt;br /&gt;
# Use that library to make HTTP requests for fetching the HTML code behind a website&lt;br /&gt;
# 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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;net/http&#039;&lt;br /&gt;
google_html = Net::HTTP.get(URI(&#039;http://google.com&#039;))&lt;br /&gt;
# &amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=\&amp;quot;content-type\&amp;quot; content=\&amp;quot;text/html;charset=utf-8\&amp;quot;&amp;gt;\n&amp;lt;TITLE&amp;gt;302 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;\n&amp;lt;H1&amp;gt;302 Moved&amp;lt;/H1&amp;gt;\nThe document has moved\n&amp;lt;A HREF=\&amp;quot;http://www.google.ee/?gfe_rd=cr&amp;amp;amp;ei=o6KlWKCGA9Oq8wfoq5OQAg\&amp;quot;&amp;gt;here&amp;lt;/A&amp;gt;.\r\n&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;\r\n&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Now you can work with the HTML returned by the server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117777</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117777"/>
		<updated>2017-02-16T13:15:35Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: Add example about modules&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= Lecture Recordings =&lt;br /&gt;
&lt;br /&gt;
You can find recordings of the lectures here:&lt;br /&gt;
&lt;br /&gt;
https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  class Error &amp;lt; StandardError; end&lt;br /&gt;
  class BalanceNegative &amp;lt; Error; end&lt;br /&gt;
&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increse_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &#039;Not enough money!&#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increse_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def setup&lt;br /&gt;
    @account = BankAccount.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_decreses_the_balance_when_withdrawing_money&lt;br /&gt;
    @account.deposit!(101).withdraw!(50)&lt;br /&gt;
    assert_equal 51, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_increses_the_balance_when_depositing_money&lt;br /&gt;
    @account.deposit!(196583)&lt;br /&gt;
    assert_equal 196583, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_account_does_not_go_in_to_negative&lt;br /&gt;
    assert_raises BankAccount::BalanceNegative do&lt;br /&gt;
      @account.deposit!(1).withdraw!(20)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_it_raises_bank_account_error_when_withdrawing_too_much_money&lt;br /&gt;
    @account.deposit!(1).withdraw!(20)&lt;br /&gt;
  rescue BankAccount::Error =&amp;gt; err&lt;br /&gt;
    assert true, &#039;everything is ok&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-16 Lecture and Lab: using modules and bundler =&lt;br /&gt;
&lt;br /&gt;
== Modules in Ruby ==&lt;br /&gt;
&lt;br /&gt;
* Modules are a collection of methods that can be included in another class&lt;br /&gt;
* By including the module in a class, objects belonging to that class get access to the methods defined in the module&lt;br /&gt;
* If multiple included modules define the same method, the method from the module that was included last counts&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
module ModuleExample&lt;br /&gt;
  def foo&lt;br /&gt;
    &#039;foo&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Including modules binds their methods to the class instances&lt;br /&gt;
# Extending modules binds their methods to the class itself&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  include ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Book&lt;br /&gt;
  extend ModuleExample&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Person.foo     # =&amp;gt; NoMethodError: undefined method `foo&#039; for Person:Class&lt;br /&gt;
Person.new.foo # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.foo       # =&amp;gt; &#039;foo&#039;&lt;br /&gt;
Book.new.foo   # =&amp;gt; NoMethodError: undefined method `foo&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
= Reference material: making HTTP requests in Ruby =&lt;br /&gt;
&lt;br /&gt;
# Require the &amp;quot;net/http&amp;quot; library in your program&lt;br /&gt;
# Use that library to make HTTP requests for fetching the HTML code behind a website&lt;br /&gt;
# 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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;net/http&#039;&lt;br /&gt;
google_html = Net::HTTP.get(URI(&#039;http://google.com&#039;))&lt;br /&gt;
# &amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=\&amp;quot;content-type\&amp;quot; content=\&amp;quot;text/html;charset=utf-8\&amp;quot;&amp;gt;\n&amp;lt;TITLE&amp;gt;302 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;\n&amp;lt;H1&amp;gt;302 Moved&amp;lt;/H1&amp;gt;\nThe document has moved\n&amp;lt;A HREF=\&amp;quot;http://www.google.ee/?gfe_rd=cr&amp;amp;amp;ei=o6KlWKCGA9Oq8wfoq5OQAg\&amp;quot;&amp;gt;here&amp;lt;/A&amp;gt;.\r\n&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;\r\n&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Now you can work with the HTML returned by the server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117776</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117776"/>
		<updated>2017-02-16T13:10:46Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: Fix markup&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= Lecture Recordings =&lt;br /&gt;
&lt;br /&gt;
You can find recordings of the lectures here:&lt;br /&gt;
&lt;br /&gt;
https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  class Error &amp;lt; StandardError; end&lt;br /&gt;
  class BalanceNegative &amp;lt; Error; end&lt;br /&gt;
&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increse_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &#039;Not enough money!&#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increse_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def setup&lt;br /&gt;
    @account = BankAccount.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_decreses_the_balance_when_withdrawing_money&lt;br /&gt;
    @account.deposit!(101).withdraw!(50)&lt;br /&gt;
    assert_equal 51, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_increses_the_balance_when_depositing_money&lt;br /&gt;
    @account.deposit!(196583)&lt;br /&gt;
    assert_equal 196583, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_account_does_not_go_in_to_negative&lt;br /&gt;
    assert_raises BankAccount::BalanceNegative do&lt;br /&gt;
      @account.deposit!(1).withdraw!(20)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_it_raises_bank_account_error_when_withdrawing_too_much_money&lt;br /&gt;
    @account.deposit!(1).withdraw!(20)&lt;br /&gt;
  rescue BankAccount::Error =&amp;gt; err&lt;br /&gt;
    assert true, &#039;everything is ok&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: making HTTP requests in Ruby =&lt;br /&gt;
&lt;br /&gt;
# Require the &amp;quot;net/http&amp;quot; library in your program&lt;br /&gt;
# Use that library to make HTTP requests for fetching the HTML code behind a website&lt;br /&gt;
# 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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;net/http&#039;&lt;br /&gt;
google_html = Net::HTTP.get(URI(&#039;http://google.com&#039;))&lt;br /&gt;
# &amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=\&amp;quot;content-type\&amp;quot; content=\&amp;quot;text/html;charset=utf-8\&amp;quot;&amp;gt;\n&amp;lt;TITLE&amp;gt;302 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;\n&amp;lt;H1&amp;gt;302 Moved&amp;lt;/H1&amp;gt;\nThe document has moved\n&amp;lt;A HREF=\&amp;quot;http://www.google.ee/?gfe_rd=cr&amp;amp;amp;ei=o6KlWKCGA9Oq8wfoq5OQAg\&amp;quot;&amp;gt;here&amp;lt;/A&amp;gt;.\r\n&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;\r\n&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Now you can work with the HTML returned by the server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117775</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117775"/>
		<updated>2017-02-16T13:09:42Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: Add information about making HTTP requests&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= Lecture Recordings =&lt;br /&gt;
&lt;br /&gt;
You can find recordings of the lectures here:&lt;br /&gt;
&lt;br /&gt;
https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  class Error &amp;lt; StandardError; end&lt;br /&gt;
  class BalanceNegative &amp;lt; Error; end&lt;br /&gt;
&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increse_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &#039;Not enough money!&#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increse_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def setup&lt;br /&gt;
    @account = BankAccount.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_decreses_the_balance_when_withdrawing_money&lt;br /&gt;
    @account.deposit!(101).withdraw!(50)&lt;br /&gt;
    assert_equal 51, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_increses_the_balance_when_depositing_money&lt;br /&gt;
    @account.deposit!(196583)&lt;br /&gt;
    assert_equal 196583, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_account_does_not_go_in_to_negative&lt;br /&gt;
    assert_raises BankAccount::BalanceNegative do&lt;br /&gt;
      @account.deposit!(1).withdraw!(20)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_it_raises_bank_account_error_when_withdrawing_too_much_money&lt;br /&gt;
    @account.deposit!(1).withdraw!(20)&lt;br /&gt;
  rescue BankAccount::Error =&amp;gt; err&lt;br /&gt;
    assert true, &#039;everything is ok&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Reference material: making HTTP requests in Ruby =&lt;br /&gt;
&lt;br /&gt;
1. Require the &amp;quot;net/http&amp;quot; library in your program&lt;br /&gt;
2. Use that library to make HTTP requests for fetching the HTML code behind a website&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;net/http&#039;&lt;br /&gt;
google_html = Net::HTTP.get(URI(&#039;http://google.com&#039;))&lt;br /&gt;
# &amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=\&amp;quot;content-type\&amp;quot; content=\&amp;quot;text/html;charset=utf-8\&amp;quot;&amp;gt;\n&amp;lt;TITLE&amp;gt;302 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;\n&amp;lt;H1&amp;gt;302 Moved&amp;lt;/H1&amp;gt;\nThe document has moved\n&amp;lt;A HREF=\&amp;quot;http://www.google.ee/?gfe_rd=cr&amp;amp;amp;ei=o6KlWKCGA9Oq8wfoq5OQAg\&amp;quot;&amp;gt;here&amp;lt;/A&amp;gt;.\r\n&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;\r\n&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Now you can work with the HTML returned by the server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117762</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117762"/>
		<updated>2017-02-16T10:20:55Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: Show detailed exception handling&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= Lecture Recordings =&lt;br /&gt;
&lt;br /&gt;
You can find recordings of the lectures here:&lt;br /&gt;
&lt;br /&gt;
https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  class Error &amp;lt; StandardError; end&lt;br /&gt;
  class BalanceNegative &amp;lt; Error; end&lt;br /&gt;
&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increse_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &#039;Not enough money!&#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increse_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def setup&lt;br /&gt;
    @account = BankAccount.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_decreses_the_balance_when_withdrawing_money&lt;br /&gt;
    @account.deposit!(101).withdraw!(50)&lt;br /&gt;
    assert_equal 51, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_increses_the_balance_when_depositing_money&lt;br /&gt;
    @account.deposit!(196583)&lt;br /&gt;
    assert_equal 196583, @account.balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_if_account_does_not_go_in_to_negative&lt;br /&gt;
    assert_raises BankAccount::BalanceNegative do&lt;br /&gt;
      @account.deposit!(1).withdraw!(20)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def test_it_raises_bank_account_error_when_withdrawing_too_much_money&lt;br /&gt;
    @account.deposit!(1).withdraw!(20)&lt;br /&gt;
  rescue BankAccount::Error =&amp;gt; err&lt;br /&gt;
    assert true, &#039;everything is ok&#039;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117744</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117744"/>
		<updated>2017-02-15T12:19:36Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: Add link to lecture videos&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= Lecture Recordings =&lt;br /&gt;
&lt;br /&gt;
You can find recordings of the lectures here:&lt;br /&gt;
&lt;br /&gt;
https://echo360.e-ope.ee/ess/portal/section/b02ab032-010b-4111-a53d-2f5a47db1fdd&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increase_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &amp;quot;not enough money on account&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increase_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# in Java: class BankAcountTest extends minitest.Test&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def test_it_decreases_the_balance_when_withdrawing_money&lt;br /&gt;
    account = BankAccount.new&lt;br /&gt;
                         .deposit!(100)&lt;br /&gt;
                         .withdraw!(49)&lt;br /&gt;
&lt;br /&gt;
    assert_equal 51, account.balance&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117681</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117681"/>
		<updated>2017-02-09T11:30:34Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
require &#039;minitest/autorun&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increase_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &amp;quot;not enough money on account&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increase_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# in Java: class BankAcountTest extends minitest.Test&lt;br /&gt;
class BankAccountTest &amp;lt; Minitest::Test&lt;br /&gt;
  def test_it_decreases_the_balance_when_withdrawing_money&lt;br /&gt;
    account = BankAccount.new&lt;br /&gt;
                         .deposit!(100)&lt;br /&gt;
                         .withdraw!(49)&lt;br /&gt;
&lt;br /&gt;
    assert_equal 51, account.balance&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117680</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117680"/>
		<updated>2017-02-09T11:15:06Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: Fix markup&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increase_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &amp;quot;not enough money on account&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increase_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117679</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117679"/>
		<updated>2017-02-09T11:14:20Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: Share bank account example&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 2017-02-09 Lecture&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
require &#039;minitest&#039;&lt;br /&gt;
&lt;br /&gt;
class BankAccount&lt;br /&gt;
  def initialize&lt;br /&gt;
    @balance = 0&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def deposit!(amount)&lt;br /&gt;
    increase_balance!(amount)&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def withdraw!(amount)&lt;br /&gt;
    if enough_money?(amount)&lt;br /&gt;
      reduce_balance!(amount)&lt;br /&gt;
    else&lt;br /&gt;
      raise StandardError, &amp;quot;not enough money on account&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    self&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def balance&lt;br /&gt;
    @balance&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def enough_money?(amount)&lt;br /&gt;
    balance &amp;gt;= amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def increase_balance!(amount)&lt;br /&gt;
    @balance = @balance + amount&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def reduce_balance!(amount)&lt;br /&gt;
    @balance = @balance - amount&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=English&amp;diff=117601</id>
		<title>English</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=English&amp;diff=117601"/>
		<updated>2017-02-02T20:36:50Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: Add link to Ruby course&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__NOTOC__&lt;br /&gt;
&lt;br /&gt;
Welcome to Estonian IT College wikis English version.&lt;br /&gt;
&lt;br /&gt;
Our official web page&lt;br /&gt;
&lt;br /&gt;
==Courses==&lt;br /&gt;
&lt;br /&gt;
* [https://wiki.itcollege.ee/index.php/I253_Presessional_Informatics Presessional course in Informatics]&lt;br /&gt;
* [https://wiki.itcollege.ee/index.php/Category:I600_Introduction_to_Computers_and_Informatics Introduction to Computers and Informatics]&lt;br /&gt;
** [[Exam help]]&lt;br /&gt;
* [[Logic and Discrete Mathematics Exam Help]]&lt;br /&gt;
* [[Operating systems]]&lt;br /&gt;
* [https://wiki.itcollege.ee/index.php/Category:I704_Ruby I704 Ruby (Spring 2017)]&lt;br /&gt;
* [https://wiki.itcollege.ee/index.php/Category:I703_Python Python]&lt;br /&gt;
* [https://wiki.itcollege.ee/index.php/Category:I702_Web_Application_Programming Web Application Programming]&lt;br /&gt;
* [https://wiki.itcollege.ee/index.php/Category:I802_Firewalls_and_VPN_IPSec Firewalls and VPN/IPSec]&lt;br /&gt;
&lt;br /&gt;
* [https://wiki.itcollege.ee/index.php/Category:Ideas Ideas for research project or thesis]&lt;br /&gt;
* [[I803 IT Infrastructure services]]&lt;br /&gt;
* [[Basics of C/C++ Programming]]&lt;br /&gt;
&lt;br /&gt;
2nd year Spring semester&lt;br /&gt;
&lt;br /&gt;
* [https://wiki.itcollege.ee/index.php/Category:I804_Linux_Windows_administration Linux/Windows administration]&lt;br /&gt;
* [https://wiki.itcollege.ee/index.php/Category:I805_Authentication_and_Authorization Authentication and Authorization]&lt;br /&gt;
&lt;br /&gt;
Misc&lt;br /&gt;
&lt;br /&gt;
* [https://wiki.itcollege.ee/index.php/Category:Lecturer_FAQ Lecturer FAQ]&lt;br /&gt;
&lt;br /&gt;
==Quickstart==&lt;br /&gt;
&lt;br /&gt;
This section is for freshmen who want to get up to speed with latest open-source technology.&lt;br /&gt;
&lt;br /&gt;
* [https://wiki.itcollege.ee/index.php/User:Akerge CSE survival guide]&lt;br /&gt;
* [[Getting started with Ubuntu]]&lt;br /&gt;
* [[Getting started with Raspberry Pi]]&lt;br /&gt;
* [[Accessing a virtual machine via SSH connection]]&lt;br /&gt;
* [[Setting up SSH access to enos.itcollege.ee]]&lt;br /&gt;
* [[Getting started with GCC]]&lt;br /&gt;
&lt;br /&gt;
==International Projects==&lt;br /&gt;
&lt;br /&gt;
* [[Deploying IT Infrastructure Solutions| Erasmus intensive program &amp;quot;Deploying IT Infrastructure Solutions&amp;quot;]]&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117600</id>
		<title>Category:I704 Ruby</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Category:I704_Ruby&amp;diff=117600"/>
		<updated>2017-02-02T20:35:22Z</updated>

		<summary type="html">&lt;p&gt;Dhamidi: Created page with &amp;quot;= About this course = This course teaches the Ruby programming language.  By the end of the course you&amp;#039;ll hopefully have a good understanding of:  * the basics of Ruby,  * too...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= About this course =&lt;br /&gt;
This course teaches the Ruby programming language.  By the end of the course you&#039;ll hopefully have a good understanding of:&lt;br /&gt;
&lt;br /&gt;
* the basics of Ruby,&lt;br /&gt;
&lt;br /&gt;
* tools commonly used in the Ruby ecosystem,&lt;br /&gt;
&lt;br /&gt;
* written a few small Ruby applications,&lt;br /&gt;
&lt;br /&gt;
* know about unit testing,&lt;br /&gt;
&lt;br /&gt;
* know how to use third-party code (Ruby gems),&lt;br /&gt;
&lt;br /&gt;
* know how to write web applications using Ruby.&lt;br /&gt;
&lt;br /&gt;
= About yourself =&lt;br /&gt;
To help me make this course interesting for you and meet your&lt;br /&gt;
expectations, please fill out this survey if you haven&#039;t done so&lt;br /&gt;
already:&lt;br /&gt;
&lt;br /&gt;
http://bit.ly/2jxwrs8&lt;br /&gt;
&lt;br /&gt;
= Reference material =&lt;br /&gt;
Here you find a list of useful links to things that have been&lt;br /&gt;
mentioned or discussed during the lectures:&lt;br /&gt;
&lt;br /&gt;
* [https://learnxinyminutes.com/docs/ruby/ Ruby syntax reference]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bbatsov/rubocop Rubocop: analyzes and formats your code]&lt;br /&gt;
&lt;br /&gt;
* [http://learn.onemonth.com/ruby-tutorial-map-vs-each Map vs Each explained]&lt;br /&gt;
&lt;br /&gt;
= Editors/IDEs =&lt;br /&gt;
&lt;br /&gt;
* [http://www.jesshamrick.com/2012/09/10/absolute-beginners-guide-to-emacs Absolute beginner&#039;s guide to Emacs]&lt;br /&gt;
&lt;br /&gt;
= Grading =&lt;br /&gt;
Students develop several small projects during the lectures and&lt;br /&gt;
independent study.  At the end of the course students pick one of&lt;br /&gt;
their projects and are assigned a feature request to implement in&lt;br /&gt;
their project and a set of questions about the code in their project.&lt;br /&gt;
&lt;br /&gt;
Points are awarded for the following:&lt;br /&gt;
&lt;br /&gt;
* 50 points for a working implementation of the feature request&lt;br /&gt;
&lt;br /&gt;
* 40 points for an implementation of the feature request that works only for expected inputs&lt;br /&gt;
&lt;br /&gt;
* 20 points for a running unfinished implementation (i.e. feature not fully implemented, but the program still runs)&lt;br /&gt;
&lt;br /&gt;
* 10 points for an unfinished implementation (i.e. feature not fully implement and the program does not run).&lt;br /&gt;
&lt;br /&gt;
* 20 points for providing automated tests for their implementation&lt;br /&gt;
&lt;br /&gt;
* 10 points for proper use of version control&lt;br /&gt;
&lt;br /&gt;
* 10 points for adhering to common Ruby coding standards&lt;br /&gt;
&lt;br /&gt;
* 20 points for answering 80% of the questions correctly&lt;br /&gt;
&lt;br /&gt;
The student needs at least 60 points in order to pass the course.&lt;br /&gt;
&lt;br /&gt;
= 2017-02-02: Lecture and Lab =&lt;br /&gt;
== Analyzing bank statements ==&lt;br /&gt;
Given the following contents of a file called &amp;lt;tt&amp;gt;input.csv&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    transaction_id,date,amount,credit&lt;br /&gt;
    1,2017-02-02 12:40,1.30,debit&lt;br /&gt;
    2,2017-02-02 12:55,2.50,debit&lt;br /&gt;
    3,2017-02-02 13:00,1.00,credit&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: find the amount of money left on your bank account.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
# read the data line by line&lt;br /&gt;
# analyze each line to find out whether it&#039;s credit or debit and the amount of money&lt;br /&gt;
# add all the debit transaction amounts (money lost)&lt;br /&gt;
# add all the credit transaction amounts (money gained)&lt;br /&gt;
# Output money gained - money lost&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Our code so far&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot; line=&amp;quot;line&amp;quot;&amp;gt;&lt;br /&gt;
File.open(&#039;input.csv&#039;, &#039;r&#039;) do |the_file|&lt;br /&gt;
  lines = the_file.readlines.map do |line|&lt;br /&gt;
    line.chomp.split(&#039;,&#039;)&lt;br /&gt;
  end&lt;br /&gt;
  lines = lines[1..-1]&lt;br /&gt;
  debit_total = &#039;&#039;&lt;br /&gt;
  lines.each do |line|&lt;br /&gt;
    debit_total = debit_total + line[2]&lt;br /&gt;
  end&lt;br /&gt;
  puts debit_total&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dhamidi</name></author>
	</entry>
</feed>