Different ways to invoke a method in Ruby

  1. (Single run time) Using dot notation and calling the method on the object:
  2. (Persist name of method to database) Using send method:
  3. Get the method and call it like a Proc:
  4. Not good practice but useful sometimes. (Self-modifying method code):

Limit a method’s access in Ruby

To limit a method’s access we use 3 methods: private, protected and public.
Public: By default all methods defined are public. All instances of a class can use these public methods. Public methods are called with a explicit receiver.
Private methods are the ones defined under the private keyword and they can only be used within the class definition. The receiver in private methods is self. We use private methods for internal usage without a receiver.
Protected methods are similar to private with addition that it can be called with or without an explicit receiver but that receiver is always self or an object that inherit from self. We use protected methods defined as self.my_method for internal usage in other classes whenever inheritance isn’t used.

Differences between class, object and module in Ruby

Ruby is a class-based object-oriented programming language. Meaning that every object is an instance of a class, and a class defines the state (variables) and behaviours (methods) of an object. An object is an entity with state and behaviour, as defined by its class.

Object is an instance of a class which allows you to use the classes attributes and methods. Object is the default root of all Ruby objects. Everything in Ruby is an object, including classes. An object in code is a thing with all the data and all the logic required to complete a task. Objects are models and metaphors for the problems we solve in code. Ruby comes with a few types of Objects to get us started, things like Integer, String, Array, etc. We call these base types of Objects “Primitives.” But what if we wanted to create a new type in our programming universe, a new kind of object for our code? That’s what the class keyword and object orientation allows us to do.
Class is a type of structure of information and code. A Ruby class is an object of class Class, which contains all the object things plus a list of methods and a reference to a superclass. A class is the blueprint from which individual objects are created.
A module is a collection of methods and constants. They hold methods, like classes but they can’ t be instantiated. It is not possible to create objects from a module. Modules are useful when we have methods that want to reuse in certain classes

Stubs, mocks, spies and fakes

Minimal implementations of interfaces or base classes
A Spy will record which members were invoked
More complex, a fake may resemble a production implementation
It is usually dynamically created by a mock library and depending in its configuration a mock can behave like a dummy, a stub, or a spy.
They all relate to functions, objects and other types.
Stubbing is a way to provide dummy data/info instead of making the calling to the actual DB
It is for example a function which you create inside the test file to mimic the real function or be a simplified version of the actual function.

Thank you post to Flatiron

I’ve completed the self-paced Online Software Engineering bootcamp with Flatiron School and through that year I was able to gain a “tech family” and grow as a developer.

My background is in the art market and it was not easy for me to get into the dev world. I was a new mum and had to take care of my daughter too. My time was limited and I got quite often long periods of baby sickness, lots of until late study nights and “I am not going to get this” tears but I finally graduated! Flatiron School was helping me through my journey with their technical help, ask a question feature and lessons.

I started to look for a job in January and 5 weeks later¬†I’ve accepted an amazing job offer. During those weeks I was doing tons of code challenges, lots of interviews and getting rejections almost every week. But I had Emma helping me! She was my career coach. She was helping me with my emotions, with my interview skills, and giving me good advice. THANK YOU Emma!

I am not sure that this blog post is going to comply with Flatiron requirements of a one technical blog post but I can treat it as an algorithm to become a developer:

  1. Make sure your passion is coding.
  2. Enroll at Flatiron.
  3. Study, be curious, share your knowledge, have fun with side projects too.
  4. Graduate.
  5. Land your dream job as a dev and start an amazing new career!

Virtual environment (Python)

Create a virtual environment for your Python app in a few steps. (For Linux and OS X). Go inside the directory where you want your virtual environment and type in the terminal:

This command will create a directory called myvenv with the virtual environment.

Start your virtual environment by running:

Now that you inside the virtual environment (you will know it because the prompt of the console is prefixed with ( myenv )) you can install whatever you need using pip. To get the latest version of pip type:

When working within a virtual environment, python will automatically refer to the correct version so you don’t need to specify anything.

Create a requirements.text file and add packages inside.

Run the following to install them:

The end! Happy coding!

Junior developer interview questions

I am looking for my first job as a Junior developer and during my interviews I was asked the following:

  • Difference between === and ==:
    • === Compares the value ad the type (true / false).
    • == Compares only type.
  • Difference between slack and heap memories:
    • Slack memory: local variables and function call.
    • Heap memory: store objects.
  • Difference between data type and data structure:
    • Data type: the most basic classification. (int, string, var).
    • Data structure: collection of data types. (stacks (LIFO(last in first out)), queues, linked lists, binary tree)
  • Difference between padding and margin:
    • padding¬†is the space between the content and the¬†border, whereas¬†margin¬†is the space outside the border.

Time complexity

  • You can get it counting the number of operations
  • Time complexity is defined as a function using Big O notation.
  • n is the size of the input.
  • O is the worst case scenario.
  • The O function is the growth rate in function of the input size n.

O(1) Constant : odd or even / look up table / check if item in array is null / print first element from a list / find on a map.

O(log n) Logarithmic: find in sorted array with binary search.

O(n) Linear: find max in unsorted array / duplicate elements in array with Hash map / find / print all values.

Algorithms running times:

Factorial complexity(no good),

exponential complexity(no good),

polinomial complexity (no good),

linearithmic complexity (middle point),

linear time complexity (good)(for loop in one level array, searching, printing) O(n),

logarithmic complexity (good) O(log n),

constant time complexity (best one)(swap variables). O(1)

Time Conversion

Problem from Hackerranck.

I am practising algorithms and tried this one today.

I first get the two first characters of the string ‘s’, the ones that represent the hour and change them to a number with parseInt and assign them to the variable hour.

If this variable hour is bigger than 12 I rest 12, if it is equals 12, hour would be the string ’00’ and for the rest of the cases I add 12.

Then I add hour to a substring of the argument without the two first characters and delete the two last characters too, the one that should be “AM” or “PM”.

My solution:


Creating a Django app with a Postgres db using Docker Compose.

I was following this great tutorial.

To run the app:

docker-compose up command from the top level directory for your project.

The app should be running at port 8000 on your Docker host.

Now that I have my app set and running I want to start creating my content.

I want to create an app where users can login/logout securely and perform CRUD operations with their logins.  That means I will need a Login model. Each login will have some attributes too: a version, length, hash, type and prefix.

Some webs ask you to change your password many times so you can store in this interface the number of the version where you are currently, and don’t have to remember it. Same thing happens with that extras that some webs ask you, like a specific length, or special characters that the SuperGenPass don’t use. If a web asks me about a special character I put it at the beginning of the generated password. This interface would be a great place where you can store all info without compromising your security.

Creating my model.

To be tidy, I am going to create a new app, dashboard, and let know to Django to use it, in supergenpy/settings.py I add ‘dashboard’ to INSTALLED_APPS.

I create my Login model inside models.py and run my migrations:

docker-compose run web python manage.py migrate --noinput

Now, what I want is to see my Login model! For that I create a superuser.

I need the id of my docker-compose container:

docker-compose ps -q

and I run the following command

docker exec -it container_id python manage.py createsuperuser

When you run this command, the server has to be running.

Then, you can return to the browser and login in the admin dashboard (http://localhost:8000/admin/) with the credentials you have set up in the command line when creating the superuser.