Blog

Learning about Ruby’s Super (OOP)

So often we are overly hasty to move on to the next section; we rarely stop to take a breather. So take a moment to let your mind go blank. Remember, deep breaths.

In this article, we are going to analyze super, a topic covered in the OOP section of Launch School.

In one sentence, super is a built-in function in Ruby that allows us to invoke methods that would otherwise be replaced by method overriding during single inheritance.

What does that all mean?

We will start small.

Take a look at the example below.

class Programmer

def write_code
    "This code is generic and quite possibly mediocre"
  end
end

class Rubyist < Programmer

  def write_code
    "This code follows the principles of OO Design"
  end
end

The above example showcases method overriding. As we can see, there are two instance methods called #write_code.

If we were to instantiate an object from the Rubyist class and invoke the #write_code method, we would see the return value of the Rubyist instance method only. Example below:

class Programmer

  def write_code
    "This code is generic and quite possibly mediocre."
  end
end

class Rubyist < Programmer
 def write_code
    "This code follows the principles of OO Design."
  end
end

steve = Rubyist.new

puts steve.write_code 
# => "This code follows the principles of OO Design."

Unlike, some other languages, Ruby is single inheritance, which means each subclass can only inherit from one superclass using the < character.

In the example above, we see that invoking the #write_code instance method on an instance of the Rubyist class, stored in the local variable steve, only calls the instance method in the Rubyist class, despite inheritance with the above class Programmer.

If we wanted to run the instance method in the superclass Programmer, we would need to use super.

class Programmer

  def write_code
    "This code is generic and quite possibly mediocre"
  end
end

class Rubyist < Programmer

  def write_code
    super + " " + "This code follows the principles of OO Design"
  end
end

steve = Rubyist.new

puts steve.write_code 
# => "This code is generic and quite possibly mediocre. This code follows the principles of OO Design."

(As we can see, now when we call the #write_code method, we invoke both classes’ instance methods.)

Super (with arguments)

Super is often found inside the #initialize instance method, which gets called when we instantiate a new object using the built-in class method #new.

Take a look at this example:

class Programmer
  def initialize(name, experience)
    @name = name
    @experience = experience
  end

  def write_code
    "This code is generic and quite possibly mediocre"
  end
end

class Rubyist < Programmer
  def initialize(name, experience)
    super
  end

  def write_code
    super + " " + "This code follows the principles of OO Design"
  end
end

steve = Rubyist.new("Steve", "Newcomer")

p steve # => #<Rubyist:0x007fe93312ea48 @name="Steve", @experience="Newcomer">

(We cannot forget that embedded in the #p method is an #inspect method, which displays the object and its instance variables.)

From the code above, we see that simply putting super inside the #initialize method automatically passed the parameters “name” and “experience” (quotations for emphasis) up to the #initialize method of the Programmer class.

Generally, we want to think of inheritance as something that occurs at the class level — and that a subclass inherits behavior from its superclass, not state.

This is where super comes in.

— — — —

If we include a parenthesis after super — like such, super() — we will depart from super’s natural behavior of forwarding all the arguments on to the superclass.

# code omitted

class Rubyist < Programmer
  def initialize(name, experience)
    super
  end

# code omitted
end

steve = Rubyist.new("Steve", "Newcomer")

# => 15.rb:2:in `initialize': wrong number of arguments (given 0, expected 2) (ArgumentError)

In this example, the code raises an error. Ruby tells us that the #initialize method in the Programmer class has not been passed two arguments, like it requires.

— — — —

In the below example, we see super with a parenthesis again.

class Programmer
  def initialize(name, experience)
    @name = name
    @experience = experience
  end
end

class Rubyist < Programmer
  
  def initialize(name, experience, best_gem)
    super(name, experience)
    @best_gem = best_gem
  end
end

steve = Rubyist.new("Steve", "Newcomer", "Pry")

Above, we are passing two arguments to super and setting the third argument to the @best_gem instance variable.

Teaching English In Order to Learn Another Language Abroad. Is It a Good Idea?

Unlike countless other articles, this article is not an “it depends” article.

I’ve taught English in Mexico and China and I consider both these two jobs to have been good “learning experiences,” but bad jobs overall. In this article, I want to explain why that is.

Living abroad provides total immersion, right?

Total immersion in a foreign language is so often touted as the most surefire way to learn a language.

I cannot disagree there, total immersion has its merits.

But teaching English is one job in particular that deprives you of total immersion (or rather, immersion in itself).

Let me use my two previous English teaching jobs to give you a better idea.

We will go with the most recent instance first: China.

After a dozen Skype interviews, I landed a “good” job teaching English in at a public school in Nanjing, China.

The company organizing everything seemed reputable and I had negotiated a salary of 10,000 Reminbi per month. It’s no Wolf of Wall Street, but it allowed me to live well enough.

Now here comes the catch. I went into this job with many unanswered questions.

I was never told which school I was to work at (not for lack of asking, they said a school would choose me when I got there), and I was told I would teach 5th grade, which ended up not being 100 percent true (I taught 5th grade once a week, and the rest of my teaching was 1st and 2nd grade.)

So shortly after landing in China, they took me to my school, where I was to live and work.

I was placed in a tiny dorm in the back of the school, requiring me to walk five minutes just to get out of the school grounds and another couple minutes by moped to get to the metro.

For my goal — to learn Mandarin Chinese — this wasn’t immersion at all. I was at a school where they expected me to only speak English, and it was rather hard to get out of this controlled environment.

Now Mexico City, Mexico.

In Mexico City, the capital of Mexico, I taught English at a Wall Street English.

I worked from 7:00 to 11:00 a.m. and 5:00 to 9:00 p.m. each day.

Yes, this is a double shift, which left me depleted.

It also left me very little time to meet people and practice my Spanish. Plus, Wall Street English wanted me to come in on Saturday every other weekend to work 4 hours.

This schedule was made worse by the fact that I rarely got off right at 11:00 a.m. or 9:00 p.m.

Often, I had to stay later because a student arrived late or I had to simply wait for students to finish their lesson on the computer.

So what’s my takaway?

My opinion is that teaching English doesn’t grant language immersion and is often exploitative.

The English teachers I met in China were often career-less, which is a term I just came up with (though I’m sure I’m not the first to say it).

What I mean by career-less is that if they were to return to their country of origin they wouldn’t have a career to fall back on.

In my opinion, career-less people are often careless and unmotivated. This lot does not make the best friends, either

Back To Learning Mandarin Chinese!

In China, I would try to speak Chinese with the other teachers (that is, Chinese people) at my school, and I really had to search out the ones who would speak to me in Chinese because most of them would want to speak English with me.

It was really an uphill battle, and I urge everyone to take caution when they commit to a long-term contract in another country.

In the case of China, anything that that is not in the contract, they won’t honor. That was my experience at least.

What should I do?

I would get a decent job in your home country, save money, and enroll in a language learning program in the target country.

Once you are there, if you still want to teach English,  you can find a good teaching job there and avoid the bad ones, which might look good on paper.

You can probably set up a job before you leave, therefore, circumventing the dangers of entering into an agreement blindly like I did.

 

 

5 Ways Foreign Languages Made Me a Better Writer

 

As a Journalism major, I spent a lot of time honing my craft. Years actually. (Truth is I’ve never stopped.)

Like many writers, I tried to refashion myself after the greats who came before me.

Of the many traits I noticed in their writing, there was one thing in particular that began to dominate my thinking: Multilingualism.

Whether it’s George Orwell, Aldous Huxley or Alex de Tocqueville, they all spoke and read multiple languages and had spent years abroad.

I knew, if I were to take my writing to the next level, I would need to do the same.

Here are the 5 ways Learning a Foreign Language Has Made Me a Better Writer:

 

1. Perspective:

Have you ever heard a college professor mention being in someone else’s shoes?

Learning a foreign language is this — and then some.

Imagine two writers, the first an armchair anthropologist reading books in his office; the second a traveler just as well-read and well-versed living in a foreign land communicating in another tongue.

Who do you think has a deeper understanding of our inner workings?

 

2. Noticing Nuance:

Learning another language, especially a European language, can serve as a lens to look at your own native language.

After learning Spanish, I realized English doesn’t always turn nouns into adjectives, for example, “education system,” and not “educational system.”

In contrast, in Spanish, they would modify the noun, making it an adjective: el sistema educativo (en Mexico, el sistema escolar).

 

3. Word of mouth:

There are countless ways to say it, but getting the scoop, the low-down, the goods, the dirt from someone else by word of mouth is invaluable.

Now take a step back and think about getting that in another language.

It worries me how few foreign correspondents speak a foreign language; it most certainly is keeping them from the scoop.

 

4. Reading in another language:

History has so much to tell us.

Reading in another language, allows us to tap into a rich vein of knowledge that would otherwise go unseen, and therefore, excluded.

Tuning into another language’s literature, history and folklore is like having two refrigerators, instead of one.

It’s literally food for thought.

 

5. Mental Effort:

Learning a foreign language exercises your brain.

As humans, language is one of our unique abilities that separate us from the rest of the Animal Kingdom.

Learning another tongue is a mental exercise that sharpens the mind, providing confidence at the same time.

Exercising your brain not only prevents mental degradation, it also improves your overall mental wellbeing.

If you would like to learn a foreign language, please download my free eBook, written to guide you through the process of learning a foreign language.

The Fischer Method for Language Learning

 

My Return to Spanish

In the YouTube video below, I talk in Spanish with my Spanish teacher, Jose.

This year I plan to learn French and continue learning Spanish. I will be posting more video of myself speaking Spanish, in hopes that each subsequent video will be an improvement on the last.

In the video we talk about the need to have a certain foundation in the target language before you start speaking.

When I lived in Mexico City, Mexico, I notice this firsthand. I would make mistakes all the time, and it was extremely frustrating and demoralizing.

It actually led to stop studying Spanish for a while.

Well, now I am back, and I plan to speak Spanish better than ever before.

It may take a moment for the video to load.

A Tour of The School That I Worked at in China

Below is a link to a video of the Chinese Primary School where I worked and lived. The school is in the suburb of Xianlin in Nanjing, China. The video is posted on my YouTube channel.

It may take a second for the video to load.

Be sure to like, comment and subscribe.

Please feel free to ask any questions you have about living in China. I am more than happy to share my experience.