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

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")

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

ruby class variable

class Customer
   @@no_of_customers = 0
end

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

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

Ruby相关代码片段

how to remove zsh style

sql server 2016 installation step by step

[{24}x{23-}-]

how to run rake task with arguments

ruby create array with n elements

Ruby Multi-root workspace

ruby each cons

bundle add gem

ruby how to add bundle

focusout event in capybara

clone vs dup ruby

Hash merge, join hashes

Splat and double Splat operators

read input from console in ruby, gets.chomp

how to download ruby on mac

add into array ruby

ruby reduce(:|)

deep copy in ruby

dup method in ruby (shallow copy)

tailwind css ruby bin/dev not working

write a method in ruby to get letter frequency

install subl command

has_one and belongs_to

convert keys to camel case

ruby number generator

rubymine copy path shortcut

rubymin open and close terminal

ruby each_key

divide arrays, partition

rest countries

write this in ruby

enumerable in ruby

bash: ruby: command not found

Ruby: Delegation

Ruby: fetch method with default value

Ruby: Using dig method to retrieve nested values

A server is already running

what would raise this exception

remove ruby using rvm

ruby 1.9 hash syntax

Ruby lowercase

obj file that contain vertex of a normal triangle

active record query log

httpparty debug

ruby to change ruby version

change system ruby

active record logs in console

access nested hash data ruby

ruby faker url

pre_posting_code

Ruby language hello

attribute accessor ruby

ruby 2d to 1d

ruby take elements from array

yield ruby

next array element

unless ruby

POSTGRESS CONFIGURATION

ruby list all variables in scope

how to check ruby version in cmd

ruby swap var

ruby copy var

ruby queue

find out running process id

kill all local hosts

ruby read from stdin

yaml load file in hash ruby

sort ruby hash by key

format ruby hash online

how to sum two numbers in ruby

ruby array inject

array skip first element ruby

Enable CORS (Cross Origin Resource Sharing)

ruby mkdir recursive

convert float to int ruby

if else ruby

ruby call class constant

Ruby comments

ruby array push if not exists

Ruby times downto upto step