The document discusses various Ruby programming concepts including conditionals, iterators, blocks, hashes and arrays. It provides examples of how to use if/else statements, loops, iterators like each and times to iterate over collections. It also demonstrates creating and manipulating hashes and arrays through various operations and accessing values.
3. Conditionals: ifif age > 17 puts “can vote”endif age > 17 puts “can vote”else puts “attends school”endStatement Modifiers:y = 7 if x == 4Other Syntax:if x == 4 then y = 7 end
4. TruthTruth: Everything is true except for:false nilTherefore0 is true“” is trueChecking for false:if !(name == “superman”) …if not (name == “superman”) …
5. Unless“unless” provides us with another way of checking if a condition is false:unless superpower == nil status = “superhero”end
9. Iterators: Conditional Looping“while” allows us to loop through code while a set condition is truex = 1while x < 10 puts x.to_s + “ iteration”x += 1end
19. Blocksdef dos_veces yield yieldenddos_veces { puts "Hola” }HolaHolaYield executes the blockThis is a Block!{
20. Yield with Parametersdef bandsyield(“abba”, “who”)endbands do |x,y| puts x,y endabbawhoYield sends its parameters as arguments to the blockyield(“abba”, ”who”) sends “abba” and “who” to |x, y|x is set to “abba”y is set to “who”
24. Stubbing TimeHow can we do this to help with Time.now?t = Time.nowt + 10 #adds 10 secondsStubbing Time:Time.stub!(:now).and_return{fake_time += 10}
25. How to Use Stubs in a Test:describe “Time stub” doit “should increment mock_time by 10 seconds” do fake_time = 0Time.stub!(:now).and_return { fake_time += 10 }Time.now.should == 10Time.now.should == 20endend
Conditionals are key to being able to make decisions in a programleft looks like every other languageparentheses are optional in ruby make sure to do ==, = is an assignment, == is a conditional testExplain putsright is a little different…people in ruby don’t like to type…english readableA statement modifier lets you move control structures at the end of an expression.
Unlike some languages with the 0 and empty string! Binds more tightly than than the “not” keyword so you do need parentheses for example 1, but don’t need parentheses for example 2
Unless can be awkward, especially with else. Usually you’ll want to use if for conditionals with else clauses.Occasionally unless is more readable:unless something is nil
Alot of the time you will be using an array when you iterate over somethingAn array is just a list of items.Every spot in the list acts like a variable and you can make each spot point to a different objectW means wordsArray is a class, needs to start with capital letter
IRBif you go off the array it will be nil
join is cool because it makes a string for youshovel operatormultidimensional array
Does anyone know what a hash is? associative array collection of key-value pairskeys can be numbers or strings Difference from an Array
merge takes the value from the second hashmerge! changes h1
you would think that delete should need a bang to change the hash, but delete doesn’t exist with a bangdelete returns the value
5 is an object that is an instance of the integer classtimes is a method of the 5 objecttimes is a method on an object that is an instance of integer
it does the block of code three timesit is very rare that you will see a while loop in ruby...you can do the loops we did earlier, but rubyists will mock you.
What is a block? It is the ability to take a block of code, wrap it up in an object and pass it to a method. Then you can run the block of code within the method any time you want…sometimes twice! The result is kind of like sending a method to a method, except that a block isn’t bound to an object like a method is – it is an object. So what? Why use blocks?elegant syntax for iteratorsBecause there are some things that only blocks can do, like being passed to a method and being returned by a method.
two ways to declare a blockuse curly brackets for single lines of codeuse do end for multi lines of code