Rubocop refers to each common code smell and check as a cop, is a Ruby static code analyzer.
I have a nightmare last night that I get “cop” when I try to run away, and she (Rubocop) said “STOP! Either you fix the code, or I will kill you.”
So, here I am to find a cop, and see what it looks for in my Ruby code. So let’s take a look at variable.rb
I’m going into my self_practice directory, making a new directory call rubocop_variable_testing. Inside of rubocop_variable_testing directory, setup RVM load Ruby 2.1.5 and the davinci_t1_2015 gemset.
Then I need to add the guard-rubocop gem.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Since in class T1 2015 we didn’t cover rspec yet, we can comment out line 4 for now. Let’s bundle them in the iTerm, and initialize guard.
In RubyMine should have a jump up on the left by now. How do we not getting screaming by itself while fire up guard?
The answer is go into RubyMine, highlight the directory robocop_variable_testing, command + N, create a new file call <.rubocop.yml>, and put into the following code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Let’s create a ruby file call <variable_force.rb>, and start
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Since I’m not allow to run away, I’m just going to fix them. Shall we? “Fix or Die!” (Totally joking)
First, let’s fix the Useless assignment to variable offenses from line 1 to line 5 with one extra line 😉 Ready?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
However, now I have a new offense variable_force.rb:9:81: C: Line is too long. [89/80] puts “#{school} #{professor.capitalize} in #{lesson} said:’#{quote1} because #{quote2}’ ”
Well, let’s fix this by break down this long line.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Another common offense in rubocop look like this: Inconsistent indentation detected. quote2 = “Google never forget!”, and we can fix by delete extra useless indentation shown in the following line 7.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The super popular offense that rubocop love to scream at are Prefer single-quoted strings when you don’t need string interpolation or special symbols. quote2 = “Google never forget!”, and we can simply fix it by change it out to single quote such as the following.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Finally, the last common offense I’m covering tonight is 1 trailing blank lines, which we just delete the extra blank line we don’t use at the end.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Let’s play with git practice step by step, shall we?
Here we go:
change into self_practice directory for class T1-2015
cd ~/workspace/davinci_coders_t1_2015/self_practice
make a new directory call practice
mkdir practice
get into the practice directory
cd practice/
In the practice directory, make a git_warmup directory
mkdir git_warmup
get into the git_warmup directory
cd git_warmup/
In the git_warmup directory:
Setup RVM
echo ‘2.1.5’ > .ruby-version
echo ‘git_warmup’ > .ruby-gemset
Get RVM to reload these setting:
cd .
rvm current
Setup Robocop
mine .
When RubyMine is open,
we use shortcut command + N to create a new file.
Type in <Gemfile> and click [OK] button.
In Gemfile, we type in source ‘http://rubygems.org’ in line 1, and gem ‘guard-rubocop’ in line 3.
Now we bundle the gems into our RubyMine files.
Time to initialize guard with guard init,
and you should see in your RubyMine have a Guardfile jump up in there.
Now when your fire up guard, you will see that there are already 2 offenses for guard file alone.
To fix this, we highlight the git_warmup directory, and going to create a new file command + N with a name <.rubocop.yml> in RubyMine.
And put into these code into <.rubocop.yml> to exclude guardfile in rubocop:
AllCops:
Exclude:
- 'Guardfile'
then the rubocop will tell you.
Setup Git
Command + T in iTerm to open a new tape, git init to create an empty repo locally, adding initial files, check status, commit initial setup files.
If you hit (enter) after git commit, you are now into vim, just hit i turn into Insert mode and type in your commit message ‘Added initial setup files‘, hit [esc] button, type :wq to save the change and quit vim
use git log -p to show any commit in this repo.
hit q to exit vim.
In the git _warmup directory:
create a ruby program call greeting
You can either go to iTerm type in touch greeting.rb, or go into RubyMine highlight git_warmup directory, command + N, and type in greeting.rb, then click [OK] button. Has the program ask for your name, then print out “Nice to meet you, (your name here).”
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I’m feeling a little lazy today, let’s use gitx [enter]. Now we have a gitx box jump up, and I am using shortcut command + A to selected all files from the left box that I need to commit and draft them to the right box.
put into commit message into the middle and click [commit] button, than close it on the left top [x] red button.
When we type in git log, and we now have 2 commit repository.
Now, let’s push a new repo on github.
Let’s go to github.com/new
and we put into the Repository name as git_self_practice_for_fun_1, type in Description as just practice git101 for fun, then click [Create repository] button. Make sure the [ssh] button is check on this new page, since we already have existing repository, we click the clipboard button on the …or push an existing repository from the command line, then go paste it onto iTerm using shortcut command + V [enter].
I’m not so sure if I get this down yet, how do I get more practice?
Let’s start the next one in the same directory practice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Valentine’s Day is coming up soon, I hope this will be a fun blog for every one!
Let’s start with Amanda’s Valentine’s Day Party
Amanda is planning on having 24 kids at her Valentine’s Day party, including herself. She invited ten girls. How many boys did she invite?
Amanda is going to buy cupcakes for the party. There are a dozen in each box. How many boxes will she need?
Amanda’s father says that Amanda must not spend more than $1.75 per person on the party supplies. Amanda has fifty dollars. That means that she should have at least how much money left over after she buys the supplies?
One third of the kids at the party will be coming from Amanda’s old neighborhood. How many kids are coming from her old neighborhood?
Everyone at the party will bring a Valentine card for everyone else at the party. How many cards is that altogether?
If you are currently in Jason Noble’s class T1 2015. We’ll do the following to practice for fun, you are welcome to follow me.
First, go to terminal and type in
cd ~/workspace/davinci_coders_t1_2015/building_the_toolbelt_t1_2015/
mkdir self_practice
cd self_practice
mkdir number_and_string
cd number_and_string
touch amandas_valentines_party.rb
mine .
When the RubyMine jump up, here’s how we do in the following:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1. Amanda is planning on having 24 kids at her Valentine’s Day party, including herself. She invited ten girls. How many boys did she invite?
# 2. Amanda is going to buy cupcakes for the party. There are a dozen in each box. How many boxes will she need?
# 3. Amanda’s father says that Amanda must not spend more than $1.75 per person on the party supplies. Amanda has fifty dollars. That means that she should have at least how much money left over after she buys the supplies?
# 4. One third of the kids at the party will be coming from Amanda’s old neighborhood. How many kids are coming from her old neighborhood?
# 5. Everyone at the party will bring a Valentine card for everyone else at the party. How many cards is that altogether?
amanda_party_total_guests = 24
number_of_girls = 10
cupcakes_per_box = 12
amandas_money = 50
supplies_cost_per_guest = 1.75
puts "There are #{amanda_party_total_guests - number_of_girls} boys Amanda invited to her Valentine's Day Party."
puts "Amanda need #{amanda_party_total_guests / cupcakes_per_box} boxes of cupcakes for the party."
puts "Amanda should have at least $#{amandas_money - (amanda_party_total_guests * supplies_cost_per_guest)} left after she buys the supplies."
puts "There are #{amanda_party_total_guests / 3} kids coming from Amanda's old neighborhood."
puts "There are total #{amanda_party_total_guests * amanda_party_total_guests} Valentine cards hanging out from this party."
Now we can run it in RubyMine by short cut “Shift + Control + R”, or go up to the menu click RUN/ RUN/ then click the file you want to run, in this case which is amandas_valentines_party.rb. You can also run it in the terminal by type in ruby amandas_valentines_party.rb
This is just the most simple way. Of cause Jason will tell you that TMTOWTDI, which stand for There’s more than one way to do it. So, let’s see what other way we can do this also. The following codes assign the formulas to new variables, which make the print line “puts” more clear.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1. Amanda is planning on having 24 kids at her Valentine’s Day party, including herself. She invited ten girls. How many boys did she invite?
# 2. Amanda is going to buy cupcakes for the party. There are a dozen in each box. How many boxes will she need?
# 3. Amanda’s father says that Amanda must not spend more than $1.75 per person on the party supplies. Amanda has fifty dollars. That means that she should have at least how much money left over after she buys the supplies?
# 4. One third of the kids at the party will be coming from Amanda’s old neighborhood. How many kids are coming from her old neighborhood?
# 5. Everyone at the party will bring a Valentine card for everyone else at the party. How many cards is that altogether?
IRB stands for Interactive Ruby, and it’s a read, evaluate print loop. It can read what we type in, and value as a ruby code. Now the resource can give really quick feedback, and it’s gonna to loop back around and ask more ruby code.
So why we using IRB? Because we went to have some instant feedback, or sometimes we just want to experience to try somethings else. IRB is kind of like a dictionary to Ruby language, fast and easy!
How do I start? Just type in IRB in terminal.
What’s next? We can type in anything we want, and IRB will be give us feedback right away. For example:
If we type in string “My name is Grace.”, then IRB will reply “My name is Grace.”.
If we type in variable greeting = “My name is Grace.”, IRB will reply “My name is Grace.”
Now we assign “My name is Grace.” to variable, IRB will still reply “My name is Grace.” when we type in variable greeting.
Let’s have some fun time! What if I type in greeting.reverse? “.ecarG si eman yM” will be IRB’s answer.
What if I type in Time.new? IRB will show you the current time, such as 2015-02-04 17:55:14 -0800
Let’s take a look of some errors we might run into IRB. If we type too fast “My name is [enter] and now we are in second line, even we finish it on line 2 of Grace.”, IRB will read it as “My name is\nGrace.” What if we forget to assign anything to the variable only type in greeting =? IRB will get lost and ask “?”, and now you can finish it with the object you want to assign to your variable greeting by type in “My name is Grace.”. We are all human, of course all of us will have typo from time to time, such as typo in greeting. Don’t worry, IRB gonna tell you with a long message such as NameError: undefined local variable or method ‘greting’ for main: Object from (IRB):10 from / Users/grace/.rvm/ruies/ruby-2.32-p0/bin/irb:16:in ”
What if I have enough fun with IRB, but now I just want to jump out? Oh, that’s easy! Just type in exit will do
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters