Section 1

Preview this deck

while <condition> # code goes here end

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

1

All-time users

1

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (41)

Section 1

(41 cards)

while <condition> # code goes here end

Front

while loop

Back

Math class

Front

Ruby has a Math class which contains the mathematics classes

Back

<array_name> = Array.new <array_name>[0] = <value> <array_name>[3] = <value>

Front

the second '1' and the third '2' elements will contain 'nil'

Back

.to_s method

Front

converts a number to a string

Back

def method_name(param1, param2, paramN[=<default-value>]) # code goes here end

Front

defining a method

Back

for line in <file>.readlines() # code goes here end

Front

loop through the lines of a file <file> is an opened file for reading

Back

<file>.read()

Front

read all the content of the file <file> is an opened file for reading

Back

<array_name> = Array[<e1>, <e2>, <e3>, <en>]

Front

declaring an array with elements

Back

begin # code might throw an error rescue <error-name> => <e> # executed code if the <error-name> has been occurred end

Front

error handling You can write many rescue blocks as needed

Back

Array methods

Front

.length() .include? <value> # check if the array contains <value> .reverse() .sort()

Back

if condition1 or condition2 # code goes here elsif !condition3 and condition4 # code goes here else # code goes here end

Front

if statement implementation Notes: - you can use the 'or' and the 'and' keywords to combine conditions - you can use the exclamation mark '!' to negate a condition

Back

string methods

Front

.upcase() .downcase() .strip() # delete useless spaces in the beginning and the end of the string .length() .include? "<substring>" # returns boolean which indicates if the string contains the <substring> .index(<substring>) # returns the position of substring in the string

Back

module <module-name> # method definitions go here end

Front

create a module. Module is a container of methods a module must be included using the: include "<module-name>"

Back

<hash_name> = { <key1> => <value1>, <key2> => <value2> }

Front

declaring hashes

Back

for i in <b>..<e> # code goes here end

Front

loop from <b> to <e>

Back

for <element> in <collection> # code goes here end

Front

loop through a collection of elements

Back

<array_name>[i,n]

Front

get another array starting from the 'i'th element to the 'n-1' element

Back

<variable_name> = gets.chomp()

Front

read input from the user without returning to new line

Back

return <value1>, <value2>, <valueN>

Front

you can return more than one value in a method. It's like an Array You can access to the returned values using the indexing. example: my_method[1] # returns the 2nd value

Back

Difference between 'puts' and 'print'

Front

'puts' show the string on the screen and return to a new line however with the command 'print' doesn't return to a new line after printing the string

Back

string_var[i,n]

Front

gets the substring starting from the position 'i' to the 'n-1'

Back

<file>.readchar()

Front

read a character from a file <file> is an opened file for reading Note: if you call the method readchar() again on the same file, the next character will be read

Back

require_relative "<file-path>"

Front

include a file in the script. However, if the file doesn't exist or Ruby can't include it, an error will occur and the script will stop from running

Back

File.open("<file-path>", "<opening-mode>") do | <filename-variable> | # operations on the file gohere end

Front

open a file

Back

include "<module-name>" <module-name>.<method-name>()

Front

calling a method from a Module

Back

<file> = File.open("<file-path>", "<opening-mode>")

Front

open a file Note: the file must be closed later

Back

<file>.close()

Front

close an opened file

Back

<file>.readlines()

Front

returns a list of the remaining lines in the file <file> is an opened file for reading

Back

class <sub-class-name> < <class-name> # sub class implementation goes here end

Front

inheritance

Back

begin # code might throw an error rescue # executed code if an error has been occurred end

Front

errors handling

Back

<array_name> = Array.new

Front

declaring an empty array

Back

class <classname> attr_accessor :<attribute1>, :<attribute2>, :<attributeN> def initialize(<param1>, <param2>, <paramN>) # This is the constructor of the class. # It's called every time an object is created @<attribute1> = <param1> @<attribute2> = <param2> @<attributeN> = <paramN> end end

Front

creating a class

Back

=begin comments goes here =end

Front

multi-line comment

Back

<array_name>.each do |<element>| #code goes here end

Front

iterate through an array of elements

Back

<integer>.times do | index | # code goes here end

Front

loop <integer> times

Back

.to_f method

Front

converts the data to float number if the data doesn't start with a number, the conversion result would be 0.0

Back

<file>.readline()

Front

read a line from a file <file> is an opened file for reading Note: if you call the method readline() again on the same file, the next line will be read

Back

.to_i method

Front

converts the data to integer if the data doesn't start with a number, the conversion result would be 0

Back

case <variable_to_check> when <value1> # code goes here when <value2> # code goes here when <valueN> # code goes here else # code goes here end

Front

case statement

Back

<variable_name> = gets

Front

read input from the user Note: when you enter an input, it will contain a return to new line Note: Ruby will treat the gotten input as a string even if you have entered only numbers

Back

<class-name>.new(<param1>, <param2>, <paramN>)

Front

create an instance of <class-name> class

Back