Loading
Abi Travers

My Blog. Full Stack Software Engineer. Ex-Growth Hacker.

Learning Ruby The Hard Way | How I Beat the Un-stoppable Error Messages

Trouble shooting exercise 25 of learning ruby the hard way by zed

I have enrolled at the intense computer programming bootcamp, the Maker Academy, next month. To get a head start I thought I would work through Zed Shaw’s Learn Ruby the Hard Way book.

Exercise 25 seems to be testing my problem solving skills so I thought I would write a post documenting how I worked out then fixed my error messages. This post is aimed at newbies to the programming world as I imagine it is very basic things which I am getting wrong.

First Script – The Start of the Problem 

This was my first attempt at writing exercise 25’s script in my editor:

exercise 25 of Learn ruby the hard way by zed shaw error messages

 

 

 

 

 

 

 

 

 

 

 

 

When I ran this in the terminal I got the following error message:

ex25.rb:2:in `<main>’: undefined local variable or method `ex25′ for main:Object (NameError)

The first thing I did was google the error message.

Solving the Error Message with Google 

I found an answer on Stackoverflow. It told me I was accidentally typing Alt + Space on my Mac, therefore creating non-breaking space. That this is considered by Ruby as part of the variable name as opposed to the whitespace it was intended to be.

It gave two solutions to this problem:

1.  Remapping Alt + Space to space to stop this typo occurring again.

2. Highlighting invisible characters in text editor to immediately realise the typos.

 

The preference seemed to be for solution 2  so I thought I would try this way first. It seemed the most simple and quick to fix. I wanted to rule out the off-the-shelf issue to my broken code.

Highlight invisible characters in my text editor – Part 1

Next I googled how to highlight invisible characters with my text editor, Sublime Text editor 2.

Turns out this is supposed to be a default feature of my text editor. Every time I highlight text I am supposed to be able to see little white dots like the image below (look close they are there in the empty spaces):

how to highlight invisible characters with Sublime Text editor 2

 

 

 

 

 

 

 

 

So I highlighted the text and nothing happened:

textselected in sublime text editor 2 learn ruby the hard way ex25

 

I decided to park the problem of ‘non-breaking’ spaces here for now and try and solve my error message an easier way. I am relatively new to coding so the next solution for finding ‘non-breaking’ spaces, fixing the preferences or default key assignments in Sublime Text editor, did not seem like the easiest way to solve my problem.

Googling the exercise itself

The next thing I did was google ‘Ex25 learn Ruby the hard way’ to see if anyone else was having the same issues as I was. I found a blog with the exercise typed out. I compared our files word for word on my screen.

I could not SEE any difference between our files (indicating the hidden non-breaking spaces may be what is causing my error).

However I needed to rule out the idea there was a typo I couldn’t spot somewhere, so I typed out the whole file again. Figuring this was a quicker way to get to the solution.

My second file worked when I ran it in the terminal. No error message.

Here is the second file:

Script for exercise 25 learn ruby the hard way. no errors

I was intrigued where I went wrong with the first file still, so I compared them line for line.

Comparing the working script to the one with an error message

Compared them on my screen and noticed two differences which I fixed one at a time.

exercise 25 of learn ruby hard way. Errors

1.  When the earlier functions were called in later ones eg.

def ex25.print_first_and_last(sentence)

words = ex25.break_word ..

The ex25.break_word function looked like it had not been recognised in the error script (one on the right) whereas the Ex25. appeared blue in the other script (left).

The most obvious reason I could come up with  was that the ‘e’ wasn’t capitalised. So I changed all Ex25’s in the broken script to a capitalised e.

exercise 25 of learning ruby the hard way, broken script

 

I ran the script but still got the same error message:

ex25.rb:2:in `<main>’: undefined local variable or method `ex25' for main:Object (NameError)
2. The next easy fix I noticed was the file name and the function name in the broken script were the same but in my new script I had named the file EX25b.rb.

Maybe it was this difference in naming which was responsible for the error?

Nope ..

ex25.rb:2:in `<main>’: undefined local variable or method `ex25' for main:Object (NameError)

Had I read the whole of the exercise in Learning Ruby the Hard Way I would have seen this wasn’t the answer:

“The Ex25 module doesn’t have to be in a file named ex25.rb. Try putting it in a new file with a random name then import that file and see how you still have Ex25 available.”

So I decided to go back to the first solution, non-breaking space as I had now exhausted the easy options and couldn’t for the life of me  see any difference between the error or the correct script.

Highlight invisible characters in my text editor- Part 2 

I read how to do this here.

1.  Open the ‘preferences’ within Sublime text editor & select ‘key bindings’.

Preferences, key bindings within Sublime text editor 2

 

 

 

 

 

 

2.   Insert into the left side, the ‘user’ tab the following code:

{

"keys": ["alt+space"],
"command": "insert_snippet",
"args": {"contents": " "}

}


User tab of key binding sublime text editor

code inserted sublime text editor changing key bindings to show up alt + space bar on macs

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3.   Saved then re-opened the original (broken 🙁 ) file.

Sure enough there was the accidental Alt and space bar non-breaking space:

Alt and Space bar error on a mac and sublime editor 2

 

 

HALLELUJAH .. I thought.. I have finally fixed my old script…….

I ran it again and I STILL had the same error message!

Here is where I probably should have thought:

“I got it correct once and managed to work through the exercise and complete it. I will just call it a day and not worry about the file which is returning an error message”.

But I am too curious / stubborn to settle for just allowing the error to hang over my first script.

I ran irb in the terminal.

I ran the file I had got correct again & the file with the error message:

Error message in terminal. Incorrect Ex25 of learning Ruby the hard way

I tried to work out what was the difference between the two files.

Maybe it was the directories they were saved in?

I check and sure enough they were saved in different directories.

So I saved the error file in the same directory as the file that worked…. STILL the error message!!

I could see no other difference so I ….

Copied and pasted the text from the working file to the error file.

Then something terrible happened!

BOTH the files stopped working!!!!

The same error message appeared for the first file and now the working file was returning ‘false’ when I ‘required’ it with irb:

Ex25 script from learn ruby the hard way returning false in the terminal

Possible cause of this problem:

Too many files with the same or similar name in the same directory?

I deleted all but one… And like magic ..

IT WORKED !!!

I ran the working script for Ex25 in the terminal and followed the instructions on how to use irb to call it’s functions.

Here are my results and my explanation of what is happening:

The correct script for Ex25

Script for exercise 25 learn ruby the hard way. no errors

Running the script in command line ‘irb’

Ex25 in the terminal correct

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

I have labeled the points where I had to think a bit about how it was working. Here is my explanation:

1.   Prints ‘wait’  because the function is acting on the ‘words’ array which is not sorted. The sorted array is called ‘sorted_words’.

2.   ‘Words’ array has had the first and last (“all’ and ‘wait’ ) values shifted off  in the print function.

3.  Calling on the ‘sorted_words’ array, not the ‘words’ array which has the missing values. Therefore ‘all’ is still in the array.

4….Seems to be missing from the picture … So I clearly deemed it irrelevant then therefore i will do the same again now..

5.  Using the original ‘sentence’ variable and passing this through the function. It is breaking this sentence variable into an array every time it encounters a space. Then using Ex25.sort_words function to sort this array of words.

6.  It is taking the original variable ‘sentence’, breaking it into an array of words then printing the first and last value of that array.

 

So we have now come to the end of my struggling with exercise 25 of Learn Ruby the Hard way. I hope you have enjoyed and found useful  my higgledy piggldy problem solving which eventually resulted in me beating the error messages given by running my script.  Please do contact me if you are by a slim chance reading this & need any help understanding it.

Enjoy, 🙂

How to Run A Ruby File In Terminal | On A Mac

Bash: “command not found”

Where is my Ruby file and how have i forgotten how to run a .rb file in terminal already?

Why is bash not finding and loading my ruby file

After having taken a few days off programming and learning Ruby to do some freelance marketing work something terrible seems to have happened. I have forgotten the most simple of terminal commands … How to run a Ruby file.

So in getting myself quickly up to scratch again I thought I would write a quick guide for everyone who is getting started (or started again in my case) with learning Ruby and Terminal.

Now, if you are a complete beginner, you may be thinking:

“What is ‘Terminal’ & why are you telling me to use it? I thought I was supposed to be using ‘Command Line’? “

 

Most Ruby scripts do not have graphical user interfaces then they are ran using the command line or command prompt. The command line or prompt as far as I am aware, can only be accessed though the terminal.

How To Find The Terminal On A Mac 

1. Click on the search icon on the top right hand corner of your screen. The Spotlight search bar will then appear.

spotlight search button on a mac - how to use it to find terminal

 

 

 

 

 

2. Type ‘Terminal’ into the search bar.

3. The terminal will pop up.

Alternatively:

1. Click on ‘Finder’ icon in the dock.

finder icon in the mac doc.

 

 

 

 

 

 

 

2. Go to the ‘Applications’ folder on the right hand hand side.

3. Search in this folder for the Terminal.

Creating Your Ruby File 

At this point I am going to assume you have been learning ruby so have written a few ruby documents before. However if not follow the simple steps below:

1. Download or open your text editor. I use  Sublime text editor, which is a good one for beginners like myself.

2. Create a ruby file by typing into your text editor:

puts “Hello Wold”

3. Save this file as ex1.rb – the important part of this is the .rb this must be at the end of any file for your computer to recognise it as a ruby document.

Installing Ruby To Your Mac

On a Mac Ruby should be pre-installed. Check this by typing into the terminal:

ruby -v            (the space after ruby before ‘-‘ is important)

You should get a message similar to:

     ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16]

2.4.1 may not be the same number you will see as the pre-installed ruby does not tend to be the latest edition. I advise updating your ruby and checking you are running the latest version. How to do that can be found in these guides:

This is the guide I used for installing Ruby & Rails.

This is a simple guide for Ruby  – For beginners stop just before the installation of a database as that isn’t necessary for you for now.

Running Your Ruby File 

When you open your terminal you should find some script already there. It will most likely contain information on your computer and username then be followed with your prompt.

The prompt is usually a single character: $ or #.

1st | Make sure you are in the folder that your .rb file is saved in. To do this input:

pwd (print working directory)

to show the folder you are in.

I save all my .rb files in a folder I have named “Ruby work”. This is usually not the folder my command line starts in so I change it to this folder (or directory) using the command:

 cd (change directory)

I type into the command line:           cd Ruby work

2nd | (Optional) Check the your .rb file is in the folder (directory) you have changed to by typing:

ls (list directory)

into your command line. This will give you the full list of all the files in the folder.

3rd | Run your ruby file by typing into your command line:

ruby ex1.rb 

Do not forget to type in ‘ruby’ before your file name. This is because you need to tell the terminal that you are wanting to use the Ruby programming framework to run your file.

If you have used the example .rb file you should see the following script appear in your terminal:

Hello world.

Getting An Error Message?

why can't i run a ruby file in my terminal

 

If you are getting an error message, something along the lines of:

syntax error, unexpected keyword_end, expecting end-of-input

that means there is an issue with your code in your .rb file, not the terminal or the running of the file itself.

How To Stop Terminal Running Your Script

If you have gone wrong somewhere you may need to stop the terminal or go back up to where you started. You can do this simply by closing and re-opening the terminal.

Or you can force a quit “kill” by pressing:

“Ctrl” & “C” at the same time.

 

That is all for me for now. Hope this helped you get started or get back to running ruby files within your terminal. If you have any questions while learning ruby then send me a message.

Thanks for read 🙂

Lessons From A Successful Indonesian Startup

While trying to figure out what makes successful founders tick, a quick search on Google would lead you to articles or interviews citing Silicon Valley titans like Elon Musk or Jeff Bezos. You are unlikely to find much information on outstanding Asian innovators and entrepreneurs.

After having worked in Asia for over a year now I was totally unsatisfied with the lack of knowledge coming out of the region to the West. So I decided to help generate some of this noise by interviewing some startup funders out here.

I have started with Sayed, the Founder of LocalBrand.co.id. Local Brand is a startup which went from a simple idea, to one of the biggest e-commerce platforms in South East Asia in just three years.

Here is his story with some actionable growth secrets sprinkled along the way.

LocalBrand went from 0 to 1000 customers in just three months

The Ideation Stage

Supply or Demand? Who Should Marketplaces Startups Target First?

Sayed: We went for supply first. We had contacts with designers and brands who already had an established customer base. The brands had a real need for our solution, because they could not sell online. As soon as the brands came onboard, they brought all their customers, so the demand side was easy.

localbrand is a successful Indonesian startup

LocalBrand now has 1000s of Indonesian Brands onboard

Should Startups Go For Funding Straight Away?

Sayed: At this point, we made a mistake. If I could do it again, I would raise funds straight away. Instead, I used my own money for the company, and by the time big players such as Lazada jumped in, it was too late to call for investment. We just couldn’t compete with the promotions that they offer. Customers go where it is cheaper, and the brands eventually follow.

“Marketplace startups face issues with brand loyalty. So if you are building one I would advise getting funding early on.”

The Acquisition Stage

How Long After Launch Should Startups Expect To Get Their First 1000 Customers? What Is A Good BenchMark?

Sayed: We received our first 1000 customers within 3 months after our launch. I think 3–4 months is a good timeline to aim for.

Which Channels Worked Best To Aquire Customers At This Stage? What Strategy Did You Use?

Sayed: We were lucky to have successfully executed a few online and offline growth strategies right from the beginning.

Offline, we started Local Fest — an event to showcase Local Brands and allow offline sales.

We used the festivals to learn more about our customers and define them for targeting purposes. I would recommend really taking the time to do this if you have a smaller budget. Identify one customer type and zero in on it. Map out their journey and find out where they hang out online.

Make sure you develop the product based on what you learn from this. In our case, the customers followed fashion bloggers and influencers, so we targeted these people and partnered with them to get them talking about our service.

Localfest Indonesia, the secrete to this asian startup's success

LocalFest Indonesia- The offline event which skyrocketed organic traffic.

Which Online Channel Worked Best? How Did You Make it Work?

Sayed: We employed quite a few successful online tactics. We found the organic channels were by far the most effective.

1. Social Media. We were one of the first companies to use Instagram. Indonesia is now one of the most active countries on social media, but back when LocalBrand had just started, not many people were on there. We correctly identified the early adaptors of Instagram as our target customer, and were able to make the most of this channel.

2. Influencers & Affiliate Marketing. We defined an influencer as anyone with over 1000 followers. We built relationships with local fashion influencers early on, and got them to endorse our products. Now they have millions of followers, and we still maintain the same partnerships. These online celebs have consistently brought a lot of traffic to our site, so identifying our ideal customer as someone who follows them was a good tactic.

3. Content Marketing.Aside from targeted outreach, we made sure to leverage our relationships with influencers to drive LocalBrand’s SEO strategy as well. They wrote articles about us, and we did interviews with them. They would link to us a lot and through this, our page built authority and trust from Google quickly.

4. SEO. At the start, we focused on target long-tail keywords where the competition was low. Local, Indonesia-specific terms which had low competition but high search volume. These keywords were easier to get on the first page of Google for, and so helped to build our page authority as well as traffic.

blogging and social media contributed to this Indonesian/ Asian startups success

Content Marketing Is Still The Most Powerful Tool To Bring Customers To LocalBrand’s Site so bloggers played a massive role.

The Growth Stage

Which Channel Brought The Highest Number of Converting Customers? Would You Recommend it?

Sayed: The mix of offline and online worked the best for us as both of these reinforced each other.

We executed one large and well-organised offline event every year. This brought us a lot of customers for the following reasons:

1. Partnerships. Every event has at least 50 partners, including those from the media. All our partners used their social media to promote us. We made sure we included this in all our agreements.

2. PR. We got media and bloggers to cover the event. We paid for some, but we also received organic coverage by using our existing partnerships with celebrity bloggers and influencers. We told the press and media about it, and many dropped by of their own accord. By Year Two, we managed to achieve an attendance of 40,000 people at Local Fest over 3 days.

Is There A Difference Between The Growth Channels Which Work Here And Those Which Work In Western Countries?

Sayed: The main difference is the timing.

“Trends tend to repeat themselves, so what was big in the US is big in Indonesia 1–2 years later. Staying ahead of this curve gave us a very effective free growth channel — Instagram.”

Indonesians are more likely to buy products from social media than customers in the US. We use these channels as a means to inform ourselves on where the demand lies, much more than US marketplaces or retailers.

How a local brand in Indonesia became a successful startup using social media

Having High Customer Retention Is Often Cited As Key To Building A Successful Startup. How Do You Make Sure You Achieve This?

Sayed: Actually that’s our biggest problem right now.

In e-commerce, Indonesian startups, like those the world over, struggle to compete with the big guys when it comes to scaling.

We have loyal customers who identity with the brands on the platform, and we keep everything we do focused on these customers and what we know about them. But when it comes to retention, it’s hard to keep most people because the big platforms such as Lazada can offer promotions such as free delivery.

Customers will ultimately go where it is cheaper and the brands will follow.

What Is The Biggest Challenge You Faced? Do You Have Advise For Anyone Who May Share This Issue?

Sayed: User retention is our biggest challenge. We have overcome this by first realising that we cannot compete in terms of price with the players with a lot of funding, so we have had to change our tactics. We decided to partner with these large marketplaces instead of compete with them.

Thus, we created LocalBrandAsia, which is a connection for the local brands to all the market places and e-commerce sites. We focused on our strength — a connection with the local brands, instead of our weakness, an inability to provide the cheapest prices.

By making this move, we made Localbrand.id even more focused on our targeted customer, and created a lifestyle brand for them.

Do You Have Any Advise For Someone Looking to Create A Startup In Indonesia?

Sayed: Yes, don’t give up.

“Failure only happens when you stop trying”

It is so rare that anyone will succeed on their first attempt. Make sure you learn from your mistakes and bring those insights with you to your next attempt. So many of the companies I see here give up after their very first failure. See it as a learning opportunity, pick yourself back up and carry on.

coconuts and pineapple, turquoise colours. Bali