# this:classPersonattr_reader:nameend# is the same as:classPersondefname@nameendend
attr_writer is shorthand for a setter method:
1234567891011
# this:classPersonattr_writer:nameend# is the same as:classPersondefname=(str)@name=strendend
attr_accessor is shorthand for both attr_reader and attr_writer:
123456789101112131415
# this:classPersonattr_accessor:nameend# is the same as:classPersondefname@nameenddefname=(str)@name=strendend
attr_accessible is part of ActiveRecord, and lets you whitelist attributes
that you can use with mass-assignment
attr_protected is part of ActiveRecord, and lets you blacklist attributes
that you can use with mass-assignment
Note 1: do not use attr_accessible and attr_protected together.
You must use one or the other.
Note 2: Rails 4 has moved away from attr_accessible and attr_protected and
instead uses the concept of strong parameters, where you explicitly whitelist
params inside the controller.
Example:
include is used to make a module’s methods instance methods for a class
extend is used to make a module’s methods class methods for a class
123456789101112131415161718192021222324252627
moduleExtendabledefa_class_method"I'm a class method"endendmoduleIncludabledefan_instance_method"I'm an instance method"endendclassClassWithExtendextendExtendableendclassClassWithIncludeincludeIncludableendClassWithExtend.a_class_method#=> "I'm a class method"c1=ClassWithExtend.newc1.a_class_method#=> NoMethodError: undefined method `a_class_method' for #<ClassWithExtend:0x007fa7f24cc740>c2=ClassWithInclude.newc2.an_instance_method#=> "I'm an instance method"ClassWithInclude.an_instance_method#=> NoMethodError: undefined method `an_instance_method' for ClassWithInclude:Class
(1..3).inject(100){|sum,n|sum+n}#=> 106, this is the same as 100 + (1 + 2 + 3)
Instance Methods
How to get the instance methods of an object:
12
a=Array.newa.methods
Loading Dev DB Schema into Test
1
rakedb:test:load
Loops
12345678910111213141516171819202122232425
# these loops all execute 5 times# while loopi=1whilei<=5puts'this is a while loop'i+=1end# do while loop (begin/end/while in ruby)i=1beginputs'this is a do while loop'i+=1endwhilei<=5# for loopforiin1..5puts'this is a for loop'end# looping with a times block5.timesdoputs'this is a times block'end
Random Numbers
Generating random numbers:
1234567
rand# a random percentage between zero and onerand(5)# a random integer between zero and four (note that five is excluded)rand(1..3)# a random integer between one and threerequire'securerandom'SecureRandom.hex[0..5]# outputs a random hex string of length 5
Range to Array
Convert a range to an array:
1
(1..10).to_a#=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Retry
(from tutorialspoint)
If retry appears in rescue clause of begin expression, restart from the beginning of the 1begin body.
123456
begindo_something# exception raisedrescue# handles errorretry# restart from beginningend
If retry appears in the iterator, the block, or the body of the for expression, restarts the invocation of the iterator call. Arguments to the iterator is re-evaluated.
123
foriin1..5retryifsome_condition# restart from i == 1end
There’s an example in the documentation
Use let to define a memoized helper method. The value will be cached
across multiple calls in the same example but not across examples.
Note that let is lazy-evaluated: it is not evaluated until the first time
the method it defines is invoked. You can use let! to force the method’s
invocation before each example.
Setting focus options
1234567891011
RSpec.configuredo|config|# ...# only run specs where the focus option is set# (or run all of them if no focus option)config.filter_runfocus:trueconfig.run_all_when_everything_filtered=true# ...end
Stub Chain
12345678910111213141516
# stub_chain takes in a list of subobject names, and one method name# returns an object with each of those subobjects nested into each other# and mocks a method that returns nil for the last argument# eg - obj = stub_chain(%w{ account sms messages create })# returns an object that stubs this method call:# obj.account.sms.messages.create# and returns nildefstub_chain(method_chain)returnnilifmethod_chain.empty?next_object_name=method_chain.shiftobj=Object.newobj.stub(next_object_name.to_sym).and_return(stub_chain(method_chain))objend
Rspec with Capybara
Use save_and_open_page to help debug integration tests: