Ruby: attr_accessor, attr_reader, attr_writer

Accessors are sometimes known as getters/setters or mutators.

They let you get/set instance variables.

In Ruby attr_reader, e.g.:

attr_reader :age

is equivalent to:

def age

    @age

end

Breaking it down – the :age is a symbol named “age”. Symbols have the feature that any two symbols named the same will be identical (they use the same pointer). 

This is not the same in a string.

This means comparing them is super fast – you just do a pointer comparison.

Also, symbols are immutable – again, not like strings.

 http://stackoverflow.com/questions/6337897/what-is-the-colon-operator-in-ruby

So, writing attr_reader :age gets translated into code that returns the instance variable @age.

And, attr_writer :age gets translated to:

def age=(value)

  @age = value

end

attr_accessor :age

just combines the two.

http://stackoverflow.com/questions/5046831/why-use-rubys-attr-accessor-attr-reader-and-attr-writer 

and

http://stackoverflow.com/questions/4370960/what-is-attr-accessor-in-ruby?lq=1

Ruby: how the interpreter parses your code

I wasn’t sure exactly how the Ruby code I’d written was being interpreted.

For example, 

class TestClass

def initialize(x = 1)

@x = x

end

puts “inside the class”

end

a = TestClass.new(5)

puts “declared an instance”

Now, does this print “inside the class” or “declared an instance” first?

The answer is “inside the class”.

See also this post describing using Ripper to parse code:

http://www.rubyinside.com/using-ripper-to-see-how-ruby-is-parsing-your-code-5270.html 

rvm: Permission denied

Getting this error?

$ gem install passenger

Fetching: passenger-4.0.5.gem (100%)

ERROR:  While executing gem … (Gem::FilePermissionError)

    You don’t have write permissions for the /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195 directory.

[scripts (master)]$ ls -ld /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195

drwxrwsr-x  8 root  rvm  272 21 May 16:07 /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195

Changing permissions fixes things:

sudo chown -R <user-here>:<group-here> ~/.rvm/

A StackOverflow discussion on a slightly different aspect:

http://stackoverflow.com/questions/4303088/permission-denied-error-with-rvm

Rails: Phusion Passenger

Passenger is an application server for Ruby (Rack) and Python (WSGI) apps.

Some notes on installing it:

https://www.phusionpassenger.com/download

Ignore the docs that suggest:

$ sudo gem install passenger

$ sudo passenger-install-apache2-module

Instead use:

gem install passenger

passenger-install-apache2-module

Then paste the LoadModule code it spits out at the end into your Apache conf file.

Test whether Passenger has installed by running:

curl -sI localhost | grep ^Server

and check for the “Phusion_Passenger” signature. E.g. 

Server: Apache/2.2.22 (Unix) DAV/2 PHP/5.3.15 with Suhosin-Patch Phusion_Passenger/4.0.5 mod_ssl/2.2.22 OpenSSL/0.9.8x

If you need to uninstall Passenger using sudo then:

$ sudo gem uninstall passenger

And a discussion on whether to use rvmsudo or not here:

https://groups.google.com/forum/#!topic/rubyversionmanager/9dnmAsTiHR4

In short, https://www.phusionpassenger.com/download

suggests using sudo but the rvm page suggests not:

https://rvm.io/integration/passenger

Note:

If you get a warning doing the passenger install apache2 module like this:

rvmsudo rvm get stable && rvm reload && rvmsudo rvm repair all

Warning: can not check /etc/sudoers for secure_path, falling back to call via /usr/bin/env, this breaks rules from /etc/sudoers. Run:

    export rvmsudo_secure_path=1

Then run:

    export rvmsudo_secure_path=1

and:

rvmsudo rvm get stable && rvm reload && rvmsudo rvm repair all

(remember to run rvm requirements afterwards)

On the Mac the Apache conf and restart commands go like this:

[~]$ sudo vim /etc/apache2/httpd.conf

[~]$ sudo apachectl restart

See also:

https://developer.apple.com/library/mac/#featuredarticles/PhusionRails/_index.html

and this very useful guide:

http://jasoncodes.com/posts/mac-os-rails-server