In Ruby, when we say ruby.upcase is telling the object ruby to do the message upcase( the method name ). However, when we say the method puts, Ruby already have the investable object “SELF” which we do not need to put in front of the method puts. So what is “SELF”? Self in Ruby is the default receiver for method calls which always reference the “current object”, and it change depending on where you are. When we puts self in Ruby, it will show the local object reference by self as main, and when we puts self.class into Ruby, it shown self’s Ruby class is an object. So in this case, self is the goest since we do not need to type self into Ruby.
Some people might ask if we ever need self in Ruby then. The answer is yes.
classSoftware
defself.print_developer
puts “The developer of this software is Jason”
end
end
Software.print_developer
In the context of a class, even self still refers to the current class in this case, which simply become an instance of the class Class now. Defining a method on self creates a class method. *self = Software
Working on a group project as part of the Building the Toolbelt of a Junior Ruby on Rails course that I am taking, I implemented the use of OmniAuth. I followed the example that we had learned in class. We have been using Rspec for our TDD, however, we had not learned the specific tests to write for OmniAuth. How hard could those be?
Working on my personal project this weekend, and feeling a little more comfortable with my coding.
However, tonight I found the new RSPEC error I had never seen before.
Failures:
1) Managing user New user can sign up
Failure/Error: click_link ‘Signup’
Capybara::Ambiguous:
Ambiguous match, found 2 elements matching link “Signup”
On my personal project Study Tool’s home page, I have 2 “Signup” links and the RSPEC don’t know which link to click on. My 1st solution is search online, which I tried first(‘.signup a’).click_link ‘Signup’
which raise another error
Failures:
1) Managing user New user can sign up Failure/Error: click_links ‘Signup’ NoMethodError: undefined method `click_links’ for #<RSpec::ExampleGroups::ManagingUser:0x007fe868898c28>
Then I saw Anthony was on Slack by the time and start chat with him about this errors.
First, he suggest me to save_and_open_page and see if you see multiple links on the page. Even I already know I have two links of ‘Signup’, I still use the save_and_open_page to see this on the page.
Anthony also suggest me first(:link, link).click in RSPEC, so I change it into first(:link, link).click_link ‘Signup’ , and it raise another error
Failures:
1) Managing user New user can sign up
Failure/Error: first(:link).click_link ‘Signup’
Capybara::ElementNotFound:
Unable to find link “Signup”
We tried to change the line into first(:Signup, Signup).click, and it raise another error
Then we check on the Inspect Element on this two ‘Signup’ link , and change the line as second(:link, ‘Signup’).click this time. However, it still raise another error
undefined method `second’ for #<RSpec::ExampleGroups::ManagingUser:0x007fb3ff4fe718>
I don’t want to change my app for the spec since is a really good way for me to learn RSPEC. I did some research on Capybara, and read this blog http://techblog.fundinggates.com/blog/2012/08/capybara-2-0-upgrade-guide/ , and I’m start thinking, what if I point it to the link first, than tell it to click the link? so I try
first(:link, 'Signup').click
click_link 'Signup'
Yeah! I finally got this error fixed! Now I can have a nice sleep ;D
Rails is a skeleton web-application framework, it base on Model-View-Controller (MVC) pattern includes everything that needed to create database-backed web applications.
To create a new rails app type the following in the terminal: rails new <apps-name> -T -B, the -T is skip Testing, -B is skip Bundle since we might need to adding some more gem later.
rails g/generate, it generate new basic code for us into all different files.
rails c/console, it start the rails console. Rails console is basically IRB in Ruby, but rails c is for Rails.
rails s/server, it start the rails server. Which give us access to localhost:3000 in the browser of the web we just create.
There were 3 main directories under app directory we will be focus on, which are controllers, models, views. Yes, this are the MVC pattern.
Gemfile is also really important part of this app. It tell the rails app what gems that we depends on in order to run. We’ll need the rails gem to run rails, then we also need sqlite3 gem to cornet to sqlite database, and all other gems that we needs depend on our app. After we finish adding all the gems that we need, we will need to run bundle or bundle install to install all the apps.
As a beginner developer in ruby on rails. We learned how the webs work with MVC, CRUD, linking the Web into Apps (Rails in this case) which including User, Browser, Router, Controller, Model, View, Data Base. I’ll talk about CRUD in my future blogs in Rails. Today I want to focus on the three different stage: Development, Production, and Testing.
Development, which is the local area when wrote our code, some time it will be internal website for people who authorize to CRUD. Normally we use ‘rails s’ or ‘rails service’ in our terminal/I-term to pull up the service, than type in ‘localhost:3000’
Testing area is complete different stage area for test purpose to test if the web will be function or not. If you use Rspec TDD, it would even lead you of how to code your web as well. Professor Jason Noble’s favor quote of TDD is ‘RED, GREEN, REFACTOR’ (Red means write a fail test, Green means make the test pass, Refactor means make the code more professional such as take care of DRY codes).
Production, usually is the finished life website for the clients.
Please let me know if you have any thoughts of this stage areas.
Jeff Casimir talked about the LSAT (Law School Admission Test) is required for admission to most law schools and is offered four times each year. I feel this depends on how we are using the TEST. Of course if the test is just sort out students different level, and put those students who have the same level as a group to study would be really helpful. However, if this required test going to take away of those who don’t have any knowledge but really want to try and open a door for new career, I would not agree with this action. I understand this world there is nothing fair, but at least, we should all have a chance, a key, maybe a little guide to help us to see the light.
“There are many people, programs, and instructional tools that will tell you anyone can program.” That’s true. Now a day, there are a lot of people out there in the programing field that study on their own with some books, online programs and jump on working, earning experience. However, how many of them have enough foundation to understand how their code works? When the bugs kick in, what really happens and how to fix it? I agree of Jeff’s “That’s true in the same sense that just about anyone can write a book, but not all of us should be authors. The art of crafting characters, a dramatic arc, and life-like dialog is the work of those with an aptitude for storytelling combined with long hours of practice.” A true story, one of my friend who in the field for a while, but some times I can impress him with some basic skills that I have learned in class. When he shown me the company code, I almost cry~ most of them make no scene to me and he cannot even fully explained to me what is that code doing. He told me that he use a lot of online reference to put all the code together, but one big issue is that it is not fully his own coding style, so many bugs that just take hours, days, weeks, even years to fix…more over, is just temporary fix. It can be break down again after 2 hours, or 3 month later. Such as a writing a book, you can put 10 different people’s story together as a story. However, can I ask what is your soul, your spirit, your purpose of this story? I believe that an authors have their own purpose on each book they wrote with their soul, and spirit support. I think same as programmer, we need to have our patience to understanding what we are doing. Write a program is kind of raise a child. We need to step by step, lead them to what needs to be done by break them down into little piece by piece. Put them together logically.
“Anyone can write a program, but not everyone can be a programmer. A programmer is a person who uses software to solve problems.” This part I don’t really agree. Is it really anyone can write a program?
Do you mean anyone can write a functioning program? Each person on earth have their special unit skills for sure, but I believe some people are just won’t able to write a program that would function. Such as some people cannot do match, some people cannot draw etc. However, I do believe “a programmer is a person who uses software to solve problems.” because making software to solve problems is the purpose for programming language exist, at least this is how I believe. What do you think?
I believe I can be what I want to be, I believe I can be a programmer. Just depends on how I find the way to build up my foundation, how I walk my path into this field, and find the best way to study more, practice more. For me, believe is a magic, is a power to lead me forward. You never know if that 90% of brain usage will open your path with magic until you try and try hard, right? We won’t have light balls today if Thomas Edison don’t believe in himself. Thomas Edison said that “Genius is one per cent inspiration and ninety-nine per cent perspiration. Accordingly, a ‘genius’ is often merely a talented person who has done all of his or her homework.” And I believe a good programmer just need to study hard, practice more, and believe in yourself. Please and you are welcome to share your thoughts.
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?