1 / 13

Basic Ruby Syntax

This lecture note provides a comprehensive overview of Ruby syntax, covering essential topics such as variable declarations, method definitions, and control structures including loops and conditionals. Learn how to sum the squares of numbers using a while loop and understand the usage of blocks and iterators. The notes also touch on string handling with single and double quotes, keyword arguments, and class/module mechanisms. Perfect for CS 142 students, these notes serve as a valuable resource for grasping Ruby programming concepts.

metta
Télécharger la présentation

Basic Ruby Syntax

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*i i = i + 1 end puts "Sum of squares is #{sum}\n" Newline is statement separator do ... end instead of { ... } Optional parentheses in method invocation Substitution instring value CS 142 Lecture Notes: Ruby

  2. Ruby String Syntax • Single quotes (only \' and \\) 'Bill\'s "personal" book' • Double quotes (many escape sequences) "Found #{count} errors\nAborting job\n" • %q (similar to single quotes) %q<Nesting works: <b>Hello</b>> • %Q (similar to double quotes) %Q|She said "#{greeting}"\n| • Here documents <<END First line Second line END @ExploreNY

  3. Variable Names and Scopes foo Local variable $foo Global variable @foo Instance variable in object @@foo Class variable MAX_USERS “Constant” (by convention) CS 142 Lecture Notes: Ruby

  4. Ruby Statements if x < 10 then ... elsif x < 20 ... else ... end while x < 10 do ... end array = [14, 22, 34, 46, 92] for value in array do ... end CS 142 Lecture Notes: Ruby

  5. Factorial deffac(x) if x <= 0 then return 1 end return x*fac(x-1) end CS 142 Lecture Notes: Ruby

  6. Arguments: Defaults, Variable # definc(value, amount=1) value+amount end def max(first, *rest) max = first for x in rest do if (x > max) then max = x end end return max end CS 142 Lecture Notes: Ruby

  7. Keyword Arguments defcreate_widget(size, properties) ... end create_widget(6, {:id => "table22", :class => "Cart"}) create_widget(6, :id => "table22", :class => "Cart") create_widget(6, id: "table22", class: "Cart") CS 142 Lecture Notes: Ruby

  8. Blocks, Iterators, Yield oddNumbers(3) do |i| print(i, "\n") end defoddNumbers(count) number = 1 while count > 0 do yield(number) number += 2 count -= 1 end end Block: code passedto method Invoke method’s block CS 142 Lecture Notes: Ruby

  9. Another Block/Iterator Example defsumOdd(count) sum = 0 oddNumbers(count) do |i| sum += i end return sum end defoddNumbers(count) number = 1 while count > 0 do yield(number) number += 2 count -= 1 end end CS 142 Lecture Notes: Ruby

  10. Equivalent Code array = [14, 22, 34, 46, 92] for value in array do print(value, "\n") end array = [14, 22, 34, 46, 92]; array.each do |value| print(value, "\n") end CS 142 Lecture Notes: Ruby

  11. Simple Class class Point def initialize(x, y) @x = x @y = y end def x @x end def x=(value) @x = value end end p = Point.new(3,4) puts "p.x is #{p.x}" p.x= 44 CS 142 Lecture Notes: Ruby

  12. Module Example New methods available in MyClass: min, max, sort, map, select, ... class MyClass include Enumerable ... def each ... end end CS 142 Lecture Notes: Ruby

  13. CS 140 Lecture Notes: File Systems

More Related