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
Iteration AryList = [5,6,7,8] SndList = Array.new SndList = AryList.collect puts b Everything has a value x = 88 y = 11 z = if x < y true else false end Symbols are not lightweight Strings :marzsexy.object_id == :marzsexy.object_id Everything is an Object marielouClass = Class.new do attr_accessor :instance_var end Variable Constants M_NUM = 58 Naming conventions http://itsignals.cascadia.com.au/?p=7 -Instance variables @cust_id=id @cust_name=name @cust_addr=addr -global variables $global_variable = 10 Keyword arguments http://brainspec.com/blog/2012/10/08/keyword-arguments-ruby-2-0/ def foo(x, str: "foo", num: 424242) [x, str, num] end foo(13) #=> [13, 'foo', 424242] foo(13, str: 'bar') #=> [13, 'bar', 424242] The universal truth if 0 puts "0 is true" else puts "0 is false" end Prints “0 is true” Access modifiers apply until the end of scope http://www.tutorialspoint.com/ruby/ruby_classes.htm class Sample def hello puts "Hello Ruby!" end end Method access http://www.tutorialspoint.com/ruby/ruby_classes.htm class Customer @@no_of_customers=0 def initialize(id, name, addr) @cust_id=id @cust_name=name @cust_addr=addr end end Classes are open http://rubylearning.com/satishtalim/ruby_open_classes.html 1. class Dog 2. def big_bark 3. puts 'Woof! Woof!' 4. end 5. end 6. # make an object 7. d = Dog.new('Labrador', 'Benzy') 8. d.bark 9. d.big_bark 10. d.display Funny method names http://rubylearning.com/satishtalim/ruby_names.html 1. rice_on_square = 1 2. 64.times do |square| 3. puts "On square #{square + 1} are #{rice_on_square} grain(s)" 4. rice_on_square *= 2 5. end Singleton method http://rubymonk.com/learning/books/4-ruby-primer-ascent/chapters/39-ruby-sobject-model/lessons/131-singleton-methods-and-metaclasses class Foo end foo=Foo.new def foo.shout puts "Foo Foo Foo!" end foo.shout p Foo.new.respond_to?(:shout) Missing methods http://rubylearning.com/satishtalim/ruby_method_missing.html 1. class Dummy 2. def method_missing(m, *args, &block) 3. puts "There's no method called #{m} here -- please try again." 4. end 5. end 6. Dummy.new.anything Message passing, not function calls # This 3 + 9 # Is the same as this ... 3.+(9) # Which is the same as this: 3.send "+", 9 Blocks are Objects, they just don’t know it yet http://www.tutorialspoint.com/ruby/ruby_blocks.htm def test(&block) block.call end test { puts "Hello World!"} Operators are syntactic sugar http://rubymonk.com/learning/books/1-ruby-primer/chapters/6objects/lessons/37-syntactic-sugar-for-special-methods words = ["foo", "bar", "baz"] words[1]