|* Trustie实践教学平台||* Trustie协同开发平台|
|* Trustie开源监测与推荐平台||* Trustie可信资源库平台|
|* Trustie服务组合开发平台||* Trustie可信评估与增强平台|
This 3-part homework gives some basic practice in Ruby as well as getting you accustomed to making testing a regular part of your workflow.
After completing this assignment, you will know how to:
The repo for this assigment follows a fairly standard Ruby convention for codebases: the code files are stored in lib/ and the test files are stored in spec/. (We use the RSpec unit-testing framework; if we were using Ruby's default framework, known as Test::Unit, the test files would be under test/.)
We've placed "starter code" in lib/ruby_intro.rb; when you're all done, you can submit to Trustie.
However, you can test each of the 3 parts separately. The files spec/part_spec.rb contain RSpec tests for each of the three parts. For example, to test your answers to Part 1, say rspec spec/part1_spec.rb. rspec with no arguments runs the tests in all the files spec/*_spec.rb.
To ensure you have the rspec gem installed you need bundler and can then run bundle install like so (if you accidentally do not use c9 ide):
$ gem install bundler $ cd hw-ruby-intro $ bundle
When the above completes successfully you'll have RSpec installed and can run rspec from the command line to test your code.
Before starting your homework, please refer to the recommanded workflow as below for this and future assignment:
git clone https://github.com/saasbook/hw-ruby-intro
Please refer to the Trustie user guide for creating project, repository, and using basic git command to manage your code repositories.
Check the Ruby 2.x documentation on Array, Hash and Enumerable as they could help tremendously with these exercises. :-)
Define a method sum(array) that takes an array of integers as an argument and returns the sum of its elements. For an empty array it should return zero. Run associated tests via:
$ rspec spec/part1_spec.rb:5
Define a method max_2_sum(array) which takes an array of integers as an argument and returns the sum of its two largest elements. For an empty array it should return zero. For an array with just one element, it should return that element. Run associated tests via:
$ rspec spec/part1_spec.rb:23
Define a method sum_to_n?(array, n) that takes an array of integers and an additional integer, n, as arguments and returns true if any two elements in the array of integers sum to n.sum_to_n?(, n) should return false for any value of n, by definition. Run associated tests via:
$ rspec spec/part1_spec.rb:42
You can check your progress on the all the above by running $ rspec spec/part1_spec.rb.
Check the documentation on String and Regexp as they could help tremendously with these exercises. :-)
Define a method hello(name) that takes a string representing a name and returns the string "Hello, " concatenated with the name. Run associated tests via:
$ rspec -e '#hello' spec/part2_spec.rb
Define a method starts_with_consonant?(s) that takes a string and returns true if it starts with a consonant and false otherwise. (For our purposes, a consonant is any letter other than A, E, I, O, U.) NOTE: be sure it works for both upper and lower case and for non letters! Run associated tests via:
$ rspec -e '#starts_with_consonant?' spec/part2_spec.rb
Define a method binary_multiple_of_4?(s) that takes a string and returns true if the string represents a binary number that is a multiple of 4. NOTE: be sure it returns false if the string is not a valid binary number! Run associated tests via:
$ rspec -e '#binary_multiple_of_4?' spec/part2_spec.rb
You can check your progress on the all the above by running $ rspec spec/part2_spec.rb.
Define a class BookInStock which represents a book with an ISBN number, isbn, and price of the book as a floating-point number, price, as attributes. Run associated tests via:
$ rspec -e 'getters and setters' spec/part3_spec.rb
The constructor should accept the ISBN number (a string, since in real life ISBN numbers can begin with zero and can include hyphens) as the first argument and price as second argument, and should raise ArgumentError(one of Ruby's built-in exception types) if the ISBN number is the empty string or if the price is less than or equal to zero. Include the proper getters and setters for these attributes. Run associated tests via:
$ rspec -e 'constructor' spec/part3_spec.rb
Include a method price_as_string that returns the price of the book formatted with a leading dollar sign and two decimal places, that is, a price of 20 should format as "$20.00" and a price of 33.8 should format as "$33.80". Run associated tests via:
$ rspec -e '#price_as_string' spec/part3_spec.rb
You can check your progress on the all the above by running rspec spec/part3_spec.rb.
The actual RottenPotatoes starter app you will use is in repo and please read the README.md file carefully for more guidance:
Whenever you start working on a Rails project, the first thing you should do is to run Bundler, to make sure all the app's gems are installed. Switch to the app's root directory (presumably rails-intro) and run bundle install --without production(you only need to specify--without production the first time, as this setting will be remembered on future runs of Bundler for this project).
If there is an error information on installing gems, edit source to 'http://ruby.taobao.org' in Gemfile.
Finally, get the local database created:
This creates a local development database and runs the migrations to create the app's schema. It also creates the file db/schema.rb to reflect the latest database schema. You should place this file under version control.
...And insert "seed data" into the database--initial data items that the app needs to run:
At this point you should be able to run the app locally (rails server) and navigating to http://localhost:3000/moviesin your browser.
Notice: You should also have a repository in your Trustie platform.
If you have deployed to Heroku before, just create a new app container with heroku create. If this is your first time deploying to Heroku, you will need to do two things. First, sign up for a free Heroku account. Then set up ssh keys to securely communicate with Heroku for app deployments. The three basic commands you need are the following, but see the Heroku page for more details if following commands fail to work.
ssh-keygen -t rsa heroku login heroku keys:add
Once your keys are set up (a one-time process), you should be able to create an "app container" on Heroku into which you'll deploy RottenPotatoes:
Heroku will assign your app a whimsical name such as luminous-coconut-237; once your app is deployed, you would access it at http://luminous-coconut-237.herokuapp.com. You can login to the Heroku website if you want to change the name of your app.
Finally, we deploy our app to Heroku:
git push heroku master
(It is normal to see the following warning the first time---answer "yes", and in the future you shouldn't see it any more:)
The authenticity of host 'heroku.com (220.127.116.11)' can't be established. RSA key fingerprint is 8b:48:5e:67:0e:c9:16:47:32:f2:87:0c:1f:c8:60:ad. Are you sure you want to continue connecting (yes/no)? Please type 'yes' or 'no':
Is the app running on Heroku? No, because just as we ran rake db:setup to do first-time database creation locally, we must also cause a database to be created on the Heroku side:
heroku run rake db:setup
Now you should be able to navigate to your app's URL. heroku open opens your browser to that URL in case you forgot it.
Once you're confident the functionality works correctly on Heroku, submit the URI of your deployed Heroku app in a text file with no other contents.
Please be careful to use http and not https, that is, submit http://your-app.herokuapp.com and NOT https://your-app.herokuapp.com.