This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Date Created: 17 June 2021 | |
# Ruby Version 3.0.0p | |
def object_id_of_str obj_1, obj_2 | |
# The argument has same object_id as original object because.. | |
# ..it refers to same object, it doesn't copy objects' value but it points.. | |
# ..to original objects' memory, hence holding objects' reference ID in the memory | |
puts "Before modification, object 1 ID: #{obj_1.object_id}" # => 60 | |
puts "Before modification, object 2 ID: #{obj_2.object_id}" # => 80 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Date Created: 16 June 2021 | |
# Ruby Version 3.0.0p | |
class Abcd;end | |
# ------------------ Superclass --------------- | |
Abcd.superclass # => Object | |
Abcd.superclass.superclass # => BasicObject | |
Abcd.superclass.superclass.superclass # => nil | |
# as BasicObject is top level class that doesn't have any parent class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Date Created: 20 June 2021 | |
# Ruby Version 3.0.0p | |
# initiated constant | |
FRUIT = 'Apple' # => Apple | |
# can be modified | |
FRUIT << 's' # => Apples | |
# and can also give totally different value, but with a warning raised by ruby | |
FRUIT = 'Orange' # => warning: already initialized constant FRUIT | |
# => Orange |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local_var = "I am local var defined in root." | |
puts local_var # => "I am local var defined in root." | |
# accessing local var which is defined in root | |
def access | |
puts local_var # => NameError | |
end | |
# accessing variable in nested method | |
def parent_method |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string = "I am a string." # => String | |
int = 123 # => Integer | |
float = 34.5 # => Float | |
complex = 1i # => Complex | |
boolean = true # => Boolean | |
array = [a,b,c] # => Array | |
hash = {ruby: 3.0} # => Hash | |
total = int + float # => 157.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Inst_var | |
# will initialize instance variable when class is initiated | |
def initialize | |
@count = 0 | |
end | |
# add up | |
def add_count | |
@count += 1 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# accessing through instance of a class | |
object.instance_variables # => @var | |
# accessing through class after initilize | |
Inst_var.new.instance_variables # => @var | |
# accessing directly through class | |
Inst_var.instance_variables # => NoMethodError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Class_var | |
# class variable | |
@@count = 0 | |
# will increase class variable count as soon as class is initiated | |
def initialize | |
@@count += 1 | |
end | |
def output | |
@@count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object.class_variables # => NoMethodError | |
Class_var.class_variables # => [:@@count] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# global variable | |
$count = 0 | |
class Class_var | |
# will increase class variable count as soon as class is initiated | |
def initialize | |
$count += 1 | |
end | |
# we dont need this for global var |
OlderNewer