Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

ruby create class method

# class << self opens up self's singleton class, so that methods can be redefined for the current self object
class String
  class << self
    def value_of obj
      obj.to_s
    end
  end
end

String.value_of 42   # => "42"

# This can also be written as a shorthand:

class String
  def self.value_of obj
    obj.to_s
  end
end

# Even shorter
def String.value_of obj
  obj.to_s
end
Comment

creating a class in ruby

class SampleHumanClass
	attr_accessor :name, :age, :gender
    
    @@all = []
    
    def initialize(name, age, gender)
    	@name = name
        @age = age
        @gender = gender
        @@all << self
    end 
    
    def self.all
    	@@all
    end
end

#now we can create an instance of the class

mike = SampleHumanClass.new("Mike", 32, "Male")
Comment

ruby class

class Dog # Initialize class!
    def initialize(name) # Initialize function of class Dog
        @name = name # Set name to param in class
    end

    def print_name() # Function to print out the name in params
        puts @name # Puts!
    end
end

my_dog = Dog.new "Bruno" # Create class and save it in variable
my_dog.print_name # Call print_name function from class

# Output:
# Bruno
Comment

ruby class variable

class Customer
   @@no_of_customers = 0
end
Comment

Classes and objects in Ruby

class Person
	def initialize(name) # this is an empty class
    end
end

p1 = Person.new("Ben")

class Person
	def initialize(name)
    	@name = name #This is an instance variable
    end
end

p1 = Person.new("Ben")

#To recall the name
class Person
	def initialize(name)
    	@name = name
    end
    def name
    	@name
    end
end

#We can call the above
p2=Person.new("Brian")
puts p2.name

#Attribute writers

class Person
  def initialize(name)
	@name = name
  end
  def name
  	@name
  end
  def password=(password)
	@password = password
  end
end

p3=Person.new("Margret")
p3.password = "lovydovy"
p p3
Comment

Ruby Classes

class Library
  attr_accessor :title, :author

  def readBook()
    puts "Reading #{self.title} by #{self.author}"
  end
end

book1 = Library.new()
book1.title = "Shreds of tenderness"
book1.author = "Dr Mwaniki"

book1.readBook()
puts book1.title
Comment

PREVIOUS NEXT
Code Example
Ruby :: rspec log to console 
Ruby :: using module function in main ruby 
Ruby :: ruby find object identifier 
Ruby :: Range extraction: convert a comma separated list of integers into range format 
Ruby :: rails active record to fetch only list of ids 
Ruby :: print in rails 
Ruby :: link_to 
Ruby :: allow raise inside rescue rspec 
Ruby :: ruby substring 
Ruby :: api blueprint minitest rails 
R :: r ggplot regression line 
R :: remove elements from character vector in r 
R :: how to append in R list 
R :: r stack data frames 
R :: r change a single value in a dataframe 
R :: r bar plot 
R :: superscript in r 
R :: how to extract p value from lm in r 
R :: sequence r 
R :: convert na to 0 in r 
R :: find row with na r 
R :: how to wait for a key press in R 
R :: if a condition is true skip loop r 
R :: how to make the minutes zero in r 
R :: how to add a totals row in r using mutate 
R :: Levels in factor in r 
R :: excel date format r 
R :: XLConnect 
R :: how to change column names in r 
Rust :: rust get items in a list with index and value 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =