chap 1
attr_*
attr_accessor :engine allows you to read AND write to the variable @engine.
attr_reader :engine only allows you to read the value of @engine
attr_writer :age
# up is equal to bottom.
def age=(value)
@age = value
endattr_reader :age
# up is equal to bottom.
def age
@age
endattr_accessor :age
# up is equal to bottom.
def age=(value)
@age = value
end
def age
@age
endvirtual attribute/ setter / :method=
:method= assign attributes by =
:method assign attributes by ()
use var & @var in class
var & @var in classWith
attr_readerinside class, you can use bothelement&@element.Without
attr_readerinside class, you can use only@element.
&:symbol
block: {...} or do ... end
Proc: set block as a variable.
&Proc: return the original block.
Difference between Proc and lambda:
1. lambda checks argument numbers, and Proc not.
2. return in lambda means "leaving lambda"; return in Proc means "leaving the method.
define_method(*args)
define_method(*args)Defines an instance method for the receiver: args.
constantize(str)
constantize(str)tries to find a declared constant with the name specified in the string.
Inject and Reduce
Inject and Reduce are the same.
File
The second param is the starting point for the first one in File.expand_path.
$0 = name of the script being executed.
For example, when running ruby hello_world.rb, $0 = hello_world.
reference: http://periclestheo.com/2014/02/what-is-up-with-__FILE__-and-$0.html
const_get / send (fetch class/method by "string" or :sym)
check and return the constant
Invoke the method
include / extend
include provides instance methods for the class that mixes it in.
extend provides class methods for the class that mixes it in.
Last updated
Was this helpful?