Search This Blog

June 7, 2017

Ruby Commands



Ruby Commands

String
Text, Alphabets should be entered in inverted commas
Array
List of numbered items in a square bracket, separated by comma. [0,1,2]. Remember position in array starts from 0 and not 1.
Puts
Output, execute
Print
Prints on screen
Conditions
False, Nil    True,0
**
Raise to power
%
Gives us the remainder of the division
==
Test whether the condition holds true of false
.eql?
Checks equal in absolute term 3.eql?3.0 (False)
!=
Not Equal to
If
Checks the argument. Each if to have corresponding end
Else
Used for false return in If formula.
Elsif
Checks multiple if conditions
Unless
Executes the command if condition is false
&&
 And, checks both conditions in If argument
||
Or, evaluates either of the conditions as true
!
Not, reverses the output i.e true conditions is responded as false
Case, when
Checks the value of case and gives result based on when conditions. Each case to have corresponding end.
While
Keeps on executing the code till statement holds true.
Untill
Keeps on executing the code untill statement holds false
(..) Two dots
Inclusive range. When used with square brackets it presumes the given numbers as positions and not numbers of the range.
(…) Three dots
Exclusive range
to_a
Convert range into array
For x in (..)
Returns values of that range in a loop
Loop do
Create a loop
Break
To stop a loop
Next
Skip iteration in the loop
<<, .push
To put a value in the end of the array
Pop
Removes the last value in the array
.insert (position number, value)
Inserts the value at the desired positon in the array list
Delete_at (Position number)


Array Manipulations           
&
Find common elements in brackets
| and .uniq!
Removes duplicates in a array
.reverse

.reverse!

.length and .size
Number of elements in array
.sort
Sorts the elements in array
.freeze
Freezes array from being modified
.include?(obj)
Searches for Obj in array and returns true if present.
.min
Returns the minimum value in the array
.max
Returns the element with maximum value
-1
Returns the last element of the array





Hash Manipulations
Month = {“Jan”=>“January”}
Hash = {Key=>Value}
A=:10,A:10
Key=:Symbol/Value. These cannot be changed.
hash.DELETE(:key)
Deleted particular key in the hash
hash.INDEX(:value)
Reverse searches and gives key for the value specified. Returns nil if not found.
Hash.INVERT
Keys become value and values becomes keys
Hash.KEYS
Returns the keys in the hash
Hash.values
Returns the values in the hash
Hash.length
Returns the length of keys in the hash as Integer

.each  | |
Converts each array item into a variable.
Times
Creates loop for prescribed number of times
.downcase!
Convert all letters into lowercase

Methods
Def X  puts Y   end
Define a method. Renames/Processes a thing. Here X is called parameter.
Gets.chomp
Used to take user input
Return
Prints the called value. Anything mentioned after return and before end is not counted.
Private
Method defined after it won’t be available outside that class.
Rand()
Pick random number.

Types of variables
Local
Available only inside the loop
Global
Prefixed with $ sign. Can be changed from anywhere in the code.
Instance
object.methodname
(getter method)

Executes that method defined in the class. Fetches the variable
To_s
Is activated by default when calling for the object for the class.
Prints object class.random encoding of the object id.
If the same is defined as a method, it can give information in a format.
Set_name=(name)
Object.set_name will change the already attached variable to the object.
@
To create Instance variable. Tagging object/identity to variable. These are accessible from anywhere in the project.
Class
Outlines what an object would be
Classname.NEW
To add new objects to the class
@@
Class variable. These are used to get information about class and not individual objects.
CAP,PI
Constant that do no change value
Class::CONSTANT
To call for the constant
Initialize
Method defined inside a class. It is called for every object created.
attr_accessor:Object name, objectname, objectname

Self.object
Class method.For calling the method within class. For defining the method which can be called along with class.
Show_info






Class
 <
Subclass (derived class) < Superclass (Base class)
Subclass inherits/uses/gets the methods and attributes.
Base class may have multiple sub class. But Sub class cannot have multiple superclasses.
However any method of the same name defined in subclass will overrule the method definition of main class.
Super
Calls for corresponding method of main class. Allows subclass method to give output after main class method output.
Module
Collection of method that can be used in other classes. An object cannot be created from module.
Module::Class.new


Module.classmethod
Self.method
Include
To include module into class.

Class can inherit from only one class but can include any number of modules.
Struct
Structure
Require ostruct
Open structure
Maths
Some predefined Maths command
Time
Some predefined Time command.
Proc

Lambda

 Do
Define a method
_proc.call
To call a method created using Proc
 call




File.new(“Filename”, “Accesspermisions”)

R
Read only
R+
Read + Write
W
Write only
File.file?(“Filename”)
To return true if the file exists and false if it does not exist.
File.open(“Filename”, “Accesspermisions”)

File.read(“filename”)
File.readable?

File.readlines (“filename”)

File.close
Remember to close the file after performing operations.
File.delete(“filename”)

F.size
Sixe of file
File.writable?

File.executable?


No comments:

Post a Comment