Ruby Bits 2
- Level 1
- Procs are for storing some kind of function, that will be called later with a
yield
in some other block- can be done by:
name = Proc.new do ; # code here; end
.And then called withname.call
, and remember that a proc being called can take an argument that it would do something with: i.e.name.call(operand)
is fine - if you make a lambda with
name = lambda do ; # code here ; end
, it can be called the same way, withname.call
- shortcut for
lambda
is->
- shortcut for
- procs make the invisible-like argument passing of a block to a method containing a yield, into a visible passing of a variable assigned to a block, which will then be called. (could thereby pass in multiple procs at once, though)
- can be done by:
- Some methods expect block objects (like
each
andmap
), if you’ve assigned some block to a procmy_proc
, then pass that proc as an argument with an ampersand like:array_of_stuff.each(&my_proc)
- you can also define a method to expect procs:
def method(&var)
, (that is convert a block into a proc) and then call it within the method with another ampersand – this might be particular to the each method…not sure - The ampersand can come in handy if want to call a pre-existing method within an iterator
- instead of doing
list.map { |each| each.method1 }
you can now dolist.map(&:method1)
where the method’s name as a symbol will be the name of the method searched for- the only disadvantage is that you cannot call methods on the symbol
- instead of doing
- you can also define a method to expect procs:
- in a constructor, by making an object yield itself in the initialize method, it makes itself available to being constructed in a block
- you can do:
Thing.new do |object| ; object.parameter = "high" ; end
as long as in the class’sdef initialize
you do something likeyield self if block_given?
- you can do:
- they call it Closure: cool thing you can do is first decorate a lambda with a method that takes a parameter and puts it inside a lambda. then, the method can be called with a parameter, making a version of a lambda. That particular version of the lambda can be called by giving some object to it, creating outputted possibilities with 2 degrees of difference
- Procs are for storing some kind of function, that will be called later with a
- Level 2
Struct.new(:var1, :var2)
gives the same functionality as making a class, usingattr_accessor :var1, :var2
, and assigning local variables to the instance variables in theinitialize
method.- additional methods can be added via a block after the Struct constructor
- good with data handling, rather than behavior
alias_method :new_method, :old_method
makes a copy of a preexisting method. Great for “reopening” methods, just make a copy of the old one, and restate it in the new one. But at the same time… you could just make a new class, inherit from the old, and callsuper
from within the redefined and repeated method in the new class- define_method is the way that you would create a bunch of really similar methods
- when using &block with it, things get confusing