Skip to content

Instantly share code, notes, and snippets.

View JuzerShakir's full-sized avatar
🎯
Focusing

Juzer Shakir JuzerShakir

🎯
Focusing
View GitHub Profile
@JuzerShakir
JuzerShakir / 01.str.rb
Last active June 23, 2021 06:05
A look into object_id
# 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
@JuzerShakir
JuzerShakir / ancestors_&_superclass.rb
Created June 23, 2021 06:21
A look into hierarchical tree of classes and modules in ruby
# 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
@JuzerShakir
JuzerShakir / main.rb
Last active June 23, 2021 06:38
Constants & Variable in Ruby
# 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
@JuzerShakir
JuzerShakir / local_var.rb
Last active June 30, 2021 04:44
Local Variable medium
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
@JuzerShakir
JuzerShakir / define_var..rb
Last active July 8, 2021 03:01
Used in variable doc medium
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
@JuzerShakir
JuzerShakir / instance_var.rb
Last active June 30, 2021 04:45
Used in Variable doc in medium
class Inst_var
# will initialize instance variable when class is initiated
def initialize
@count = 0
end
# add up
def add_count
@count += 1
end
@JuzerShakir
JuzerShakir / get_instance_variables.rb
Last active June 30, 2021 03:16
Getting instance variables, for variable doc in medium
# 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
@JuzerShakir
JuzerShakir / class_var.rb
Created June 30, 2021 02:53
Used in Variable doc in medium
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
@JuzerShakir
JuzerShakir / get_class_var.rb
Created June 30, 2021 03:09
Used in variable doc in medium
object.class_variables # => NoMethodError
Class_var.class_variables # => [:@@count]
@JuzerShakir
JuzerShakir / global_var.rb
Last active July 8, 2021 02:53
For variable doc medium
# 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