git remote add. git push origin master

What exactly do these mean?

Briefly:

1. git remote add origin git@github.com:snowcrash/my_app.git

git works locally but to connect to other repositories (or repos) it uses the term  remote which are repos on other servers that you can push your changes to (so other people can see them) or pull updates (from other people).

This command creates a new remote called origin (by convention but could be something else) which is located at the github URL (again, it could be another server) called git@github.com:snowcrash/my_app.git.

2. git push origin master

OK, so you’ve defined a new remote called origin. This command simply pushes your master branch to this server.

Note: in future, you just do:

git push

to push your master branch to origin. This is because:

– if no remote is specified, the remote of the current branch is used. If that’s not set up then origin is used

– if there’s no refspec specified (i.e. master, some-branch) then git pushes every local branch that has the same name as the branch on th remote.

To avoid accidentally pushing other branches it’s a good habit to use the full version, i.e. 

git push origin master

despite it being a bit longer.

Seeing these settings:

1. All of this information is stored in your local git repo in this file:

.git/config

e.g. details of your new remote will be on the line following:

[remote “origin”]

2. Also, you can use the following to list your remotes:

git remote

and to see the URL that git has stored for the short names:

git remote -v 

To find out more information about a remote:

git fetch origin

This fetches all the data about that remote project that you don’t have.

For more information about this remote:

git remote show origin

This lists the URL for the remote repo as well as the tracking branch information.

Finally, you’ll want to be able to pull from the remote. Just use:

git pull

The git book is here:

http://git-scm.com/book/ch2-5.html

which contains lots of useful examples including how to remove and rename remotes.

There’s also a great post here:

http://stackoverflow.com/questions/5617211/what-is-git-remote-add-and-git-push-origin-master

Google Webmaster Tools

==== UPDATE ====

Google have just released their new and improved navigation.

However, much of the comments I had about the last version still apply.

In addition I would add that changing the UI confuses people. You have to dig around again to try and find things.

Bit of an odd bag.

Crawl Stats 

This gives you a progress report on how far Google has progressed with a crawl.

Now under Crawl (was under the Health category.  

It would make more sense to split this out into a Status (or perhaps Statistics) category.

Sitemaps

Sitemaps lets you add or test a site map. It also, importantly, shows you the current status of how much of your site has been indexed (i.e. how many submitted links have been indexed by Googlebot) (as mentioned below, this differs from the total number shown in Index Status – odd). If the search engine hasn’t started indexing you’ll see a “Pending” under the Indexed column.

Now under Crawl (was under Optimization).

Surely it would make more sense to go under Configuration!

Index Status

Index Status is a graph showing the total number of pages indexed. Not particularly useful other than to refer to once in a blue moon to check totals add up. 

Oddly the total number of pages shown as indexed here does not equal the total number of pages indexed via Sitemaps.

Location:  now under Google Index (was under Health).

Surely it would make more sense to be in a Status (or Statistics) category. I don’t see how the number of pages indexed had anything to do with Health. 

Generally speaking, perhaps Google should add a generic Status category.

Google seem to be able to create awesome tools but really poor product designs or user interfaces.

Using active_support to get second, third

Great post here about how to get second and third using Active Support. i.e. use

require ‘active_support/core_ext’

The moral of the tale though is that using an indexer (i.e. people[2] instead of people.third) is probably preferable apart from cases like people.first (which comes with Array anyway).

http://stackoverflow.com/questions/16821491/ruby-odd-behaviour-with-second-third-objects-in-an-array/16821624?noredirect=1#16821529

Calling a controller from the Rails console

Fire up your console:

rails c

then:

app.get ‘/posts/1’

response = app.response

response.body # if you need the HTML body

response.cookies # if you want a hash of the cookies

Plenty more here:

http://rails.rubyonrails.org/classes/ActionController/Integration/Session.html

and here:

http://stackoverflow.com/questions/151030/how-do-i-call-controller-view-methods-from-the-console-in-rails

However, if you want to do some serious debugging you’ll probably want to use pry. See an earlier post.

Ruby: idiomatic code

Here’s an example of making some not-particularly idiomatic Ruby code into something that’s a little more “Ruby’ish”:

Non-idiomatic code:

class Person 

    attr_accessor :id, :name

    def initialize(init)

        init.each_pair do |key, val|

            instance_variable_set(‘@’ + key.to_s, val)

        end

    end

end

@adam = Person.new(:id => 1, :name => “Adam”)

@eve = Person.new(:id => 2)

@people = [ @adam, @eve, nil ]

print @people

puts

@people.map! do |person|

    person ||= Person.new(:id => 3, :name => “Some default”)

    if person.name.nil?

        person.name = “Eve”

    end

    person

end 

print @people 

And the idiomatic version:

class Person

  attr_accessor :id, :name

  def initialize(init = {})

    init.each do |k, v|

      send(“#{k}=”, v)

    end

  end

end

people = [ 

  Person.new(:id => 1, :name => “Adam”), 

  Person.new(:id => 2), 

  nil,

]

print people 

puts

people.map! do |person|

  person ||= Person.new(:id => 3, :name => “Some default”)

  person.name ||= ‘Eve’

  person

end

print people 

Notes:

1. we’re initialising init to an empty hash here:

  def initialize(init = {})

2. ‘send’ invokes the method identified by the symbol passing it any arguments specified.

http://ruby-doc.org/core-2.0/Object.html#method-i-send

Here it is called on self which is an instance of Person.

3. people is created with Person.new code inline

4. we can save 2 lines of code by using the same ||= operator for Eve

All in all, a lot tidier.

For more on the send method, see this post:

http://stackoverflow.com/questions/12892045/send-method-in-ruby

which explains this, initially obtuse, code:

x = [1,2,3]

x.send :[]=,0,2

x[0] + x.[](1) + x.send(:[],2)

Ruby: idiomatic code

Here’s an example of making some not-particularly idiomatic Ruby code into something that’s a little more “Ruby’ish”:

Non-idiomatic code:

class Person 

    attr_accessor :id, :name

    def initialize(init)

        init.each_pair do |key, val|

            instance_variable_set(‘@’ + key.to_s, val)

        end

    end

end

@adam = Person.new(:id => 1, :name => “Adam”)

@eve = Person.new(:id => 2)

@people = [ @adam, @eve, nil ]

print @people

puts

@people.map! do |person|

    person ||= Person.new(:id => 3, :name => “Some default”)

    if person.name.nil?

        person.name = “Eve”

    end

    person

end 

print @people 

And the idiomatic version:

class Person

  attr_accessor :id, :name

  def initialize(init = {})

    init.each do |k, v|

      send(“#{k}=”, v)

    end

  end

end

people = [ 

  Person.new(:id => 1, :name => “Adam”), 

  Person.new(:id => 2), 

  nil,

]

print people 

puts

people.map! do |person|

  person ||= Person.new(:id => 3, :name => “Some default”)

  person.name ||= ‘Eve’

  person

end

print people 

Notes:

1. we’re initialising init to an empty hash here:

  def initialize(init = {})

2. ‘send’ invokes the method identified by the symbol passing it any arguments specified.

http://ruby-doc.org/core-2.0/Object.html#method-i-send

Here it is called on self which is init.

3. people is created with Person.new code inline

4. we can save 2 lines of code by using the same ||= operator for Eve

All in all, a lot tidier.

For more on the send method, see this post:

http://stackoverflow.com/questions/12892045/send-method-in-ruby

which explains this, initially obtuse, code:

x = [1,2,3]

x.send :[]=,0,2

x[0] + x.[](1) + x.send(:[],2)

Ruby: completing object initialization

Say you’ve got some partially initialised objects and want to complete the initialisation. 

Here’s an example: 

class Person 

    attr_accessor :id, :name

    def initialize(init)

        init.each_pair do |key, val|

            instance_variable_set(‘@’ + key.to_s, val)

        end

    end

end

@adam = Person.new(:id => 1, :name => “Adam”)

@eve = Person.new(:id => 2)

@people = [ @adam, @eve, nil ]

print @people

puts

@people.map! do |person|

    person ||= Person.new(:id => 3, :name => “Some default”)

    if person.name.nil?

        person.name = “Eve”

    end

    person

end 

print @people 

This prints out:

[#<Person:0x007faf2bae37d8 @id=1, @name=”Adam”>, #<Person:0x007faf2bae3648 @id=2>, nil]

[#<Person:0x007faf2bae37d8 @id=1, @name=”Adam”>, #<Person:0x007faf2bae3648 @id=2, @name=”Eve”>, #<Person:0x007faf2bae3328 @id=3, @name=”Some default”>]

Notes:

1. the map! will directly change the contents of @people

2. you need to have ‘person’ at the end of the map! method as the value returned will be used to change the value of person. And if you don’t have it there, the next return value would be “if person.name.nil?” which generally returns nil. Probably not what you want

3. Finally, ‘return person’ would not work as you’d just break out early. See: 

http://stackoverflow.com/questions/4601498/what-is-the-point-of-return-in-ruby

4. Finally, this isn’t the most idiomatic Ruby code. Do a search for idiomatic to find a more Ruby’ish version.