Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson #2 #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Documentation:
Style/FrozenStringLiteralComment:
Enabled: false
AllCops:
TargetRubyVersion: 2.4.1
TargetRubyVersion: 2.5.1
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://rubygems.org'

ruby '2.3.1'
ruby '2.5.1'

gem 'rspec'
gem 'rubocop'
58 changes: 32 additions & 26 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
GEM
remote: https://rubygems.org/
specs:
ast (2.1.0)
astrolabe (1.3.1)
parser (~> 2.2)
diff-lcs (1.2.5)
parser (2.2.3.0)
ast (>= 1.1, < 3.0)
powerpack (0.1.1)
rainbow (2.0.0)
rspec (3.3.0)
rspec-core (~> 3.3.0)
rspec-expectations (~> 3.3.0)
rspec-mocks (~> 3.3.0)
rspec-core (3.3.2)
rspec-support (~> 3.3.0)
rspec-expectations (3.3.1)
ast (2.4.0)
diff-lcs (1.3)
jaro_winkler (1.5.1)
parallel (1.12.1)
parser (2.5.1.2)
ast (~> 2.4.0)
powerpack (0.1.2)
rainbow (3.0.0)
rspec (3.8.0)
rspec-core (~> 3.8.0)
rspec-expectations (~> 3.8.0)
rspec-mocks (~> 3.8.0)
rspec-core (3.8.0)
rspec-support (~> 3.8.0)
rspec-expectations (3.8.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-mocks (3.3.2)
rspec-support (~> 3.8.0)
rspec-mocks (3.8.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-support (3.3.0)
rubocop (0.34.2)
astrolabe (~> 1.3)
parser (>= 2.2.2.5, < 3.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.0)
rubocop (0.59.2)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
parser (>= 2.5, != 2.5.1.1)
powerpack (~> 0.1)
rainbow (>= 1.99.1, < 3.0)
ruby-progressbar (~> 1.4)
ruby-progressbar (1.7.5)
rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-progressbar (1.10.0)
unicode-display_width (1.4.0)

PLATFORMS
ruby
Expand All @@ -37,5 +40,8 @@ DEPENDENCIES
rspec
rubocop

RUBY VERSION
ruby 2.5.1p57

BUNDLED WITH
1.10.6
1.16.6
18 changes: 18 additions & 0 deletions ball.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'yaml'

class Ball
ANSWERS = YAML.load_file(File.join(__dir__, './answers.yml'))
GROUP_SIZE = 5
FOREGROUND_OFFSET = 31

def colorize(answer)
color_index = ANSWERS.find_index(answer) / GROUP_SIZE
"\e[#{FOREGROUND_OFFSET + color_index}mANSWER\e[0m"
end

def shake
answer = ANSWERS.sample
puts colorize(answer)
answer
end
end
26 changes: 26 additions & 0 deletions game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require_relative './pet'

class Game
def initialize
p 'Hello, lets play)'
p 'Put the name of your dog'
name = gets.chomp
@pet = Pet.new(name)

choose_action
end

def choose_action
action = gets.chomp

if @pet.respond_to? action
@pet.public_send(action)
else
p 'sorry there no such action, try put `help`'
end

choose_action
end
end

Game.new
116 changes: 116 additions & 0 deletions pet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
class Pet
def initialize(name)
@name = name
@type = 'DOG'
@lives = 5
@stuff_in_belly = 10
@stuff_in_intestine = 0
@tired = 0
@mood = 'NORMAL'
@ruby_skill = 0
@js_skill = 0

p "#{@name} was born"
end

def help
p 'Here all available commands:'
p 'name => display pet name'
p 'study_ruby => learn Ruby language'
p 'study_js => learn JS language'
p 'walk => reset poop'
p 'put_to_bed => reset tired'
p 'just_watch => do nothing'
end

def info
p "Name: #{@name}"
p "Hungry: #{hungry?}"
p "Poopy: #{poopy?}"
p "JS: #{poopy?}"
end

def feed
@stuff_in_belly = 10
p 'I`m full'
passage_of_time
end

def study_ruby
if @tired < 10
@ruby_skill += 1
p 'Wow. Ruby is awesome!'

if @ruby_skill == 10
puts 'looks at you like a piece of shit'
exit
end
passage_of_time
end
end

def study_js
if @tired < 10
@js_skill += 1
p 'I love JS!'

if @js_skill == 10
p 'looks at you like a piece of shit'
exit
end

passage_of_time
end
end

def walk
p 'Waaaaaaalk!'
@stuff_in_intestine = 0
passage_of_time
end

def put_to_bed
p 'Good night'
@tired = 0

passage_of_time
end

def just_watch
p 'I`m boring, lets do something?'
passage_of_time
end

private

def passage_of_time
@stuff_in_belly -= 1
@stuff_in_intestine += 1

if hungry
if @stuff_in_belly == 0
p 'i live you'
exit
else
p 'I m hungry'
end
end

if poopy
if @stuff_in_intestine == 10
p 'Oops'
@stuff_in_intestine = 0
else
p 'Wanna poop'
end
end
end

def hungry
@stuff_in_belly <= 2
end

def poopy
@stuff_in_intestine >= 8
end
end
39 changes: 39 additions & 0 deletions spec/pet_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'yaml'
require_relative '../pet'

RSpec.describe Pet do
let(:pet) { Pet.new('Pushka') }

it 'Should check js_skill inc' do
expect(pet.instance_variable_get('@js_skill')).to eql(0)
pet.study_js
expect(pet.instance_variable_get('@js_skill')).to eql(1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect { pet.study_js }.to change(pet.instance_variable(@js_skill)).form(0).to(1)

end
it 'Should check ruby_skill inc' do
expect(pet.instance_variable_get('@ruby_skill')).to eql(0)
pet.study_ruby
expect(pet.instance_variable_get('@ruby_skill')).to eql(1)
end
it 'Should don`t inc ruby_skill if tired' do
pet.instance_variable_set('@tired', 10)
pet.study_ruby
expect(pet.instance_variable_get('@ruby_skill')).to eql(0)
end

it 'should fill `@stuff_in_belly` on `feed`' do
pet.instance_variable_set('@stuff_in_belly', 5)
pet.feed
expect(pet.instance_variable_get('@stuff_in_belly')).to eql(9)
end

it 'should reset tired if put_to_bed' do
pet.instance_variable_set('@tired', 5)
pet.put_to_bed
expect(pet.instance_variable_get('@tired')).to eql(0)
end
it 'should reset @stuff_in_intestine if walk' do
pet.instance_variable_set('@stuff_in_intestine', 5)
pet.walk
expect(pet.instance_variable_get('@stuff_in_intestine')).to eql(1)
end
end