Learning Ruby Programming Fundamentals

slide1 n.w
1 / 32
Embed
Share

Discover the basics of Ruby programming language including variables, data types, operators, and more in this comprehensive guide. Explore examples illustrating concepts such as local variables, conversions, constants, and more to enhance your understanding of Ruby programming.

  • Ruby
  • Programming
  • Variables
  • Constants
  • Conversions

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Ruby

  2. IRB(Interactive Ruby) irb(main):001:0> irb(main):001:0> 1 + 1 2 puts "Hello, World!!" $ ruby hello.rb

  3. /* PHP */ $i = 1; echo "Value is " + $i # 1 # Ruby i=1 puts "Value is " + i #TypeError: can't convert Fixnum into String # from (irb):2:in `+' # from (irb):2 /* C */ int a = 5; float b = a;

  4. Integer Float 5 -205 9999999999 0 54.321 0.001 -12.312 0.0

  5. puts 1.0 + 2.0 puts 2.0 * 3.0 puts 5.0 - 8.0 puts 9.0 / 2.0 puts 1 + 2 puts 2 * 3 puts 5 - 8 puts 9 / 2 # 3.0 # 6.0 # -3.0 # 4.5 # 3 # 6 # -3 # 4 puts 5 * (12-8) + -15 puts 98 + (59872 / (13*8)) * -52

  6. String puts 'Hello, world!' puts '' puts 'Good-bye.' var1 = 'stop' var2 = 'foobar' var3 = "aAbBcC" puts 'I like ' + 'apple pie.' puts 'You\'re smart!' puts var1.reverse # 'pots' puts var2.length # 6 puts var3.upcase puts var3.downcase puts '12' + 12 #<TypeError: can't convert Fixnum into String>

  7. Ruby # "UPPER" puts "upper".upcase # -5 puts -5.abs # Fixnum puts 99.class # "Ruby Rocks!" 5.times do puts "Ruby Rocks!" end

  8. Local Variable composer = 'Mozart' puts composer + ' was "da bomb", in his day.' my_composer = 'Beethoven' puts 'But I prefer ' + my_composer + ', personally.'

  9. Conversions var1 = 2 var2 = '5' puts var1.to_s + var2 # 25 puts var1 + var2.to_i # 7 puts 9.to_f / 2 # 4.5

  10. Constant Foo = 1 Foo = 2 # (irb):3: warning: already initialized constant Foo RUBY_PLATFORM # => "x86_64-darwin10.7.0" ENV # => { "PATH" => "....", "LC_ALL" => "zh_TW.UTF-8" }

  11. nil nil # nil nil.class # NilClass nil.nil? # true 42.nil? # false nil == nil # true false == nil # false

  12. Comment # this is a comment line # this is a comment line =begin This is a comment line This is a comment line =end

  13. Array colors = ["red", "blue"] a = [ 1, "cat", 3.14 ] colors.push("black") colors << "white" puts colors.join(", ") # red, blue, black, white puts a[0] # 1 puts a.size # 3 a[2] = nil puts a.inspect # [1, "cat", nil] a[99] # nil colors.pop puts colors.last #black

  14. Hash config = { "foo" => 123, "bar" => 456 } puts config["foo"] # 123 config["nothing"] # nil config = { foo: 123, bar: 456 } # { :foo => 123, :bar => 456 }

  15. Symbols config = { :foo => 123, :bar => 456 } puts config[:foo] # 123 "foobar".object_id # 2151854740 "foobar".object_id # 2151830100 :foobar.object_id # 577768 :foobar.object_id # 577768

  16. puts 1 > 2 # puts 1 < 2 # puts 5 >= 5 # puts 5 <= 4 # puts 1 == 1 # puts 2 != 1 # puts ( 2 > 1 ) && ( 2 > 3 ) # puts ( 2 > 1 ) || ( 2 > 3 ) #

  17. If if account.total > 100000 puts "large account" elsif account.total > 25000 puts "medium account" else puts "small account" End puts "greater than ten" if total > 10

  18. x = 3 if x > 3 y = "foo" else y = "bar" End x = 3 y = ( x > 3 )? "foo" : "bar"

  19. Case case name when "John" puts "Howdy John!" when "Ryan" puts "Whatz up Ryan!" else puts "Hi #{name}!" end

  20. # while i=0 while ( i < 10 ) i += 1 next if i % 2 == 0 # End # loop i = 0 loop do i += 1 break if i > 10 # end # until i = 0 i += 1 until i > 10 puts i # 11

  21. True or False puts "not execute" if nil puts "not execute" if false puts "execute" if true # execute puts "execute" if # execute ( JavaScript ) puts "execute" if 0 # execute ( C ) puts "execute" if 1 # execute puts "execute" if "foo" # execute puts "execute" if Array.new # execute

  22. Methods def say_hello(name) result = "Hi, " + name return result end puts say_hello('ihower') # Hi, ihower def say_hello(name) "Hi, " + name end say_hello 'ihower'

  23. ?! array=[2,1,3] array.empty? # false array.sort # [1,2,3] array.inspect # [2,1,3] array.sort! # [1,2,3] array.inspect # [1,2,3]

  24. Classes color_string = String.new color_string = "" # color_array = Array.new color_array = [] # color_hash = Hash.new color_hash = {} # time = Time.new # puts time

  25. Classes class Person # p1 = Person.new("ihower") p2 = Person.new("ihover") def initialize(name) # @name = name # end p1.say("Hello") # Hello, ihower p2.say("Hello") # Hello, ihover def say(word) puts "#{word}, #{@name}" # end end

  26. class MyClass def public_method end private def private_method end protected def protected_method end end

  27. Class class Pet class Dog < Pet attr_accessor :name, :age def say(word, person) puts "Bark at #{person}!" def say(word) super(word) puts "Say: #{word}" end end end end Cat.new.say("Hi") class Cat < Pet Dog.new.say("Hi", "ihower") def say(word) puts "Meow~" super Meow~ end Say: Hi end Bark at ihower! Say: Hi

  28. Iterator # 1.upto(9) { |x| puts x } languages = ['Ruby', 'Javascript', 'Perl'] languages.each do |lang| puts "I love #{lang}!" end # languages = ['Ruby', 'Javascript', 'Perl'] languages.each_with_index do |lang, i| puts "#{i}, I love #{lang}!" end # 0, I Love Ruby # 1, I Love Javascript # 2, I Love Perl # I Love Ruby # I Love Javascript # I Love Perl # 3.times do puts 'Good Job!' end # Good Job! # Good Job! 3.times { puts "Hello" } # Good Job!

  29. Iterator # [2,1,3].sort! { |a, b| b <=> a } # [3, 2, 1] # a = [ "a", "b", "c", "d" ] b = a.map {|x| x + "!" } puts b.inspect # ["a!", "b!", "c!", "d!"] # (5..10).inject {|sum, n| sum + n } # b = [1,2,3].find_all{ |x| x % 2 == 0 } # find the longest word longest = ["cat", "sheep", "bear"].inject do |memo,word| ( memo.length > word.length )? memo : word end b.inspect # [2] # a = [51, 101, 256] a.delete_if {|x| x >= 100 } # [51]

  30. Iterator file = File.new("testfile", "r") # ... file.close File.open("testfile", "r") do |file| # ... end #

  31. Yield # def call_block puts "Start" yield yield puts "End" end call_block { puts "Blocks are cool!" } # # "Start" # "Blocks are cool!" # "Blocks are cool!" # "End"

  32. Yield def call_block yield(1) yield(2) yield(3) end call_block { |i| puts "#{i}: Blocks are cool!" } # # "1: Blocks are cool!" # "2: Blocks are cool!" # "3: Blocks are cool!"

More Related Content