Ruby Metaprogramming Part 2

We continue with more reflection methods in Ruby like instance_variables, local_variables, constants for variables and public_methods, private_methods, method_definded? for methods. Note: This is part 2 of metaprogramming in Ruby, in part 1 we covered evaluating strings and blocks with instance_eval and such. Reflection Methods…

Ruby Metaprogramming Part 1

define_method, Kernel.eval, instance_method, instance_eval, class_eval, instance_exec, send, bind. Have you seen Ruby programs with these methods or haven't been sure what each of them do exactly? By the end of this post you will be able to confidently read Ruby code that contains these…

Ruby Method Object, define_method and bind

Ruby methods are executable language constructs. They are not objects. But we can get objects that represents methods with the Object class's method method. Note: This is similar to how blocks are language syntax but not objects in Ruby and we can get object versions of blocks in the forms…

Difference Between Ruby Blocks, Procs, and Lambdas

If you're new to Ruby or if you've been using Ruby for a while but want to understand procs and lambdas a bit deeper, this post is for you. We will look at the Proc object, four ways to create procs and lambdas, see how to invoke them, and look…

How &: Works in Ruby

Chances are you've seen and written code like users.map(&:first_name) in ruby or rails applications many times. If you're new to Ruby, you may have wondered about this strange syntax. Let's demysify &: and explain exactly how it works. First we start with blocks and how to…