Intro to 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?
Base Challenges
Use irb
in your terminal for these challenges. Feel free to copy your solutions into a file in Sublime so you can reference them later.
Data Types
Store your
first_name
in a variable and yourlast_name
in another variable.Concatenate your
first_name
andlast_name
variables, and store the output in a new variable calledfull_name
.Use
.split
to turn yourfull_name
variable into an array.
Loops
Print (
puts
) "Ruby is awesome!" 50 times. Implement this 3 different ways, using: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
, yourcount
hash would look like this:{a: 1, p: 2, l: 1, e: 1}
.
- For the string
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 ofgets
to remove trailing\n
.
- Hint: Use
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.
- Use
Iterators: Each
Define an array of 4 phrases:
"Hello, world"
,"OMG"
,"Ruby"
, and"Pair Programming"
. Use.each
to iterate over the array andputs
each phrase.Iterate over your array of phrases again, but this time, only
puts
the phrase if its length 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
Write a program that maps an array of numbers to double each number.
Write a program that maps an array of words to the reverse of each word. (Hint: Look up
.reverse
)
Let's have an HTTParty!
Install the httparty gem
$ gem install httparty
.Now require it in a new ruby script file, and use it to call an album search on the word "White" to the spotify API.
Can you require both
httparty
andawesome_print
to have the output look nice? (remember just require awesome_print and then useap
instead ofp
)In the same file, can you write a loop that returns an array of the album names from your search?
Stretch Challenges
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
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