Skip to main content

yaml file

1. type require 'yaml' at the top of your code.

2. Next  type   MESSAGES = YAML.load_file('calculator.yml')

3. For example your YAML file is like this,

welcome: "Welcome to Calculator! Enter your name:" valid_name: "Make sure to end a valid name."

Then, all your MESSAGES is a hash file like this.

{ "welcome" => "Welcome to Calculator! Enter your name:" ,
"valid_name" => "Make sure to end a valid name." }

You can extract the message with code like this:

MESSAGES["welcome"]


3. If you want to internationalize your program. Your yaml file will have another layer of language keys like en or es

en: welcome: "Welcome to Calculator! Enter your name:" valid_name: "Make sure to enter a valid name." es: welcome: "Bienvenido a la calculadora! Entre su nombre:" valid_name: "Asegúrese de entrar un nombre válido."

Then, your new hash will look like this.

{"en"=>{"welcome"=>"Welcome to Calculator! Enter your name:",
"valid_name"=>"Make sure to enter a valid name."},
"es"=>{"welcome"=>"Bienvenido a la calculadora! Entre su nombre:",
"valid_name"=>"Asegúrese de entrar un nombre válido."}}

You can only extract the message with code like this:

MESSAGES["en"]["welcome"]


Comments

Popular posts from this blog

Problem Solving - Refactored

I am going to outline how I approach problem solving. The relative importance and the amount of effort/time required for each is stated as a percentage beside each topic. I borrowed some idea from George Polya's How to Solve It Thoroughly Understand the Problem (30%) When encountering hard problem , you need to deeply understand the problem at hand. Take a paper and list down all known facts and data and what the question is trying to find. Sketch out the problem if applicable. Visualize the problem in your head. A lot of times, we only have to understand the problem well, then the solution will obvious. Have a Plan (20%) You need to have an outline of how you are going to tackle the problem. You need to have a logical pathway that will ultimate produce outcome (nothing to do with coding syntax yet). Without a plan, you are just randomly poking around and got lucky. No hard problem ever gets solved without a plan. Plan using pseudo-code, pen & paper or flowchart. Use wh...

My Burnout Experience

I want to share with you my experience of burning out. After registering with Launch School, I am extremely excited about my programming journey. I studied for 10 to 12 hours a day, memorizing fact, trying out practice problems, understanding programming concepts. It was fun and exciting and I love seeing myself growing from nothing in programming to something more. After about 3 months, thing starts to change. I started noticing myself paying less attention to details. I find myself skimming through the course material. I skip "Further Exploration" in the practice problem. I am more interested to study just to pass the assessment rather than truly mastering the concept. It was a gradual burning out process but I continue to study for 10 to 12 hours a day through sheer grit. It felt like doing house chore or working a day job that you don't like. One particular morning I woke up, and I remember this deep feeling of dread because I can anticipate that the next 10 to 1...

Static vs Relative vs Absolute vs Fixed

Static: the default position. Everything starts from top to bottom nicely according the document flow. Relative: still within normal flow of document. But accept offset position, relative to its original document flow position Absolute: out of normal flow. It doesn't interact with elements around it anymore. The reference point will be parent element that has relative positioning, if not, reference body element. Accepts offset position Fixed: out of normal flow. The element sticks to a corner of the page even if user scrolls. Reference the windows view.