Run!!!! It’s a cop. Oh, don’t worry! If you fix it, Rubocop will stop screaming at you ;)

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.” robo_cop_cosplay

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.

Screen Shot 2015-02-19 at 6.54.55 PM

Then I need to add the guard-rubocop gem.

source 'https://rubygems.org'
gem 'guard-rubocop'
gem 'guard-rspec'
view raw Gemfile hosted with ❤ by GitHub

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. Screen Shot 2015-02-19 at 7.15.09 PM

In RubyMine should have a jump up on the left by now. How do we not getting screaming by itself while fire up guard? Screen Shot 2015-02-19 at 7.28.08 PM

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.

AllCops:
Exclude:
- 'Guardfile'
view raw .rubocop.yml hosted with ❤ by GitHub
Now rubocop should be happy for a little while. Screen Shot 2015-02-19 at 7.31.06 PM

Let’s create a ruby file call <variable_force.rb>, and start

#!/usr/bin/env ruby
professor = 'jason'
lesson = 'Building the Toolbell of a Junior Ruby on Rail Developer'
school = 'DaVinci'
quote1 = "Don't be a jerk"
quote2 = "Google never forget!"
 Screen Shot 2015-02-19 at 8.01.23 PM

Since I’m not allow to run away, I’m just going to fix them. Shall we? robo_cop_cosplay“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?

#!/usr/bin/env ruby
professor = 'jason'
lesson = 'Building the Toolbell of a Junior Ruby on Rail Developer'
school = 'DaVinci'
quote1 = "Don't be a jerk"
quote2 = "Google never forget!"
puts "#{school} #{professor.capitalize} in #{lesson} said:'#{quote1} because #{quote2}' "
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}’ ” Screen Shot 2015-02-19 at 8.14.47 PM

Well, let’s fix this by break down this long line.

#!/usr/bin/env ruby
professor = 'jason'
lesson = 'Building the Toolbell of a Junior Ruby on Rail Developer'
school = 'DaVinci'
quote1 = "Don't be a jerk"
quote2 = "Google never forget!"
puts "#{school} #{professor.capitalize} in #{lesson}
said:'#{quote1} because #{quote2}' "

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.

#!/usr/bin/env ruby
professor = 'jason'
lesson = 'Building the Toolbell of a Junior Ruby on Rail Developer'
school = 'DaVinci'
quote1 = "Don't be a jerk"
quote2 = "Google never forget!"
puts "#{school} #{professor.capitalize} in #{lesson}
said:'#{quote1} because #{quote2}' "

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.

#!/usr/bin/env ruby
professor = 'jason'
lesson = 'Building the Toolbell of a Junior Ruby on Rail Developer'
school = 'DaVinci'
quote1 = "Don't be a jerk"
quote2 = 'Google never forget!'
puts "#{school} #{professor.capitalize} in #{lesson}
said:'#{quote1} because #{quote2}' "
Or else, you can disable the double-single quote in rubocop.

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. 

#!/usr/bin/env ruby
professor = 'jason'
lesson = 'Building the Toolbell of a Junior Ruby on Rail Developer'
school = 'DaVinci'
quote1 = "Don't be a jerk"
quote2 = 'Google never forget!'
puts "#{school} #{professor.capitalize} in #{lesson}
said:'#{quote1} because #{quote2}' "

Guess what? Screen Shot 2015-02-19 at 9.30.15 PMRubocop

Haha…I guess tonight I won’t have nightmare from rubocop. Just joking. Hope you like it 😉

GIT101 practice for fun :P

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 .

Screen Shot 2015-02-15 at 4.50.45 PM

When RubyMine is open,

Screen Shot 2015-02-15 at 4.47.22 PM

we use shortcut command + N to create a new file.

Screen Shot 2015-02-15 at 4.47.43 PM Screen Shot 2015-02-15 at 4.48.03 PM

Type in <Gemfile> and click [OK] button. Screen Shot 2015-02-15 at 4.48.38 PM

In Gemfile, we type in source ‘http://rubygems.org&#8217; in line 1, and gem ‘guard-rubocop’ in line 3.Screen Shot 2015-02-15 at 4.50.13 PM

Now we bundle the gems into our RubyMine files. Screen Shot 2015-02-15 at 5.02.00 PM

Time to initialize guard with guard init, Screen Shot 2015-02-15 at 5.04.31 PM

and you should see in your RubyMine have a Guardfile jump up in there. Screen Shot 2015-02-15 at 5.05.09 PM

Now when your fire up guard, you will see that there are already 2 offenses for guard file alone. Screen Shot 2015-02-15 at 5.12.26 PM

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. Screen Shot 2015-02-15 at 5.21.51 PM

And put into these code into <.rubocop.yml> to exclude guardfile in rubocop: 

AllCops:
  Exclude:
    - 'Guardfile'

then the rubocop will tell you Screen Shot 2015-02-15 at 5.27.21 PM.

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. Screen Shot 2015-02-15 at 5.34.50 PM 

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. Screen Shot 2015-02-15 at 5.38.57 PM

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).”
#!/usr/bin/env ruby
print 'What is your name?'
name = gets.chomp.capitalize
puts "Nice to meet you, #{name}."
view raw gistfile1.rb hosted with ❤ by GitHub

Now I want to check my git status. Screen Shot 2015-02-15 at 6.26.50 PM

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. Screen Shot 2015-02-15 at 6.33.21 PM Screen Shot 2015-02-15 at 6.36.39 PM

put into commit message into the middle and click [commit] button, than close it on the left top [x] red button. Screen Shot 2015-02-15 at 6.38.46 PM

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 Screen Shot 2015-02-15 at 6.45.38 PM

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. Screen Shot 2015-02-15 at 6.53.51 PM 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]. Screen Shot 2015-02-15 at 6.57.57 PM

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 

Graces-MacBook-Pro:git_warmup2 RiverGracefish$ cd ..
Graces-MacBook-Pro:practice RiverGracefish$ pwd
/Users/RiverGracefish/workspace/davinci_coders_t1_2015/self_practice/practice
Graces-MacBook-Pro:practice RiverGracefish$ ls
git git_warmup
Graces-MacBook-Pro:practice RiverGracefish$ mkdir git_warmup2
Graces-MacBook-Pro:practice RiverGracefish$ cd git_warmup2
Graces-MacBook-Pro:git_warmup2 RiverGracefish$ echo '2.1.2' > .ruby-version
Graces-MacBook-Pro:git_warmup2 RiverGracefish$ echo 'git_warmup' > .ruby-gemset
Graces-MacBook-Pro:git_warmup2 RiverGracefish$ cd .
ruby-2.1.2 - #gemset created /Users/RiverGracefish/.rvm/gems/ruby-2.1.2@git_warmup
ruby-2.1.2 - #generating git_warmup wrappers...............
Graces-MacBook-Pro:git_warmup2 RiverGracefish$ mine .
Graces-MacBook-Pro:git_warmup2 RiverGracefish$ bundle
Fetching gem metadata from https://rubygems.org/............
Resolving dependencies...
Installing ast 2.0.0
Installing parser 2.2.0.3
Installing astrolabe 1.3.0
Installing hitimes 1.2.2
Installing timers 4.0.1
Installing celluloid 0.16.0
Installing coderay 1.1.0
Installing ffi 1.9.6
Installing formatador 0.2.5
Installing rb-fsevent 0.9.4
Installing rb-inotify 0.9.5
Installing listen 2.8.5
Installing lumberjack 1.0.9
Installing nenv 0.2.0
Installing shellany 0.0.1
Installing notiffany 0.0.5
Installing method_source 0.8.2
Installing slop 3.6.0
Installing pry 0.10.1
Installing thor 0.19.1
Installing guard 2.12.1
Installing powerpack 0.1.0
Installing rainbow 2.0.0
Installing ruby-progressbar 1.7.1
Installing rubocop 0.29.1
Installing guard-rubocop 1.2.0
Using bundler 1.6.2
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
Graces-MacBook-Pro:git_warmup2 RiverGracefish$ guard init
19:19:38 - INFO - Writing new Guardfile to /Users/RiverGracefish/workspace/davinci_coders_t1_2015/self_practice/practice/git_warmup2/Guardfile
19:19:38 - INFO - rubocop guard added to Guardfile, feel free to edit it
Graces-MacBook-Pro:git_warmup2 RiverGracefish$ guard
19:20:04 - INFO - Inspecting Ruby code style of all files
warning: parser/current is loading parser/ruby21, which recognizes
warning: 2.1.5-compliant syntax, but you are running 2.1.2.
Inspecting 2 files
.C
Offenses:
Guardfile:27:9: C: Use %r only for regular expressions matching more than 1 '/' character.
watch(%r{.+\.rb$})
^^^^^^^^^^^
Guardfile:28:9: C: Use %r only for regular expressions matching more than 1 '/' character.
watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 files inspected, 2 offenses detected
19:20:05 - INFO - Guard is now watching at '/Users/RiverGracefish/workspace/davinci_coders_t1_2015/self_practice/practice/git_warmup2'
[1] guard(main)> exit
19:20:12 - INFO - Bye bye...
Graces-MacBook-Pro:git_warmup2 RiverGracefish$ guard
19:21:35 - INFO - Inspecting Ruby code style of all files
warning: parser/current is loading parser/ruby21, which recognizes
warning: 2.1.5-compliant syntax, but you are running 2.1.2.
Inspecting 1 file
.
1 file inspected, no offenses detected
19:21:35 - INFO - Guard is now watching at '/Users/RiverGracefish/workspace/davinci_coders_t1_2015/self_practice/practice/git_warmup2'
[1] guard(main)> exit
19:21:37 - INFO - Bye bye...
Graces-MacBook-Pro:git_warmup2 RiverGracefish$ git init
Initialized empty Git repository in /Users/RiverGracefish/workspace/davinci_coders_t1_2015/self_practice/practice/git_warmup2/.git/
Graces-MacBook-Pro:git_warmup2 RiverGracefish$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
.rubocop.yml
.ruby-gemset
.ruby-version
Gemfile
Gemfile.lock
Guardfile
nothing added to commit but untracked files present (use "git add" to track)
Graces-MacBook-Pro:git_warmup2 RiverGracefish$ git add .ru* Gemfile* Guard*
Graces-MacBook-Pro:git_warmup2 RiverGracefish$ git commit -m 'Added initial setup'
[master (root-commit) 828d37f] Added initial setup
6 files changed, 100 insertions(+)
create mode 100644 .rubocop.yml
create mode 100644 .ruby-gemset
create mode 100644 .ruby-version
create mode 100644 Gemfile
create mode 100644 Gemfile.lock
create mode 100644 Guardfile
Graces-MacBook-Pro:git_warmup2 RiverGracefish$ ruby greeting.rb
What is your name? Phil
Nice to meet you, Phil.
Graces-MacBook-Pro:git_warmup2 RiverGracefish$ ruby greeting.rb
What is your name? lovephilippe
Nice to meet you, Lovephilippe.
Graces-MacBook-Pro:git_warmup2 RiverGracefish$ ruby greeting.rb
What is your name? smile
Nice to meet you, Smile.
Graces-MacBook-Pro:git_warmup2 RiverGracefish$ git add greeting.rb
Graces-MacBook-Pro:git_warmup2 RiverGracefish$ git commit -m 'adding greeting file'
[master c853877] adding greeting file
1 file changed, 6 insertions(+)
create mode 100644 greeting.rb
Graces-MacBook-Pro:git_warmup2 RiverGracefish$ git remote add origin git@github.com:RiverGracefish/git_self_practice_for_fun_2.git
Graces-MacBook-Pro:git_warmup2 RiverGracefish$ git push -u origin master
Counting objects: 11, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (11/11), 1.84 KiB | 0 bytes/s, done.
Total 11 (delta 1), reused 0 (delta 0)
To git@github.com:RiverGracefish/git_self_practice_for_fun_2.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
Graces-MacBook-Pro:git_warmup2 RiverGracefish$ ls
Gemfile Gemfile.lock Guardfile greeting.rb
Graces-MacBook-Pro:git_warmup2 RiverGracefish$ cd ..
Graces-MacBook-Pro:practice RiverGracefish$ ls
git git_warmup git_warmup2
Graces-MacBook-Pro:practice RiverGracefish$

code for <Gemfile>

source 'https://rubygems.org&#39;
gem 'guard-rubocop'
gem 'guard-rspec'
view raw Gemfile hosted with ❤ by GitHub

code for <.rubocop.yml>

AllCops:
Exclude:
- 'Guardfile'
view raw .rubocop.yml hosted with ❤ by GitHub

Want more practice? Repeat 😉

Valentine’s Day party~ number and string for fun ;)

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

  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?

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:

# 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.

# 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
number_of_boys = amanda_party_total_guests - number_of_girls
total_boxes_of_cupcakes = amanda_party_total_guests / cupcakes_per_box
amandas_balance = amandas_money - (amanda_party_total_guests * supplies_cost_per_guest)
old_naighbors = amanda_party_total_guests / 3
total_cards = amanda_party_total_guests * amanda_party_total_guests
puts "There are #{number_of_boys} boys Amanda invited to her Valentine's Day Party."
puts "Amanda need #{total_boxes_of_cupcakes} boxes of cupcakes for the party."
puts "Amanda should have at least $#{amandas_balance} left after she buys the supplies."
puts "There are #{old_naighbors} kids coming from Amanda's old neighborhood."
puts "There are total #{total_cards} Valentine cards hanging out from this party."

Don’t forget to run it see if it works or not 😉

Please leave a comment let me know if it’s helpful or not, or any topic you wish me to talk about on my blog. Thanks, and have a nice day.

What’s IRB? Not a Baby of IRs!

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

Graces-MacBook-Pro:studio_game RiverGracefish$ irb
2.1.2 :001 > "My name is Grace."
=> "My name is Grace."
2.1.2 :002 > greeting = "My name is Grace."
=> "My name is Grace."
2.1.2 :003 > greeting
=> "My name is Grace."
2.1.2 :004 > greeting.reverse
=> ".ecarG si eman yM"
2.1.2 :005 > Time.new
=> 2015-02-06 16:01:54 -0700
2.1.2 :006 > "My name is
2.1.2 :007"> Grace."
=> "My name is\nGrace."
2.1.2 :008 > greeting =
2.1.2 :009 > "My name is Grace."
=> "My name is Grace."
2.1.2 :010 > greting
NameError: undefined local variable or method `greting' for main:Object
from (irb):10
from /Users/RiverGracefish/.rvm/rubies/ruby-2.1.2/bin/irb:11:in `<main>'
2.1.2 :011 > exit
Graces-MacBook-Pro:studio_game RiverGracefish$
view raw IRB.rb hosted with ❤ by GitHub

Hope you enjoy this  😉