Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Started with the first four scripts

  • Loading branch information...
commit 6e8c56ced95f477da66dcb30741d9f03bf5d8a77 0 parents
@TomK32 authored
12 README.txt
@@ -0,0 +1,12 @@
+This are a few scripts that I'm using in a course teaching ruby to non-programmers.
+
+If you want to use them as well, feel free to do so and if you think this or that could be
+better, please fork the repository and let me know about your changes.
+
+The scripts are ordered alphabetically, so start with "a-big-hello.rb" and go through.
+
+Authors
+* Thomas R. Koll
+
+License
+* MIT License
46 a-big-hello.rb
@@ -0,0 +1,46 @@
+#!/usr/bin/env ruby
+
+# You can try any of the following lines on its own by starting irb (interactive ruby shell)
+# and execture them.
+# Strings are a bunch of characters
+
+# http://www.ruby-doc.org/core/classes/String.html
+# http://www.ruby-doc.org/core/classes/Array.html
+
+
+puts "Hello World"
+
+puts "Hello World".reverse # => dlroW olleH
+
+# oh, and any line starting with a # is a comment and won't be executed by ruby
+
+puts "Hello World".upcase # => HELLO WORLD
+
+puts "Hello" + "World" # => HelloWorld
+
+text = "Hello World"
+
+puts (text * 3).reverse # => dlroW olleHdlroW olleHdlroW olleH
+
+# want more methods?
+puts text.methods.sort.join(', ') # there are pleny more methods
+
+text['World'] = 'Earthling'
+
+puts text # => Hello Earthling
+
+puts text[0] # => 72 A number, WTF???
+
+puts text[0].chr # => H
+
+puts text[0...4] # => Hell
+
+# an array
+puts ['Hello', 'World'].inspect # => ["Hello", "World"]
+
+# and a hash. notice the :hello, that's a symbol. the look more yellowish in my editor, that's
+# why I love them so much
+myhash = {:hello => 'world', :goodbye => 'moon'}
+puts myhash.inspect # => {:hello=>"world", :goodbye=>"moon"}
+puts myhash[:hello] # => world
+puts myhash['hello'] # => nil (that's nothing)
43 beginning-with-simple-math.rb
@@ -0,0 +1,43 @@
+#!/usr/bin/env ruby
+
+# We'll do some simple maths here, notice a interesting difference in ruby
+# to most programming languages and we will count to ten. thrice
+# http://www.ruby-doc.org/core/classes/Fixnum.html
+# http://www.ruby-doc.org/core/classes/Float.html
+
+puts 2 + 5 # => 7, of course
+
+puts 1.0 / 2 # => 0.5
+
+puts 1 / 2 # => 0, yes zero, null, nothing the great emptiness when everyone has left your party
+
+puts "The class of (1/2) is %s" % (1 / 2).class # => Fixnum
+puts "The class of (1/2.0) is %s" % (1 / 2.0).class # => Float
+
+# there are plenty ways to count to ten
+# all => 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
+puts 1.upto(10).to_a.join(', ')
+puts (1..10).to_a.join(', ')
+puts (1...11).to_a.join(', ')
+
+# and now only the odd ones
+# all => 1, 3, 5, 7, 9
+puts 1.step(10,2).to_a.join(', ')
+puts (1..10).to_a.select{|d| d.odd? }.join(', ')
+puts %w(1 3 5 7 9).join(', ')
+
+# what else you might need?
+puts 13 % 5 # => 3
+puts 13 / 5 # => 2 (not unexpected I hope)
+
+
+# additional methods for a Float
+puts ((1.0).methods - 1.methods).join(', ') # => nan?, infinite?, finite?
+
+# and additional methods for a Fixnum
+puts (1.methods - (1.0).methods).join(', ')
+# => odd?, <<, &, >>, to_sym, ord, next, even?, downto, |, size, ~, times, ^, upto, pred, succ, chr, id2name, []
+
+
+# so there's no "odd" in Float? :(
+puts 5.3.odd # => NoMethodError: undefined method ‘odd’ for 5.3:Float
36 create-me-methods-and-blocks.rb
@@ -0,0 +1,36 @@
+#!/usr/bin/env ruby
+
+# Methods are some code with a name that you can call as often as you want without
+# writing the code over and over and over again
+
+def sanitize(text)
+ text.squeeze
+ text.capitalize!
+ text.gsub!('&', '&amp;')
+ text.gsub!('<', '&lt;')
+ text.gsub!('>', '&gt;')
+ return text
+end
+
+puts "Some <i>HTML</i> in my text"
+
+puts sanitize("Some <i>HTML</i> in my text")
+
+
+# Let's have a look at an alternative, lambda and blocks.
+# They are used as methods but are less formal, they don't have a name and you often
+# don't use them at other places.
+%w(1 2 3 4 5).each{|d| puts "%d is %s" % [d, d.to_i.even? ? 'even' : 'odd'] }
+
+grades = {"tom" => 5, "lisa" => 1, "bart" => 6, "jerry" => 2}
+
+puts (grades.select{|student, grade| grade < 3}).inspect # => [[:lisa, 1], [:jerry, 2]]
+grades.each do |student, grade|
+ if grade > 4
+ puts student + " has to work harder"
+ elsif grade > 1
+ puts student + " has shown great potential"
+ elsif grade == 1
+ puts student + " has received a power-up"
+ end
+end
42 defining-classes.rb
@@ -0,0 +1,42 @@
+#!/usr/bin/env ruby
+
+# A class is a wireframe with methods of which you can instanciate single objects
+# About everything in ruby is a Class or object, we've already used them and now
+# it's time to write our own.
+
+class Human
+ attr_accessor :size, :weight, :sex
+ def initialize(sex, size, weight)
+ @self = sex
+ @size = size
+ @weight = weight
+ end
+ def male?
+ @sex == :male
+ end
+ def female?
+ @sex == :female
+ end
+ def other?
+ @sex != :female && @sex != :male
+ end
+ def height_in_inch
+ @size * 0.393700787 if @size
+ end
+ def <(other)
+ return @size < other.size
+ end
+ def >(other)
+ ! this < (other)
+ end
+end
+
+tom = Human.new(:male, 193, 98)
+jerry = Human.new(:male, 185, 75)
+
+puts tom.inspect
+
+puts "Tom's height of %scm is %s in inch" % [tom.size, tom.height_in_inch]
+puts "Tom's %s a girl" % (tom.female? ? '' : 'not')
+
+puts "Tom is %s than Jerry" % (jerry < tom ? 'taller' : 'smaller')
Please sign in to comment.
Something went wrong with that request. Please try again.