Classes and Objects Flashcards

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

How to see all instance variables for the instance of a class?

A

.instance_variables

class Hello
def initialize
@sample = 2
end
end

Hello.new.instance_variables #=> [@sample]

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

What is the difference between supper and super() ?

A

super -> call superclass method with params from the method by default
super() -> calls methods without params

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

How to see all possible methods of the object?

A

.methods

class Hello
def hello
end
end

Hello.new.methods #=> [:hello, …]

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

What is self?

A

will show the same object

It’s a current object. An instance of the class inside the clas

class MyClass
def first_method
puts “Current instance within first_method: #{self}”
end
end

my_object = MyClass.new
puts “my_object refers to this object: #{my_object}”
my_object.first_method

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

When we must use the method self explicitly inside the class?

A

When you try to assign local values with the same name as a method

class MyClass
def first_method
a = 10
# self.a #=>’hello’
end

def a
‘hello’
end
end

my_object = MyClass.new
my_object.first_method
# => 10

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

How to see all the ancestors of the class in the right order?

A

=> [Class, Module, Object, Kernel, BasicObject]

Class.ancestors

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

How to make an existing method private?

A

private :method_name

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

How to make an existing class method private?

A

private_class_method :new

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

What is the base ruby class?

A

class Object < BasicObject
include Kernel
end

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

What is the class of class?

A

something like this

Class.class # => Class

class Class

end

Example = Class.new
instance_of_class = Example.new
instance_of_class.class # => Example

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

How to find what modules are included in a class?

A

Class.included_modules
# [Kernel]

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

What returns .singleton_methods on the class?

A

This method returns all class methods

class Animal
def self.test
end
end

p Animal.singleton_methods
# [:test]

The singleton class is like an “empty shell”, used only for the purpose of holding onto methods, it lives between the object itself & the object’s class.

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

What are the differences between prepend and include module?

A

Prepend overrides class methods

module Hello
def hello
p ‘from module’
end
end

class Name
prepend Hello

def hello
p ‘from class’
end
end

Name.new.hello
# from module

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

What happens if you define the same class twice but with different methods?

A

class Hello
def x
‘x’
end
end

class Hello
def y
‘y’
end
end

hello = Hello.new
p hello.x # x
p hello.y # y

They extend each other

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

Is it possible to assign class name to variables?

A

class Hello
def say
p ‘a’
end
end

hello = Hello
a = hello.new

a.say

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

What is the second way to define class? and with Inheritance?

A

Inheritance

hello = Class.new do
def say
p ‘say’
end
end
hello.new.say # say

# Class.new(Array)

17
Q

Is it possible to create an instance of Module class?

A

yes

18
Q

What is the second way to create module?

A

$sample = Module.new do
def hello
p ‘hello’
end
end

class Hello
include $sample
end

Hello.new.hello

19
Q

What are the differences between variables and constant?

A
  • The scope ( variables don’t available everywhere )
20
Q

What’s the class of Object? BasicObject? Class? Module?

A

=> Class

p Object.class
p BasicObject.class
p Class.class
p Module.class

21
Q

What’s the superclass of Module?

A

p Module.superclass
# Object

22
Q

Is it possible to call a private method with an explicit receiver? (self.method)

A

Private methods are governed by a single simple rule: you cannot call a private method with an explicit receiver. In other words, every time you call a private method, it must be on the implicit receiver—self.

class C
def public_method
self.private_method
end

private

def private_method; end
end

C.new.public_method
# raised error

23
Q

What does Refinement mean? How to use it?

A

refine to define and using to add

Refinements are similar to Monkeypatches, but they’re not global. A Refinement is active in only two places: the refine block itself and the code starting from the place where you call using until the end of the module (if you’re in a module definition) or the end of the file (if you’re at the top level)

module StringExtension
refine String do
def hello
self * 10
end
end
end

using StringExtension

p ‘s’.hello

24
Q

What are three places where a program leaves the previous scope behind and opens a new one?

A

• Class definitions ( class keywords)
• Module definitions ( module keywords)
• Methods ( def keywords)

25
Q

Is it possible to define method inside another method?

A

Yes

def hello

def hello2
p ‘Hello’
end

hello2

end

hello # ‘Hello’

26
Q

What are the differences between Class Variables, Class Instance variable and instance variable of class object?

A

Class Variable—they belong to class hierarchies ( something like global value)

class MyClass
@@my_var = 1 # Class Variable
@my_var = 1 # Class Instance Varible
def self.read; @my_var; end
def write; @my_var = 2; end # Intance variable of class object
def read; @my_var; end
end

obj = MyClass.new
obj.read # => nil
obj.write
obj.read # => 2
MyClass.read # => 1

27
Q

What is SIngleton Method?

A

str = ‘dd’

def str.hello
self.upcase
end

p str.hello # DD

A method like this one, which is specific to a single object, is called a Singleton Method.

28
Q

How to create your own attr_reader ?

A

1. define macros

# 2. dynamically create method inside
# 3. get variable

class Hello

def self.my_attr_reader(method)
define_method method do
instance_variable_get(“@#{method}”)
end
end

def initialize
@hello = ‘Sample’
@hello2 = ‘SSS’
end

my_attr_reader :hello
my_attr_reader :hello2

end

p Hello.new.hello
p Hello.new.hello2

29
Q

How extend module work inside?

A

module MyModule
def my_method; ‘hello’; end
end

class MyClass
class &laquo_space;self
include MyModule
end
end

p MyClass.my_method

30
Q

How to get 1+ 1 == 3?

A

alias do not override both methods

class Integer
alias_method :original_plus, :+

def +(value)
self.original_plus(value).original_plus(1)
end
end

p 1+ 1

31
Q

What is class macros?

A

attr_reader
attr_accessor

and methods something like this

32
Q

How to disable a ruby method?

A

Ruby provides a special keyword which is known as undef keyword. This keyword used to avoid the current working class from responding to calls to the specified named methods or variable.

def hello
p ‘a’
end

undef hello
# or remove_method hello ( which will remove from the class but not from parrents)

33
Q

Is it possible to get access inside the class/module/method to the local variable outside?

A

Yes, if class was created via block. The same for module and define_method

a = 10
Hello = Class.new do
p a
end

Hello.new
# 10
# method
a = 10
define_method(:hello) do
p a
end
hello

34
Q

Can you tell me how Constant are inherited for classes?

A

1

class C
Hello = 2
end

module A
Hello = 1
class B < C
p Hello
end
end

35
Q

How to create class methods? 4 options

A

class Hello
def self.method_1
p “method 1”
end

class &laquo_space;self
def method_2
p “method 1”
end
end
end

def Hello.method_3
p ‘method 3’
end

class &laquo_space;Hello
def method_4
p ‘method 4’
end
end

Hello.method_1
Hello.method_2
Hello.method_3
Hello.method_4

36
Q

How to check if a new class has been instantiated or new module has been included?

A

def self.inherited(subclass)
end

def self.included(klass)
puts “Hey, I’ve been included in #{klass}”
end