rubyistt's profile picture. --

Rubyist

@rubyistt

--

An often overlooked operator: "the modulus operator", which is used to output a division's left-over, e.g.: ``` puts 8%2 # prints 0 puts 8%3 # prints 2 ``` #Ruby


To set a default value for a function parameter in Ruby, we can put it at the declaration (which makes it an optional parameter), e.g.: ``` def printer(pdf, type="default type") print pdf + type end printer("file.pdf ") ``` #Ruby


Strings, integers, floats, and everything in Ruby, is an object, which makes Ruby a fully object-oriented programming language. #Ruby


When defining a function in Ruby we can omit the parenthesis, even when calling the function... e.g.: ``` def all puts "everything" end all ``` prints everything


".times do" is a useful alternative to "for" loops, as it's a built-in Ruby method, i.e.: ``` v = 0 5.times do v=v+1 puts "done #{v} times" end ``` #Ruby


What if we want to increase (or decrease) a variable by one, what can be done? we have three possible solutions: ``` # long answer c = c + 1 # shorter version c++ # used in other cases ++c ``` #Ruby


Conditionals in one code: ``` while true until false if true unless false puts "all passed" break end break end break end break end ``` #Ruby


We can use Ruby "Procs" instead of functions, which allow to store code inside a variable, e.g: ``` agree = Proc.new do |param| puts "I agree with you #{param}" end ``` can be called with "agree.call 'name' ", like lambdas. #Ruby


To re-execute loops in Ruby we use the "redo" keyword, so, when the loop finds it, it ignores the rest of code and redos itself (e.g. in if statement.) #Ruby


The == operator checks if two pieces of data are equal, however, it does not compare data types. i.e: int is same as float, to solve this we use the "equ?()" method: ``` print 21 == 21.0 print 21.equ(21.0) ``` prints TrueFalse


You knew you can reach the 12th dimension with Ruby? Yes you can with 12d arrays ``` quest = [2, [6, 4, [[2, [[[[[[[["12D here"]]]]]]]], 7]], 4], 2, 7] puts quest[1][2][0][1][0][0][0][0][0][0][0][0] ```


If you place a boolean in mathematical operations, the boolean gets replaced with one of the two values: - true is replaced with 1 - false is replaced with 0 #Ruby


Don't forget to use parentheses when combining logical operations, to clearify: ``` if d==z && t!=2 end # is not the same as if d==(z && t)!=2 end # nor if d==(z && (t !=2)) end ```


Three modes of accessability in Ruby classes: - "public" - anyone can access anywhere - "protected" - only the class itself can access, and not from the outside - "private" - cannot be accessed from the outside, nor called with the "self" keyword.


Modules can be used to duplicate specific parts of code to different classes--without having to rewrite code--and for organization purposes, e.g.: ``` module Font def size return 14 end end class Text include Font end t = Text.new puts t.size ``` #Ruby


You can load a Ruby gem using the simple "require" keyword (note: you need to have the gem installed), i.e.: ``` require 'digest' # loads the library ``` #Ruby


Methods used to handle files in Ruby (part 3): - File.read(filename) - read file data - File.delete(filename) - delete a file - File.file?(filename) - returns true if file exists - File.zero/?(filename) - returns true if file exists and is empty #Ruby


Methods used to handle files in Ruby (part 1, due to character limitations): - File.new(filename, editing_mode) - create a new file - File.open(filename, editing_mode) - open an existing file - File.close #Ruby


Methods used to handle files in Ruby (part 2): - File.puts(text) - write data to a file (including a \n) - File.write(text) write data to a file (without a line break) -- ``` File.readlines(filename).each { |line_data| print line_data } ``` to loop through file lines #Ruby


A more handsome way to loop on arrays is to use ".each do", a bulid-in functionality in Ruby, e.g.: ``` array.each do |item| print "#{item + 4} " end ``` (item contains array contents one by one) #Ruby


This account does not have any followers
This account does not follow anyone

United States Trends

Loading...

Something went wrong.


Something went wrong.