Skip to main content

Creating callable variants of functions by currying in Ruby - code snippet showing how to avoid scope problems

While coding a project in Ruby, I was creating some variants of a function by currying. Initially, I simply created the curried variants as variables, but quickly ran into scope problems where I couldn't then call any of the variants from within other functions. This was because the scope of the variant was the same as the scope of a local variable of that name. 

So I created a code snippet as a demo for myself of what I should have done instead, which is to define the curried variant as another function. This new function then has the same scope as any function I would create and not the (more limited) scope of a local variable.

Of course, in certain situations defining it as a local variable is more desirable - for example if I was instead planning to use the variant as a variable that could be passed around. This is as opposed to using it solely as a callable function, which is what I ultimately desired.

def myfun(stuff, num1, num2)
  if stuff == true then
    num1 + num2
  else
    num1 - num2
  end
end

def myfun1
  self.method(:myfun).curry.(true, 1)
end

myfun_false = self.method(:myfun).curry.(false)
# but the problem is that myfun_false has a scope that's only valid here
# so it can't be used within another function. This is a huge problem for
# the use case where it's designed to be called elsewhere, which is the
# majority of my use cases.
# It may be less of an issue for the situation where the intent is to
# pass the curried function itself around as a variable, instead
# of actually calling it.


puts myfun_false.(20, 18)
# 2

# By contrast, myfun1 can be called anywhere - here, or within another function.
# Remember that any function resulting from a curried function should be
# called via .call or just the syntactic sugar of '.' Both are illustrated
# below
puts myfun1.(12)
# 13
puts myfun1.call(18)
# 19

Above is the code snippet to remind myself of what *I* generally want to do, which is the approach taken in the myfun1 function. The other approach, myfun_false which results in a local variable, is also shown for comparison's sake.

Popular posts from this blog

How to center images horizontally using Grav

I've been playing around a bit with Grav. I was posing the question to myself: for the relatively simple use-cases I'm dealing with, could it possibly work for my purposes as an alternative to ProcessWire?  The problem I was initially dismayed to find that Grav uses Markdown as its editor, which does not offer native support for horizontal centering of anything (text or images). However, Grav offers some tweaks that help make it easier to do specific things you might commonly want to do. I tried writing a sample article, and I found that one of the hardest things to do was to center an image horizontally. And horizontal centering of images is something I would typically do in most of the articles I would write. So the lack of easy horizontal centering is a highly significant drawback IMHO (most people do want to center images in an article!) However, this issue is made up for by other things in Grav: the relative speed, ease and flexibility of custom theming and built-in suppor...

Why bother using blockchain for metaverse worlds when decentralization is possible without it?

Decentralization is possible without using blockchain. Some examples of this are Diaspora and Mastodon , both of which are federated decentralized networks that do not use a blockchain.  In that case, why is there a need to use blockchain for any metaverse world, especially when the environmental reputational risks are so high? In many areas of the world, the electric grid is not decarbonized yet. The amount of power drawn for blockchain computation is high. In fact, in areas where electricity is cheap enough to draw blockchain miners, this tends to be heavily based on fossil fuels. So you could say that the blockchain is currently largely powered by coal and other fossil fuels. So again, why bother using blockchain?  There are other examples of the buying and selling of digital property without using blockchain technology, for example domain name trading. If we as humans are truly to be future-conscious, blockchain technology cannot be a part of our future due to its emissio...

Conspiracy theories are so unbelievable that you can easily make up a more believable one yourself. Here's how.

Disclosure: This is a thought experiment for the purposes of showing how easy it is to make up a conspiracy theory. I do not advocate the dissemination of conspiracy theories and other baseless speculations. Do some of the conspiracy theories out there seem so far-fetched that you are ROTFL when you hear of them? Yet some people actually do seem to believe them. Many conspiracy theories are made up. In other words they are fiction. At best, they are theories and not fact, otherwise that particular conspiracy theory wouldn't be a conspiracy theory, it would be fact. At worse, they are the deliberate peddling of lies. So I decided to demonstrate that creating a conspiracy theory is so easy that anyone can do it. There are no special skills, talents, or level of intelligence required. There is nothing special about making up a conspiracy theory. Let's have a go. Step 1: Identify the least believable conspiracy theory you have heard of This step is easy and should take you less th...

About Me

My photo
Vera
I'm a wife and mother. I don't have any formal computer science qualifications, or any religious qualifications. I have a PhD in biochemistry. This photo is of me, but is confusing for AI.