Neat tips and tricks for ruby

When in doubt look to Self

1.) Knowing what you have access to at any give time within your program is confusing for a new programmer. The context you are inside of each line of code changes as your code runs. For new programmers you can quickly lose your orientation within a program as you try and create more classes, methods, and require new files.

Luckily ruby comes with a special keyword Self. Self is your compass that references the scope you are in at any given time. You can call on Self from within a developer console, such as Pry during execution of your program to quickly orientate yourself within your program. Unlocking clarity to what you do and don’t have access too at any given moment to quickly analyze errors. Just call on Self.

Using & to create procs

2.) What is going on when you see the & symbol

For instance say I want to go through an array and capitalize each string in it I would normally do the following setup:

arr = [“luck”, “muck”, “puck”] arr.map { |word| word.upcase }

I can rewrite this block using a proc

arr.map(&:upcase)

But what is the & symbol actually doing?

The & takes any object and calls to_proc on it, and for the symbol class, the to_proc method give you a proc that, when given some object, will send itself(the symbol) as a message to that object.

behind the scenes the following is occuring:

arr.map { |word| word.send(:upcase)}

So we can use & to avoid tedious blocks that have a format such as

{ |x| do_something_with(x)}

for instance we can use the following code as an example:

(1..5 ).each { |x| puts x}

and can rewrite it (1..5).each &method(:puts)

Wait, isnt that line almost longer? yeah but noone said they were neccessary useful tips.

Fun with splats

3.) There is a lot more you can do with splats. For instance maybe you really hate the join method. Lets see if we can replace join with a splat.

arr = [“I”, “Am”, “an”, “array”] arr * “, “

“I, Am, an, array”

Nice, no more join(“, “)

We can also use the splat operator to soak up argument, for instance say we want to create a sum function

def sum(x, y, z)

x + y + z

end

values = [1,2,3]

and just pass it an array when we call the method

sum(*values)

you can send an array of values with * in front and also take values in an array and assign them to variables using the splat to soak up the remaining values.

arr = *(1..5)

=> [1, 2, 3, 4, 5]

first, *rest = arr

first => 1

rest => [2, 3, 4, 5]

Wow, splats are pretty powerful.

Chaining enumrators and gaining functionality

Another cool technique is chaining enumerators to gain more functionality and avoid writing ugly code. Suppose you want to reduce with some logic that needs an element’s index. The sloppy way would be to set an index counter

i = 0

reduce do |result, element|

# do something

i += 1

end

What you really want is to just be able to have access to the index from within your block, which reduce doesn’t have by default. You can do this through chaining.

[1,2,3].each_with_index.reduce do |result, (element,index)|

#do something

end

Sick…It almost feels like the sky’s the limit. Now go out and use your new found powers for good.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.