Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Perry Cabugao 11260564 Seatwork #9 Programmatic examples: If else statement: User1 = 90 User2 = 95 If User1 > User2 Puts “User 1 is better than User2” Else Puts “User 2 is better the User1” Iteration and Loops X=0 While x < 10 Print “#{i}” X+=1 End Other way of printing 1-10 Printing by ones 1.upto(10) {|i| print “#{i}”} Printing by twos 1.step(10, 2) {|i| print “#{i}”} Declaring Constants x1_CONST = “Perry The Plattypus” x2_CONST = x1_CONST Puts x1_CONST this will display Perry The Plattypus Puts x2_CONST this will display the value of x1_CONST which is Perry The Plattypus Declaring Variables ($)x = “Pers” this is for global values class Polygon @sides = 6 this is for instance variables End Class Polygon @@sides = 6 this is for class variables Def self.sides @@sides End End Keyword Arguments def foo(bar: 'default') puts bar end foo # => 'default' foo(bar: 'baz') # => 'baz' https://robots.thoughtbot.com/ruby-2-keyword-arguments Universal Truth # in Ruby if 10 puts "0 is true" else puts "0 is false" end Prints “0 is true”. Method Access class Person def initialize(age) @age = age end def age @age end def compare_age(c) if c.age > age "The other object's age is bigger." else "The other object's age is the same or smaller." end end protected :age end chris = Person.new(25) marcos = Person.new(34) puts chris.compare_age(marcos) #puts chris.age http://rubylearning.com/satishtalim/ruby_access_control.html Blocks are Objects, 10.times do |i| print "#{i} " end numbers = [1, 2, 5, 6, 9, 21] numbers.each do |x| puts "#{x} is " + (x >= 3 ? "many" : "few") end squares = numbers.map {|x| x * x} Operators are syntactic sugar class NewDog def initialize(breed, name) @breed = breed @name = name end attr_reader :breed, :name # create reader only end nd = NewDog.new('Doberman', 'Benzy') puts nd.name http://rubylearning.com/satishtalim/ruby_syntactic_sugar.html