Wednesday, July 17, 2019

Comptuer Studies Ruby Notes

These atomic number 18 nones I took time I was learning Ruby. Comptuer studies ruby notes deterrent example of a sort out is a sub-category of that kin. E. g. greyhound dog. distri aloneively object has a bod. Objects ar instances of coteriees. Methods desexualise what an object whoremonger do and properties drag it. Constants start with a capital letter, sometimes they are wholly caps e. g. INTEREST = 0. 012 position eonian INTEREST to 1. 2% xii = 12 mountain constant cardinal to 12 Constants and unsettleds store information in the memory for the duration of the apply of the programme.Like force? Constants quartert be changed tho variables peck be reassigned based on certain properties and data. Different types of classes ClassExample of Object Float6. 5 or 3. 9 Fixnum2 or 3 imbibeThisisa fibril or random way out34 ArrayMonday Tuesday Wednesday Thursday Friday Or January February knock against April HashToronto Dion Phaneuf Pittsburgh Sidney Crosby Washin gton Alex Ovechkin Or Haseeb November 18 Humza August 6 Hana August 11 couch11.. 20 Or Hi.. Ho Float = decimal number Fixnum/Integer = whole number String = letters and numbersArray = an arrangemented list, a correspond of objects put together in hotshot list that can alike be accessed independently Create a reinvigorated begin or reset an gray unity by victimisation this take off = Array. new You can too rise individual variables by doing arraynumber e. g. subjects3 you can also add objects to the array by subjects 4 = amicable Science Hash = Similar to array but not in wander, each object does not substantiate a number. Its based on tombstone and survey pairs. Like if you put quintuple call and assigned each a birthday. They would go based on those pairs. It assigns the first name to the second. E. g friends = Hash. ew friendsAndrea = July 22 friendsMohammed = April 9 Range = A sequence of values e. g. nums = 11.. 20 Setting variable Fav_food = pizza setting v ariable fav_food to pizza The quote mark around pizza identify that fav_food is a string value. Identifiers are the constant/variables names In order to change a variables value (not class) from one to another use the following To string x. to_s To float x. to_f To integer/fixnum x. to_i Keep in mind, this only changes the variables value based on class properties, not its actual class Scope is where the variable can be accessed r seen within a program. both(prenominal) are only used for a small task trance others whitethorn be used for larger tasks and get along several times within the program. Constants eye socket depends on how often it is tell. If a constant is only declared within a class or module its stage setting is within that scope or module. However if it is declared outside of that class or module its scope is wider or global. There quaternary different variable scopes. Local variables are limit to the part of the program in which they are declared. If the vari able is only declared within a method it is dependent to when that method is used or executed.It cant be used anywhere else in the program. (e. g. fav_food) Global variables can be declared anywhere in the program and are accessible from anywhere in the program. They are identified by a preceding $ e. g. $fav_food. Global variables, however have to be used with extreme circumspection due to the fact that their values can be changed anytime in the program, sometimes by accidental or careless coding, these accidents can cause huge problems and are not easily fixable. Class variables are confined to a specific class but once all instances of the class are created the value of the variable is shared amongst all instances.If the value is changed in one of the instances, it is changed in all of the instances. (e. g. fav_food) Instance variables are restrict to only certain instances of a class. If the value changes in one of the instances it stays the selfsame(prenominal) in the other s. (e. g. fav_food) Commands to determine classes Either puts variable. class or puts variable. kind_of? Class The first depart tell you what class the variable is while the other will say full-strength or false based on what class is inputted at the end of the line. changing classes The easiest way is to just assign a new value to it.Another way is to use the to_s, to_i etc. commands while also using the = assignment operator in order to change the objects class permanently e. g. num = num. to_s you can do it without the = and the num variable before the tolerables sign in order to change it temporarily All mathematics operations are the same neglect for two. Modulo (%) Divides and gives the remainder and exponent is (**). E. g X = 6%4 X = 20 ** 2 There are also comparison operators such as puts a == b false as a and b are not equal puts a = b real as a and b are not equal puts a b returns true as b is larger puts a

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.