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

Ruby | Class & Object

# Ruby program to illustrate
# the defining of methods
 
#!/usr/bin/ruby
 
# defining class Vehicle
class GFG
 
# defining method
def geeks
 
# printing result
puts "Hello Geeks!"
 
# end of method
end
 
# end of class GFG
end
 
# creating object
obj = GFG.new
 
# calling method using object
obj.geeks
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 | Class & Object

# Ruby program to illustrate the passing
# parameters to new method
 
#!/usr/bin/ruby
 
# defining class Vehicle
class Vehicle
 
# initialize method
def initialize(id, color, name)
 
# variables
@veh_id = id
@veh_color = color
@veh_name = name
 
# displaying values
puts "ID is: #@veh_id"
puts "Color is: #@veh_color"
puts "Name is: #@veh_name"
puts "
"
end
end
 
# Creating objects and passing parameters
# to new method
xveh = Vehicle. new("1", "Red", "ABC")
yveh = Vehicle. new("2", "Black", "XYZ")
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 :: ruby debugger 
Ruby :: rails clear log files 
Ruby :: rails validates_presence_of 
Ruby :: link to do rails 
Ruby :: ruby on rails validates presence of multiple fields 
Ruby :: capitalize composed name ruby 
Ruby :: meaning of {} in ruby 
Ruby :: how to call ruby private methods 
Ruby :: read headers of csv using ruby 
Ruby :: insert element in array ruby 
Ruby :: arel_table rails 
Ruby :: rails content_tag nested 
Ruby :: ruby generate task 
Ruby :: ruby hash print 
Ruby :: ruby if else 
Ruby :: ruby convert value to boolean 
Ruby :: user.destroy all except one rails 
Ruby :: how to unloack user devise rails 
Ruby :: Rudy control S 
Ruby :: ruby execute code in string 
Ruby :: my rails server exits automatically 
Ruby :: csv file current row number ruby 
Ruby :: capybara click with offset 
Ruby :: rails activerecord saved_change_to 
Ruby :: redis localhost url 
R :: r remove rows where value is 0 
R :: extract r squared from lm in r 
R :: descending order a list in r 
R :: principal component analysis in r 
R :: show 2 ggplots together 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =