Code Monkey home page Code Monkey logo

intro-ruby's Introduction

Intro Ruby

Objectives
Identify data types, operators, and control flow patterns in JavaScript and utilize them in Ruby
Apply Ruby control flow to create command line applications

Framing for the Week

As we learn Ruby, it's important to revisit how we learned our first language and use that to organize the study of our new language. Learning our second programming language is a process of translating concepts, expressions, and patterns from our familiar language into our new language. Learning our first language involved more identification and comprehension of the knowledge required to implement our first programs. We should begin by organizing this knowledge to build a better understanding as we transition to Ruby.

Types of Knowledge

  • Declarative Knowledge > your "what is" knowledge, i.e. describing what something is
  • Imperative knowledge > your "how to" knowledge, i.e. describing how to do something

Parts of A Language

  • Primitives
  • Combinations
  • Abstractions

Discussion Questions

  • What is JavaScript? What does it look like?
  • What are some of the primitives in JavaScript?
    • Think data types, variable declarations, conditionals, functions, etc.
  • How did we use JavaScript to build things? How did we build up from the fundamentals of the language?
  • What could possibly be different in another language? How could we change the syntax, but keep the semantics?

Challenges

Options for running these challenges:

  1. Use irb in your terminal.

  2. Create a .rb file. Put your code there and run with ruby YOUR_FILE_NAME.rb.

Data Types

  1. Store your first_name in a variable and your last_name in another variable.

  2. Concatenate your first_name and last_name variables, and store the output in a new variable called full_name.

  3. Use .split to turn your full_name variable into an array.

Loops

  1. Print (puts) "Ruby is awesome!" 50 times. Implement this 3 different ways, using:
  1. Save any string to a variable, then create an empty hash called count (count = {}). Loop through the string, and count occurrences of each letter. Store the counts in your hash like this example:
  • For the string apple, your count hash would look like this: {a: 1, p: 2, l: 1, e: 1}.
  1. Write a program that gets user input from the terminal and puts it until the input is the word "quit" or "q".
  • Hint: Use gets.chomp instead of gets to remove trailing \n.
  1. Write a program that prints the "bottles of beer on the wall" song:
5 bottles of beer on the wall,
5 bottles of beer!
Take one down and pass it around,
4 bottles of beer on the wall!
...
  • Use gets.chomp to ask the user how many verses they want to hear.
  • Make sure your song prints "1 bottle of beer".
  • When the song gets to "0 bottles of beer on the wall", it should print "No more bottles of beer on the wall" instead.

Stretch Challenges

Iterators: Each

  1. Define an array of 4 phrases: "Hello, world", "OMG", "Ruby", and "Pair Programming". Use .each to iterate over the array and puts each phrase.

  2. Iterate over your array of phrases again, but this time, only puts the phrase if its length is 5 letters or longer. Otherwise, print a message that the phrase is too short, and include the phrase's index in the message (Hint: Look up .each_with_index).

Iterators: Map

  1. Write a program that maps an array of numbers to double each number.

  2. Write a program that maps an array of words to the reverse of each word. (Hint: Look up .reverse)

More Stretch Challenges

  1. Create a simple temperature convertor. It should function like the example below:
Type '1' to convert from Celsius to Fahrenheit or '2' to convert from Fahrenheit to Celsius
1
Enter Celsius temperature:
24
24 degrees Celsius is equal to 75.2 degrees Fahrenheit
  1. Create a simple calculator that first asks the user what method they would like to use (addition, subtraction, multiplication, or division), then asks the user for two numbers, printing the result of the method with the two numbers. Here is a sample prompt:
What calculation would you like to do? (add, sub, mult, div)
add
What is the first number?
3
What is the second number?
6
The result is 9

Docs & Resources

intro-ruby's People

Contributors

ben-manning avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.