1 / 11

Learning Ruby

Let's Learn Ruby AQAP As Quickly As Possible!. Learning Ruby. Running Ruby Code. Run the interactive ruby shell – irb Suggest using Netbeans – download ALL. 2 + 6 22 / 4  5 Float(22)/4 22.0 / 4 3 * 3 3 ** 3

deepak
Download Presentation

Learning Ruby

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. Let's Learn Ruby AQAP As Quickly As Possible! Learning Ruby

  2. Running Ruby Code • Run the interactive ruby shell – irb • Suggest using Netbeans – download ALL

  3. 2 + 6 22 / 4  5 Float(22)/4 22.0 / 4 3 * 3 3 ** 3 big_number = 1_000_000_000 (underscores not required and can go anywhere allowed to make it more readable – commas hard to see) Simple Ruby Number Objects

  4. Math.sqrt( 9 ) Math.methods 9.methods  will tell you what methods can be used with 9 27.class  will tell you what class 27 belongs to (Fixnum) 3.times { print "Ho! " } 1.upto( 5 ) { |i| puts i }  defines i as the iterator variable termed a “code block” Anonymous delegate given the name i Ruby Number Methods

  5. one = "1" two = "2" one + two  string concat, right? Integer( one ) + Integer( two )  casts and then adds 12.to_s  calls to string method, but wants you to request conversion print "Happy ", 12.to_s, "th Birthday!" puts "Happy ", 12.to_s, "th Birthday!" puts "Happy " + 12 + "th Birthday!" Number Conversions

  6. a1 = "this is a string\n”  evaluate backslash and contents of #{} inside of the string. Termed string interpolation a2 = 'and so is this\n‘  will see the \n as no evaluation is done print a1 print a2 answer = “joy” puts "The meaning of life is #{answer}" puts "There's #{24*60*60} seconds in a day" Ruby String Objects

  7. formatted_text = <<END_OF_TEXT Let us turn our thoughts today To Martin Luther King And recognize that there are ties between us All men and women Living on the Earth Ties of hope and love Sister and brotherhood That we are bound together In our desire to see the world become A place in which our children Can grow free and strong END_OF_TEXT  any delimiter – as long as it matches print formatted_text  interpretation does work Ruby Here Documents

  8. String.methods.sort sorted list of String methods formatted_text.size number of characters formatted_text.downcase lowercase formatted_text.upcase uppercase formatted_text.capitalize!  ! means to actually change the object in place (rather than return a changed copy) formatted_text.reverse reverses all characters formatted_text.empty?  boolean indicated by ? only used in method names (not variables) Ruby String Methods

  9. song_lines = formatted_text.to_a to array (1..10).to_a [1,2,3,4,5,6,7,8,9,10] from a range (denoted ..) (‘a’..’e’).to_a [“a”, “b”, “c”, “d”, “e”] print song_lines[1] print song_lines[5] print song_lines[99]  Gives nil if used when out of range, will add to array if assign outside of range. print song_lines print formatted_text song_lines.class Array formatted_text.class String Working with Ruby Strings

  10. Same variable can hold any type at different times… a = 42 puts a a = "The Ruby Programming Language" puts a a = 1.8 puts a What About Variable Type?

  11. Ruby So Far • Everything - numbers, strings, data structures, Class itself etc. - is an object • Methods are invoked using the dot (".") notation • Variables are dynamically created as needed (problems with typos), but will tell you during execution if you access something without a value. • The traditional notion of a variable's "type" is not something the Ruby programmer concerns themselves with (too much)

More Related