Skip to content

Instantly share code, notes, and snippets.

@Cdale3
Last active December 11, 2016 00:56
Show Gist options
  • Save Cdale3/56f22108fc91880c4873ba5faeb8ae3b to your computer and use it in GitHub Desktop.
Save Cdale3/56f22108fc91880c4873ba5faeb8ae3b to your computer and use it in GitHub Desktop.
Notes & definitions/terms
% 6543.21.modulo(137) #=> 104.21 division, first number by the second number
6543.21.modulo(137.24) #=> 92.9299999999996
==           1 + 1 == 2 => true     1 + 1 == 3 => false which means "are these equal?"
!= (which means "are these different?")
<= less than or equal
=>
abs     (-34.56).abs   #=> 34.56       absolute value
-34.56.abs #=> 34.56
array   []
names = ['Ada', 'Belle', 'Chris'] this is an example of an array and how to pull different values, which are variables
puts names
Ada
Belle
Chris
puts names[0] => Ada
puts names[1] => Belle
puts names[2] => Chris
puts names[3] => there is a return there, it is nothing. there is no value for names[3]
meals = ["Breakfast", "Lunch", "Dinner"] this is a good example of how to use an array
=> ["Breakfast", "Lunch", "Dinner"] and how to grab stuff from it
meals << "Dessert"
=> ["Breakfast", "Lunch", "Dinner", "Dessert"]
meals[2]
=> "Dinner"
meals.last
=> "Dessert"
strings will always contain " "     strings are a collection of data "" " " -- acceptable
you can select the character(s) by using "whatever in here"[] 0 is the starting place character, you can use -1 as the last character and work backwards counting increasingly connsecutively - for as many spots as you go left, alternatively you can find the length of the entire string .length and use that number for the last place character. getting the same result.
as long as it is a string the number of chacters will represent what is there.. if you more than one word you just count the number of spaces.
string methods - " ".(method here)
start looking up things that will help you be faster -- you think it is silly now -- it not only makes you faster but helps you learn commands and helps bridge some gaps.
ceil 5.55.ceil => 6
Returns the smallest Integer greater than or equal to float.
1.2.ceil #=> 2
2.0.ceil #=> 2
(-1.2).ceil #=> -1
Concat --   [ "a", "b" ].concat( ["c", "d"] ) #=> [ "a", "b", "c", "d" ] . Appends the elements of other_ary to self
delete .   for a string you will use "hello, world".delete "l" => "heo, word"
downcase "HELLO, WORLD".downcase => "hello, world" *you can put two methods that work together*
            "HELLO, WORLD".downcase.delete "l" => "heo, word" *two methods in work*
empty?       " ".empty? => false   "".empty => true [].empty? => true empty space has a value and a place in coding -- measures if there is anything in the array.
even? 3.even? => false
floor → integer
Returns the largest integer less than or equal to float.
1.2.floor #=> 1
2.0.floor #=> 2
(-1.2).floor #=> -2
(-2.0).floor #=> -2
gsub *I started to have issues with typing in the code, thought irb was messing up. So I opened my text editor up to type in the code to figure out what was up. I just typed what I had in the command line, into the text editor. My error was read back to me, in my case I had forgotten to close with a ) at the end of the method. I also was looking at the code:
> "HELLO, WORLD".gsub(/[aeiou]/, '*')
=> "HELLO, WORLD" And wondering why it wasn't replacing what I wanted it to replace. so:
2.3.0 :004 > "HELLO, WORLD".gsub(/[aeiouAEIOU ]/, '*') using '*' is interpolation
=> "H*LL*,*W*RLD" I got what I wanted, I wasn't even orignally sure that adding AEIOU was going to work; I used both so I could visualize what I was doing. a A are two different thing.
"hello".gsub(/([aeiou])/, '<1>') what you put here in the ' ' will go into the targeted replacement in the [ ]
=> "h<1>ll<1>" one of the biggest problems wrapping my head around was using keys and elemnts.        
"12345hell, world".gsub(/([1ole3])/, '<1>') typing this out has helped me see what i wasn't seeing before
=> "<1>2<1>45h<1><1><1>, w<1>r<1>d" I was thinking everything had to be named exactly the way it was
using interpolation, which I understood what it meant, I just didn't see enough of it in context to cement down maybe? So I tried to trick the system a little with that code above. using the string in that arrangement, I called into question the substitution of 1ole3, and got back what was entered in the target value.
"hello, world".gsub(/[eo]/, 'e' => 3, 'o' => '*')
=> "h3ll*, w*rld" even though eo are together in the target value it still looks at them seperately. That was getting me for a long time. I thought eo = x, I didn't see it as e and o being able to be two different things.. or more
"Terribly complex".gsub "complex", "simple" # => "Terribly simple" You can do whole words for other whole words.
"this is a sentence".gsub("e"){ puts "Found an E!"}
Found an E!
Found an E!
Found an E!
=> "this is a sntnc"
Hashes - are a collection of data where each element of data is addressed bya name.
include?       "hello".include? "lo" => true "hello".include? "ol" #=> false "hello".include? ?h #=> true
.join Returns a string created by converting each element of the array to a string, separated by the given separator. If the separator is nil, it uses current $,. If both the separator and $, are nil, it uses empty string. [].join => ""
[ "a", "b", "c" ].join("-") #=> "a-b-c"
index .           "hello".index('ll')=> 2 searches the string for the characters defined in the interpolation. The index will grab the first character in the interpolation and give you its value in return.
"hello, world".index(?r) => 9 "hello".index(/[aeiou]/, -3) => 4
integer -- Returns the greatest common divisor (always positive).
2.gcd(2) => 2
key/value pairs - a hash is an unordered collection where the data is organized into "key/value pairs".
length       .length returns the value of a string
methods are what we use to help dictate to a given line. use .method to pull up what you can do to each thing. Practice doing this.
reverse       "stressed".reverse => "desserts"
round 1.234567.round(3) => 1.235 you can indicate which integer you want to round, using the index.
Simplified Hash Syntax - We’ll very commonly use symbols as the keys of a hash. When all the keys are symbols, then there is a shorthand syntax which can be used:
produce = {apples: 3, oranges: 1, carrots: 12} Notice that the keys end with a colon rather than
puts "There are #{produce[:oranges]} oranges in the fridge." beginning with one, even though these are symbols
split           " now's the time".split => ["now's", "the", "time"]   splits it up into an array .
" now's the time".split(/ /)   #=> ["", "now's", "", "the", "time"] splits it up with the whitespace in consideration
"1, 2.34,56, 7".split(//) => ["1", ",", " ", "2", ".", "3", "4", ",", "5", "6", ",", " ", "7"]
"mellow yellow".split("ello") #=> ["m", "w y", "w"]
next "abcd".succ => "abce" uses the last character in the given string, gives it the next character in that given set
"abcd".next => "abce"
odd? 4.odd? => false
start_with?     "hello".start_with?("hell") => true . "hello".start_with?("heaven", "hell")     #=> true . if one of the two options is there it will return true.
swapcase     letters = 'aAbBcCdDeE'    puts letters.swapcase .     => AaBbCcDdEe
substrings Often with strings we want to pull out just a part of the whole – this is called a substring. This is good to know. Sometimes you can't keep up with, or forget to write down all the notes you need to take. you can use to pull differennt parts of the string:
greeting = "Hello Everyone!"
greeting[0..4]
greeting[6..14]
to_f 4.to_f => 4.0 returns showing decimal.
to_i 1 + "2".to_i => 3
to_s .     5.to_s => "5"
tr tr(from_str, to_str) => new_str so this is weird, ell ip3 each should be replaced by each of .3.0 :033 > "hello".tr('ell', 'ip') their corresponding things e to i l to p l to 3
=> "hippo" instead, something happens with the index finder(?)
2.3.0 :034 > "hello".tr('ell', 'ip3') that doesn't recognize when i have two letters next
=> "hi33o" to eachother. however, once I changed a few things
2.3.0 :035 > "hello".tr('ell', 'ipz') it did work like that. *good practice to type out
=> "hizzo" different arguments into your code to see different results.
2.3.0 :036 > "hello".tr('heo', 'ipz')
=> "ipllz"
upcase "hEllO".upcase #=> "HELLO"
upto "a8".upto("b6") {|s| print s, ' ' }
for s in "a8".."b6"
print s, ' '
end
                a8 a9 b0 b1 b2 b3 b4 b5 b6 a8 a9 b0 b1 b2 b3 b4 b5 b6 you get two sets of the array, since you called for s after the first initial argument
== str == obj → true or false
Variables - You will name your own variables. One of the biggest things that was tripping me up, was when variables would be called over again. in the code I copied it shows the code running through the program. Now it is doing what it should, but I never understood that 'composer' in this example could immediately be renamed/revalued for the next block, I thought it was still what it originally was.
in your text editor
composer = 'mozart'
puts composer + ' was "da bomb", in his day'
composer = 'beehthoven'
puts 'But I prefer ' + composer + ', personally'
=> mozart was "da bomb", in his day these two lines are the retrun when you run the file in terminal (.rb)
But I prefer beehthoven, personally
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment