These are notes I took whereas I used to be studying Ruby. Comptuer research ruby notes Occasion of a category is a sub-category of that class. E. g. greyhound; canine. Each object has a category. Objects are cases of lessons. Strategies outline what an object can do and properties describe it. Constants begin with a capital letter, generally they’re all caps e. g. INTEREST = zero. zero12 #setting fixed INTEREST to 1. 2% DOZEN = 12 #setting fixed DOZEN to 12 Constants and variables retailer info within the reminiscence in the course of using this system.
Like RAM? Constants can’t be modified however variables might be reassigned based mostly on sure properties and information. Various kinds of lessons: ClassExample of Object Float6. 5 or three. 9 Fixnum2 or three StringThisisastring or randomnumber34 ArrayMonday Tuesday Wednesday Thursday Friday Or January February March April HashToronto [Dion Phaneuf] Pittsburgh [Sidney Crosby] Washington [Alex Ovechkin] Or Haseeb [November 18] Humza [August 6] Hana [August 11] Vary11.. 20 Or Hello.. Ho Float = decimal quantity Fixnum/Integer = entire quantity String = letters and numbers
Array = an ordered record, a few objects put collectively in a single record that will also be accessed independently Create a brand new array or reset an outdated one by utilizing this: array = Array. new You can too present particular person variables by doing: array[number] e. g. topics[3] you may also add objects to the array by: topics [4] = ‘Social Science’ Hash = Just like array however not so as, every object doesn’t have a quantity. It’s based mostly on key and worth pairs. Like in the event you put 5 names and assigned every a birthday. They’d go based mostly on these pairs. It assigns the primary title to the second. E. g: associates = Hash. ew associates[‘Andrea’] = ‘July 22’ associates[‘Mohammed’] = ‘April 9’ Vary = A sequence of values e. g. nums = 11.. 20 Setting variable: Fav_food = ‘pizza’ #setting variable fav_food to pizza The quote marks round pizza establish that fav_food is a string worth. Identifiers are the fixed/variables names With the intention to change a variable’s worth (not class) from one to a different use the next To string: x. to_s To drift: x. to_f To integer/fixnum: x. to_i Be mindful, this solely adjustments the variables worth based mostly on class properties, not its precise class Scope is the place the variable might be accessed r seen inside a program. Some are solely used for a small process whereas others could also be used for bigger duties and seem a number of occasions throughout the program. Constants’ scope depends upon how usually it’s declared. If a continuing is simply declared inside a category or module it’s scope is inside that scope or module. Nevertheless whether it is declared exterior of that class or module it’s scope is wider or “world”. There 4 completely different variable scopes. Native variables are confined to the a part of this system wherein they’re declared. If the variable is simply declared inside a way it’s restricted to when that technique is used or executed.
It may well’t be used wherever else in this system. (e. g. fav_food) World variables might be declared wherever in this system and are accessible from wherever in this system. They’re recognized by a previous “$” e. g. $fav_food. World variables, nonetheless have for use with excessive warning resulting from the truth that their values might be modified anytime in this system, generally by unintentional or careless coding, these accidents may cause big issues and will not be simply fixable. Class variables are confined to a particular class however as soon as all cases of the category are created the worth of the variable is shared amongst all cases.
If the worth is modified in one of many cases, it’s modified in all the cases. (e. g. @@fav_food) Occasion variables are restricted to solely sure cases of a category. If the worth adjustments in one of many cases it stays the identical within the others. (e. g. @fav_food) Instructions to find out lessons Both: places variable. class or places variable. kind_of? Class The primary will let you know what class the variable is whereas the opposite will say true or false based mostly on what class is inputted on the finish of the road. Altering lessons The simplest method is to only assign a brand new worth to it.
One other method is to make use of the to_s, to_i and many others. instructions whereas additionally utilizing the “=” project operator to be able to change the item’s class completely e. g. num = num. to_s you are able to do it with out the “=” and the num variable earlier than the equals signal to be able to change it briefly All math operations are the identical besides for 2. Modulo (%) Divides and offers the rest and exponent is [Sidney Crosby]. E. g: X = 6percent4 X = 20 ** 2 There are additionally comparability operators corresponding to: places a == b #false as a and b will not be equal places a ! = b #true as a and b will not be equal places a < b #returns true as b is bigger places a