Skip to main content

Posts

Showing posts with the label ruby

More callable variants of functions in Ruby

 This is a slightly more advanced version of my previous post on creating callable variants of functions in Ruby while avoiding scope problems . In today's instance, I was creating an error reporting system for a program I was writing.  The error reporting, of necessity, had to emit a message to indicate what was going on. While some errors would be unique messages that only occurred once during the program, other errors could be grouped. For example, one group was "incorrect number of parameters supplied". There were several error messages within my program that fell into this category, so I didn't want to type out the exact same wording each time. I wanted to create a function that could report this sort of error indicating where it happened (of course I had to supply parameters to the messages to show the context). So here is a very basic sketch of the sort of thing I was doing. I'm writing this here so that I can go back to it easily later. The item I'm re

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

Ruby - 'reduce' or 'inject' applying a string function, a quick code snippet example

I was getting my head around the reduce method (also known as inject ) in Ruby for some code I am writing. I suspected that this would be what I really wanted to use with my array instead of the more generic each which first sprang to mind. So I was eager to try out the concept with reduce and ready to code a quick separate sketch of the outline for my particular use case. However, most examples of reduce out there are mathematical in nature and this makes it harder to mentally translate to string use examples.  In my case, I'm using the string to stand in for a more complex data structure, so I didn't want to have to mentally translate from numbers to strings to higher order structures. The Ruby documentation for reduce/inject was thorough but I struggled to wrap my head around their terminology, especially the word "memo". I finally realized that "memo" simply means the changing result. That seemed to do the trick mentally.  Since most of the functio

Ruby IO - file pathnames for use on different operating systems

I had finished writing a piece of software in Ruby in for Linux and wanted to test it also on PC. Since the software involved reading in some data from files, I wanted to be sure that the pathnames could be handled on PC. In Lisp, this is handled with relative ease where it's possible to specify the subdirectory of interest, and the file(s) within that, and it's up to the Lisp implementation to handle the differences between different operating systems.  I was rather expecting something similar for Ruby (given its influence from Lisp), but to my surprise, the official documentation on the File class had no information that I could find as to how pathnames would be handled differently on different operating systems.  Then I looked at the docs for the Ruby IO class , and found the information there. It says: Ruby will convert pathnames between different operating system conventions if possible. For instance, on a Windows system the filename "/gumby/ruby/test.rb" will

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.