Ruby Quickies #6

Is the line of code below valid Ruby code?

-> (a) {p a}[“Hello world”]

Yes, it’s valid. Here’s how to understand what it does:

The -> operator is often called the “stabby proc”, and in ruby, it creates a new Proc, which is one of Ruby’s function types.

It’s also called the “stabby lambda”, as it creates a new Proc instance that is a lambda.

All lambdas are Procs, but not all Procs are lambdas. There are some slight differences between the two.

This particular Proc takes one parameter (namely, a). When the Proc is called, Ruby executes the block


Ruby Quickies #5

it’s that simple!

Given:

x = “hello”

Can you explain the difference between:

x += “ world”

and

x.concat “ world”

Well, the += operator re-initializes the variable with a new value, so a += b is equivalent to a = a + b.

Therefore, while it may seem that += is mutating the value, it’s actually creating a new object and pointing the the old variable to that new object.

This is perhaps easier to understand if written as follows:

foo = “foo”
foo2 = foo
foo.concat “bar”
puts foo
# => “foobar”
puts foo2
# => “foobar”
foo += “baz”puts foo
# => “foobarbaz”
puts foo2
# => “foobar”

Examining the object_id of foo and foo2 will also demonstrate that new objects are being created.

The difference has implications in performance and also has different mutation behavior than one might expect.


Ruby Quickes #4

Consider:

def foo(x)
puts x * 2
end
puts foo 5 # → 10
puts foo(5) # → 10
puts foo (5) # → 10

Now consider:

def bar(y, z)
puts y + z
end
puts bar 1, 2 # → 10
puts bar(1, 2) # → 10
puts bar (1, 2) # Exception! --> syntax error, unexpected ‘,’, expecting ‘)’

The first time I saw this error raised, I was thrown off by the occurrence of ‘,’ thinking that ruby is now complaining about using a ‘,’ in between the arguments. …


Ruby Quickies #3

What is the difference between calling super and calling super()?

A call to super invokes the parent method with the same arguments that were passed to the child method. An error will therefore occur if the arguments passed to the child method

Also, a call to super doesn’t match what the parent is expecting.

But a call to super() invokes the parent method without any arguments, as presumably expected. As always, being explicit in your code is a good thing.


Ruby Quickies #2

yes, I do!

In Ruby code, you quite often see the trick of using an expression like:

array.map(&:to_s)

as a shorthand form of:

array.map { |element| element.to_s }

But, how exactly does it work?

When a parameter is passed with & in front of it (indicating that is it to be used as a block), Ruby will call to_proc on it in an attempt to make it usable as a block.

Symbol#to_proc quite handily returns a Proc that will invoke the method of the corresponding name on whatever is passed to it, thus enabling our little shorthand trick to work.


Ruby Quickies #1

In ruby, the operators “&&” and “||” in ruby statements (not if-stmts) have some special properties.

Let’s see how:

stmt1 && stmt2
returns the first falsey result, or if no falsey’s found, returns the last truthy result
example:

‘hello’ && ‘bye’
# => ‘bye’
‘hello’ && false
# => false
false && ‘hello’
# => false
nil && false && ‘hello’
# => nil
‘hello’ && false && nil && ‘bye’
# => false

stmt1 || stmt2
returns the first truthy result, or if no truthy’s found, returns the last falsey result
example:

‘hello’ || ‘bye’
# => ‘hello’
‘hello’…

The Opinionated Programmer #1

Warning: The following post is my personal opinion. If you feel I’m wrong or you disagree with me, please reply to this post and I’ll will be so glad to read your opinions.

Imagine with me for a second. One shiny morning, while working on your amazing project, written in ruby on rails, you needed to automate a job. Well, a job that you want to run may be one time every now and then. Your first choice is writing a rake task, right?

Fast forward .. 30 minutes later, you have your awesome rake task written and staring at…

ΛGHYΛD

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store