Twitter

Github

LinkedIn

Peacebuilding, Education, Tech

Blog

Why I Want to Be a Software Engineer

Why I Want to Be a Software Engineer

Silence on the other end of the phone. “Are you even good with computers? You don’t seem like a techy kind of person.” My friend was understandably incredulous of what I had just told her: I want to be a software engineer. I am an […]

Creative Logic

Creative Logic

It’s a sea of methods in Ruby.

Rock Paper Scissors Lizard Spock!

Rock Paper Scissors Lizard Spock!

Up until now in Launch School, I have only made teeny tiny programs. Their philosophy is that it is more important to drill with the small building blocks until they become second nature rather than complete a huge task without completely understanding every aspect of that task. It seems slower, but in the long run, it helps speed up the process because there are fewer bugs in the program. I lived this firsthand when making the longest program I have written to date, Rock Paper Scissors Lizard Spock.

The lesson walked us through a simple Rock, Paper, Scissors game. The program used a simple logical structure that, while repetitive, was functional. Then, it showed us how to refactor that structure into code that was less redundant by using methods.

The lesson ended, and I was feeling pretty confident. I knew what was coming: writing a program based on the same game, but with a lot more options:

The instructions gave a few hints: try to use a hash, and think about the logic behind the game. For example, Rock has only two opportunities for winning: crushing Scissors or crushing Lizard. The instructions also said to use short-hand letters like “r” or “l” instead of “rock” and “lizard” so that the player could enter their choice much more quickly. They also want the game to be able to be played until either the user or the computer reaches 5 points and becomes the Grand Master. Whew!

I knew how to do each of these basic building blocks, theoretically. I was excited to get started. I began by creating loops and methods that would deal with the point part of the game. I created prompts that would instruct the user to enter their choice, and used the .sample method so that the computer could randomly select its choice. All of the user-side code was ready to go!

But how, oh how, to make the game actually work? I spent a good half hour trying to work out the logic of the game. I did not want to simply repeat the same logical structure of the Rock Paper Scissors game since that would be redundant. I realized that I have very little experience with hashes. It has been a while since the last hash lesson, and since then, I haven’t really found much of a use for them in the “wild.” As I looked at the docs for hashes, I realized that I had no idea what I was doing.

I looked at other students’ codes to see what they had done. I compared at least 5 programs, and everyone’s programs were so different that I saw there was no single golden ticket method to get this program to work. Some of the programs used methods that I didn’t even remotely recognize (probably someone with a programming background). I tried some of the methods that I saw in the other students’ codes, and I could not get any of them to work. (Plagiarism just doesn’t work in coding – if you don’t understand enough and you have to plagiarize, it won’t work for you anyway, and if you already understand enough, you don’t need to plagiarize.)

This is exactly why Launch School’s pedagogy is the way it is – coding is so open-ended that there really is no “right” way to program. There are more efficient ways, more readable ways, perhaps, but there simply is no magical formula for programming. This is why breaking things down is so crucial. They teach students to use the PEDAC method, which I hadn’t done in my haste to get started. My last post was about how important it is to plan – did I learn from my mistakes? Not enough, apparently. At this point, I had spent all afternoon and evening working on this program with nothing to show.

So, I got out my pencil and notebook and created a small diagram to plan what would happen in my program. Then, I went to sleep and let my brain do its synapses thing.

The problem with my plan was that I didn’t know how to do the most important part of the program, which is to compare the user input and the computer choice to the winning combos from a hash. I needed to test out scenarios with this small part of the program before adding any loops or counters.

To the Ruby Docs! From what I remember about my classmates’ codes and from what I found in the documentation, I decided to focus on two methods: .fetch and .include?

I needed a way for the program to compare the user input to the computer choice. If they have the same choice, it is a tie. If the computer choice is part of the winning choices hash, the user wins. If not, the user loses. So I needed a way to return the computer choice if it was a part of the hash, and check that value against the hash to see if it was included. The .include? method seemed fairly straightforward, but the .fetch method was new to me. Time to test it out.

winning_combos = { ‘rock’ => ‘scissors’, ‘paper’ => ‘rock’ }

I started with the mini hash above. The documentation provides this example for the .fetch method:

h = { "a" => 100, "b" => 200 }    # this is the hash
h.fetch("a") # the method is invoked with key "a"
#=> 100 # the method returns this value

Using my mini hash, I created a variable called user_input and set it to ‘rock’. Then I invoked the .fetch method using the argument user_input.

user_input = 'rock'
winning_combos = { 'rock' => 'scissors', 'paper' => 'rock' }
winning_combos.fetch(user_input)
#=> "scissors"

It returns “scissors”. Now to create a way to test if the user won by using a conditional statement. I assigned the variable name user_win to the return value of the .fetch method.

user_input = 'rock'
computer_choice = 'scissors'
winning_combos = { 'rock' => 'scissors', 'paper' => 'rock' }
user_win = winning_combos.fetch(user_input)

if user_win == computer_choice
puts "You win!"
else
puts "You lose."

This scenario prints out “You win” because the key value “rock” contains “scissors”. Rock beats scissors. I tested the code again by changing computer_choice to “paper,” it printed out “You lose.” Paper beats rock. If paper is not in the list of values for “rock,” it means that it is not a winning combo for the user.

I added an elsif statement that prints out “Tie” if the user_input and the computer_choice variables are the same. This doesn’t require accessing the hash. The .fetch method did a nice job of returning the value for the key, but what if I have multiple values for each key?

I searched online and saw that I can use an array for multiple values.

winning_combos = { 'rock' => ['scissors', 'lizard'], 'paper' => 'rock' } 

Adding multiple values to the key causes the conditional statement to not be true anymore, because the computer only inputs one value and the key contains two. This is where the .include? method comes in handy. I invoked the .include? method using the return value of the .fetch method.

user_input = 'rock'
computer_choice = 'lizard'
winning_combos = { 'rock' => ['scissors','lizard'], 'paper' => 'rock' }
user_win = winning_combos.fetch(user_input)

if user_win.include?(computer_choice)
puts "You win."
elsif user_input == computer_choice
puts "Tie."
else
puts "You lose."
end

This prints out “You win” because “lizard” is one of the winning combo options for “rock”.

When I changed the variable computer_choice to “Spock”, the program prints “You lose” because Spock is not one of the value options for “rock”. It worked!

This seems so simple in retrospect, but once I had a plan, I spent an hour or so researching and testing this part of the program before adding anything else. The night before, I had spent 4-5 hours fiddling around and had nothing to show for it. With a plan uploaded into my brain and with the time I dedicated to my mini tests, I was able to complete the entire functional program in less than 2 hours. (I spent three additional hours improving/refactoring the program into methods, but that is a different story.)

It pays to plan, to test small parts of the program, and to research. I even spent a bit of time looking at the code review for my classmates’ programs, to see what the TA’s recommended and implement those changes into my own programs. The whole process was much smoother, more relaxing, and more fun than my previous shots in the dark.

Rubocop Musings

Rubocop Musings

Rubocop helps standardize Ruby code. Rules give you freedom. It’s a paradox, I know. Here are some lessons I learned the hard way.

American Dream

American Dream

Here is the story of a young woman I know who decided to go to the US undocumented. She is a young, very hard-working single mother who lives with her family, has a steady job as a teacher at a private school, and has a university degree. Yet she wants to go on this dangerous journey. What is wrong with this picture?

Reconsidering the Migration Narrative in Honduras

Reconsidering the Migration Narrative in Honduras


Photo Exhibit. Transgressing Borders: Migrant Caravans in Mexico, by Cristobal Sanchez, June 2018 in the Museo de Antropología, San Pedro Sula, Honduras. Photo by Absalón Cálix.

Each young smiling face gave their presentation about how to inspire their fellow Hondurans to stay in Honduras. “We have so many opportunities here,” one woman explained, “and people need to see that all they have to do is think positively, so that they can create their own economic opportunities. Why go somewhere else when we have so many possibilities here?”

The event, a US embassy-sponsored project to help young people investigate and tell stories about the causes and effects of migration in Honduras, pulled together passionate young communicators from different parts of the country to participate in a workshop about storytelling. Through this workshop, the youth developed their own projects that would help them tell stories about migration in Honduras. They presented their projects in an event so full of inspiration and promise.

However, the way the majority of the young people talked about migration gave me a feeling of unease. I wasn’t able to place a finger on the feeling until I heard the coordinator of the event mention how August was the month of “Prevention of Irregular Migration” in Honduras, a phrase that I hear on the radio all the time now. While this wording is used internationally, it seems to oversimplify the realities of Honduran migrants, many of whom could be better labeled as “displaced peoples” or even “refugees.”

Is a lack of positive thinking the reason that so many people are leaving Honduras?

“According to the U.N. refugee agency, UNHCR, gang violence in Honduras, along with El Salvador and Guatemala, is driving hundreds of thousands of people from their homes every year – a situation it has described as a ‘refugee crisis.’”

(Moloney, Reuters).

During that month of August, I heard government-sponsored ads on the radio about how dangerous it is to travel with “coyotes” through Mexico, but no news stories about how dangerous it is to be a small business owner in Honduras due to the “war tax” extorsion phenomenon. I heard inspirational public service announcements on the TV of Hondurans who worked hard and were able to improve their lives without immigrating, but no official statements on how to bring to justice the men dressed in police uniforms who arrested two highschool student leaders from their homes without warrants and executed them.

Miguel Mourra, executive director of the Tegucigalpa Chamber of Commerce and Industry (Cámara de Comercio e Industria de Tegucigalpa – CCIT) estimated that at least seven out of every 10 small businesses in Honduras has closed due to extortion. Other estimates suggest extortion may be responsible for the loss of as many as 130,000 jobs in recent years.
“We cannot ignore that there are other problems that push Hondurans to close their businesses,” Mourra told El Heraldo. “However, the issue of extortion and insecurity has become the main cause.”

(LaSusa, Insight Crime)

The idea of being able to prevent migration by either inspiring Hondurans to stay in their own communities and create their own work, or by scaring Hondurans with stories of the dangers of migration, furthers a narrative that is simply incorrect: people are leaving because they choose to leave.

Many leave because they must. Apart from the ever-present threat of gang violence, the US-backed election process at the end of 2017 increased the amount of human rights violations in the country, and human rights activists, currently including local high school students, are in even more danger of being killed by government or paramilitary forces.

Positive thinking is not enough to solve the migration situation in Honduras.

Lack of Data Contributes to Increased Vulnerability

A study by the National Autonomous University of Honduras about migration in Honduras concluded that there are simply not enough studies done in the area of migration in Honduras to be able to create working laws that help migrants, both internally and internationally. This lack of data creates difficulties for the government and NGOs to be able to help migrants, especially Hondurans deported from the US.

Photo: Participants in the Returned Migrant Program through CASM (Commission for Mennonite Social Action). Photo by Absalón Cálix.

One NGO in San Pedro Sula has difficulties finding deported Honduran young people to give them a chance to participate in their job training program, a way to help young people reintegrate into Honduran society and thus avoid the siren call to try to migrate again.

The data needed to find the returned migrants is simply not available.

A few months ago, my pastor, evangelizing in a small town outside of San Pedro Sula, approached a man who looked distraught. She told him that God was with him, and to not be afraid. His eyes welled with tears as he responded: “How did you know? I was deported last week from the US, and I have no idea how to find out where my daughter is. She was separated from me in the detainment center.”

The plight of this man is not uncommon: there are few ways for lawyers helping the separated children to contact parents, or for deported Hondurans to contact their children separated from them in the process of deportation (“Inside the Desperate Search for 343 Parents Deported Without Their Kids”). Lawyers in search of missing parents worry about the possibility of these children becoming essentially orphaned.

Academics and civil society agree that the lack of definitive facts related to migration in Honduras is directly tied to the government’s narrative about itself as having reduced migration, as well as reduced the crime rate. This lack of data makes the already vulnerable population of returned migrants even more vulnerable, because it is even more difficult to care for their well-being in the midst of a precarious return if the government and NGOs do not know who or where they are.

Possibilities for Change

After hearing the shining messages of the young people at the storytelling event in August, I asked a participant in one of the programs what he thought about the conclusions of the speakers. “I liked the part about making our own opportunities,” he conceded, “but I didn’t really hear anything about why people actually leave.” I asked him why people leave, and he only had one word: “Violence.”

Violence displaces people from their homes, families, jobs, and communities more than any other factor in Honduras at this moment. According to a study by ERIC-SJ in Honduras, surveys show that over 30% of people who migrate from the country chose to do so directly related to causes of violence.


SpeakOut Community Theater Event. Explored situations of violence that lead to migration. Photo by Megan Turley.

The possibilities for long-term change must include solutions for violence, corruption, and impunity in Honduras. North American governments must hold the Honduran government to higher standards in its election processes and in protecting human rights.

A positive mindset is not enough to change a government-sanctioned culture of violence.

Against all odds, Hondurans are so resilient in the face of everyday violence. How much would they be able to shine if they could use this resilience in economic and social change instead of focusing on everyday survival?


Peacebuilding Thoughts

Peacebuilding Thoughts

“I just want to be able to provide for myself and my family, and be able to give back to my community.” The young woman looked at the group of young people surrounding her. They nodded, understanding. It is hard to provide for your family […]

Life is a journey

Life is a journey

Life is about taking every step as being as important as the final destination and being present in our missteps, knowing that failure is just another step on the road to growth…