General Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Name all the 19 Parts of Speech of the Ruby language.

A

Variables, Global Variables, Instance Variables, Class Variables, Constants, Symbols, Methods, Class Methods, Method Arguments, Numbers, Strings, Blocks, Block Arguments, Ranges, Arrays, Hashes, Regular Expressions, Operators, Keywords,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Any plain, lowercase word.

A

Variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Variables can consist of what types of characters?

A

letters, digits and underscores

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are these examples of? x, y, banana2, phone_a_quail

A

Variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Two types of Numbers

A

Integers and Floats

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

A series of digits which can start with a plus of minus sign

A

Integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are these examples of? 1, 23, -1000, 12_000

A

Integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Numbers with a decimal point or scientific notation

A

Float

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are these examples of? 3.14, -8,08, 12.043e-04

A

Float

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Any sort of characters surrounded by double or single quotes

A

String

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are these examples of? “sealab”, ‘2021’, “These cartoons are hilarious”

A

String

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Look like variables, but start with a colon

A

Symbol

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are these examples of? :a, :b, :ponce_de_leon

A

Symbol

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Looks like variables, but capitalized

A

Constants

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are these examples of? Time, Array, Bunny_Lake_Is_Missing, EmpireStateBuilding

A

Constant

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Verbs attached to variables and constants by a dot

A

Method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Provides additional information to a Method to perform more specific actions

A

Method Argument

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What are these examples of? front_door. open

A

variable.method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What are these examples of? front_door.paint( 3, :red)

A

variable.method.method argument

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Usually attached after a variable and/or constant with a double colon

A

Class Method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What are these examples of? Door::new( :oak)

A

Constant::class method (method argument)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Variables that start with a dollar sign

A

Global Variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What are these examples of? $1, $x, $chunky

A

Global Variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Variables that begin with @ symbol often used to define the attributes of something

A

Instance Variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What are these examples of? @x, @y,

A

Instance Variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Variables that begin with a double @@ used to define attributes to many related objects

A

Class Variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What are these examples of? @@x, @@y

A

Class Variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Characters surrounded by curly braces to group a set of instructions together so that they can be passed around

A

Block

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

A set of variables surrounded by pipe characters and separated by commas

A

Block Argument

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Two values surrounded by parantheses and separated by an ellipsis…two or three dots

A

Range

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What are these examples of? (1..3)

A

Range

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

A list surrounded by square brackets and separated by commas

A

Array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What are these examples of? [1,2,3]

A

Array

34
Q

A dictionary surrounded by curly braces. Dictionaries match words with their definitions. Ruby does so with arrows made from an equal sign followed by a greater than sign

A

Hash

35
Q

What are these examples of? {‘a’ => ‘aardvark’}

A

Hash

36
Q

A set of characters surrounded by slashes used to find words or patterns in text

A

Regular Expression

37
Q

What are these examples of? /ruby

A

Regular Expression

38
Q

Characters used to do math to compare things

A

Operator

39
Q

Built in words, imbued with meaning that cannot be used as variables of changed

A

Keywords

40
Q

How to print to screen?

A

“puts “

“p “

41
Q

How to output to screen without newline?

A

print

42
Q

How to evaluate variable inside double quotes?

A

“test #{ variable}”

43
Q

How to open and close a file?

A

aFile = File.new(“filename”, “mode”)
process_the_file
aFile.close

44
Q

How to read the first 20 characters in a file?

A

sysread can be used to read the contents of a file

e.g. content = aFile.sysread(20)

45
Q

how to read the lines of a textfile?

A

arr = IO.readlines(“fileltest.txt”)

46
Q

How to read lines with Ruby?

A

File.open(‘test.txt’).each_line{ |s| puts s }
or
File.foreach(‘test.txt’) { |s| puts s }

47
Q

What is the output?def callBlock yield yieldendcallBlock { puts In the block” }”

A

In the block

In the block

48
Q

What does this produce?a = %w( ant bee cat dog elk )a.each { |animal| puts animal }

A
ant
bee
cat
dog
elk
49
Q

What is the effect of this?a = %w{ ant bee cat dog elk }

A

an array of string with values
e.g.
a[0]&raquo_space; “ant”
a[3]&raquo_space; “dog”

50
Q

How do you define a hash/dictionary?

A
instSection = {
  'cello' => 'string',
  'clarinet' => 'woodwind',
  'drum' => 'percussion',  
  'oboe' => 'woodwind',
  'trumpet' => 'brass',
  'violin' => 'string'
}
51
Q

Give an example of simple iteration over array

A

[1,2,3].each do | value |
puts value
end

52
Q

Give different ways to create an array with values: vitamins, minerals and chocolates

A
# Make a list of clues for a game of charades
clues = ['vitamins', 'minerals', 'chocolates']
# Create the same array by splitting on whitespace
clues = %w(vitamins minerals chocolates)
# Create the same array in steps
clues = Array.new
clues << 'vitamins'
clues << 'minerals'
clues << 'chocolates'
53
Q

How can you pass arguments to a block?

A

using | arg | in the start of the block

54
Q

how to match line with regex?

A

if line =~ /Perl|Python/
puts Scripting language mentioned: #{line}”
end

55
Q

how to control flow with if/else in ruby?

A
if customerName == "Fred" 
  print "Hello Fred!"
elsif customerName == "John" 
  print "Hello John!" 
elsif customername == "Robert" 
  print "Hello Bob!"
end
56
Q

how to access substrings?

A

string = this is an example”
=> “this is an example”

value = string[2..6]
=> “is is”

string[11..-1] = “elephant”
string
=> “this is an elephant”

57
Q

how can you iterate from 0 to 10

A

(1..10).to_a.each do |n|

end

58
Q

Meta programming:How to access meta information of a class?

A

instance_variable_get ‘@event’
instance_variable_set ‘@region’, ‘UK’
instance_variables5.public_methods

59
Q

How to create instance variables with introspection?

A
class Person
  def initiliaze(attributes)
    attributes.each do | attr, value |
      instance_variable_set("@#{attr}", val)
    end
  end
end

Person.new :name => ‘Peter’, :age => ‘31’, :sex => :male

60
Q

why are instance variables marked with @ ?

A

with the mark @ with can identify variables as instance variables and do not need getters and setters

61
Q

What is the convention for using ‘?’ as part of method names?

A

Conventional in Ruby to have ‘?’ at the end of the method if that method returns only boolean values

62
Q

What does ‘@’ signify if it’s part of the variable name?

A

Instance variable

63
Q

What is a lambda?

A

lambda is just a function… peculiarly… without a name. They’re anonymous, little functional spies sneaking into the rest of your code. Lambdas in Ruby are also objects, just like everything else!

Convention is to use {} for single line lambdas and do..end for lambdas that are longer than a single line.

64
Q

Lambdas vs. Blocks

A

A lambda is a piece of code that you can store in a variable, and is an object. The simplest explanation for a block is that it is a piece of code that can’t be stored in a variable and isn’t an object. It is, as a consequence, significantly faster than a lambda, but not as versatile and also one of the rare instances where Ruby’s “everything is an object” rule is broken.

65
Q

most common uses for a lambda

A

one of the most common uses for a lambda involves passing exactly one block to a method which in turn uses it to get some work done. You’ll see this all over the place in Ruby - Array iteration is an excellent example.

66
Q

module

A

Modules only hold behaviour, unlike classes, which hold both behaviour and state.

Since a module cannot be instantiated, there is no way for its methods to be called directly. Instead, it should be included in another class, which makes its methods available for use in instances of that class.

67
Q

What is ‘::’ ?

A

:: is a constant lookup operator that looks up the Array constant only in the Perimeter module.

68
Q

symbol, ex :octocat

A

Immutable string whose value is itself, typically used for enumeration. its own primitive type.

Used in Ruby to denote “specialness” such as being one of the set of fixed choices like an enumeration. Symbols can easily be converted back and forth with the methods to_s and to_sym.

69
Q

Return value of ‘puts’ versus ‘return’

A

always nil versus sum

1+1 returns 2

70
Q

primitives versus objects

A

Primitives dont have a common relationship to each other

71
Q

variables

A

Will always be undefined or ACT like an object

They are not objects

72
Q

variable scope indicators

A
Global $variable
Class @@variable
Instance @variable
Local variable
Block variable
73
Q

array

A

An array is an ordered integer- indexed collection of objects.
That’s a very fancy way of saying that we can take objects and put them together in order, and keep their position in the same order, and we can refer to those objects by their positions.

Expanding file folder is good analogy because folders can be empty. Classes can be put in arrays.

74
Q

a.b means: ____ ____ b on ______ a

A

•a.b means: call method b on object a
•a is the receiver to which you send the method call,
assuming a will respond to that method
•does not mean: b is an instance variable of a
•does not mean: a is some kind of data structure
that has b as a member

75
Q

def by_three?

A

best practice to end methods with a question mark if they return a boolean in ruby

76
Q

blocks

A

create a method with no name. Blocks can be defined with either the keywords do and end or with curly braces ({}).

77
Q

is 0 true of false

A

0 or empty string is true. False and nil are the only things that are true. They are not the same thing but they both return false

78
Q

what is returned by default from a method?

A
  • if a method does not explicitly return a value, the last expression’s value is returned, ex
79
Q

poetry mode

A

omit paretheses around arguments to a method call, and omit curly braces when LAST argument to a method call is a hash

there’s a single hash argument, and its the last argument

80
Q

Describe polymorphic associations

A

Allow you to have a single model that can be associated to an arbitrary number of other models.

Person, Company, Address

81
Q

What are nested resources?

A

They are the touted as the correct way to do REST with parent child model associations.

  1. Set up named route
    map. resources :events, :has_many => :tickets
  2. Route helpers
    new_event_ticket
  3. before_filter :get_event
def get_event
@event = Event.find(params[:event_id])
end
82
Q

Migration

A

At the most basic level Migrations allow you to define incremental changes to your data model (and data itself!). This sort of approach melds seamlessly with Agile and XP methodologies favored by Rails developers the world over. A Migration is defined by subclassing ActiveRecord::Migration and overriding the two required method definitions, self.up and self.down:

class SampleMigration < ActiveRecord::Migration
  def self.up
  end

def self.down
end
end

Within the up and down definitions you can create tables and indexes, add data, manipulate models and more.