From e770e8875797ecab2a43b6b1ef7004c32f36166d Mon Sep 17 00:00:00 2001 From: Ken Ding Date: Tue, 27 Jun 2017 02:41:11 +0930 Subject: [PATCH] finish assignment 1 --- Gemfile | 2 +- Gemfile.lock | 29 +++++++++++++++++++++++++++++ lib/ruby_intro.rb | 28 +++++++++++++++++++++------- 3 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 Gemfile.lock diff --git a/Gemfile b/Gemfile index 2a4bda74..38d40a5d 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ source 'https://rubygems.org' -ruby '2.3' +ruby '2.3.3' gem 'rspec' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 00000000..705df4df --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,29 @@ +GEM + remote: https://rubygems.org/ + specs: + diff-lcs (1.3) + rspec (3.5.0) + rspec-core (~> 3.5.0) + rspec-expectations (~> 3.5.0) + rspec-mocks (~> 3.5.0) + rspec-core (3.5.4) + rspec-support (~> 3.5.0) + rspec-expectations (3.5.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.5.0) + rspec-mocks (3.5.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.5.0) + rspec-support (3.5.0) + +PLATFORMS + ruby + +DEPENDENCIES + rspec + +RUBY VERSION + ruby 2.3.3p222 + +BUNDLED WITH + 1.14.6 diff --git a/lib/ruby_intro.rb b/lib/ruby_intro.rb index e74c4c28..9fdd1c90 100644 --- a/lib/ruby_intro.rb +++ b/lib/ruby_intro.rb @@ -3,33 +3,47 @@ # Part 1 def sum arr - # YOUR CODE HERE + arr.inject(0, :+) end def max_2_sum arr - # YOUR CODE HERE + sum(arr.sort {|x, y| y <=> x}.take(2)) end def sum_to_n? arr, n - # YOUR CODE HERE + return false if arr.size < 2 + + arr.permutation(2).any? {|a, b| a + b == n} end # Part 2 def hello(name) - # YOUR CODE HERE + "Hello, #{name}" end def starts_with_consonant? s - # YOUR CODE HERE + /^[b-df-hj-np-tv-z]/i.match(s) != nil end def binary_multiple_of_4? s - # YOUR CODE HERE + return true if s == "0" + /^[10]*00$/.match(s) != nil end # Part 3 class BookInStock -# YOUR CODE HERE + attr_accessor :isbn, :price + + def initialize isbn, price + raise ArgumentError if isbn.empty? || price <= 0 + + @isbn = isbn + @price = price + end + + def price_as_string + sprintf("$%2.2f", @price) + end end