can someone point me to a ruby demo-script?
I want to show a friend the beauty of ruby and am myself fear if
would try my best would acchieve the opposite of what I want.
It dont need to do anything sophisticated it only should show the
Pros of programming in ruby...
Thanks a lot in advance for any help!
Ruby!
mcc
C:\ruby\samples\tk\demos-en\widget.rbw
(0..10).map{|x| x*x}.select{|x| x < 50}
Take a Java program and translate it to ruby step-by-step by stripping
things ;-)
-----
class Person
def initialize(newName)
@name = newName
end
def getName()
return(@name);
end
def setName(newName)
@name = newName;
end
end
=>
class Person
def initialize(new_name)
@name = new_name
end
def name
@name
end
def name=(new_name)
@name = new_name
end
end
=>
class Person
attr_accessor :name
end
=>
Person = Struct.new(:name)
..
people = [Person.new('David'), Person.new('Amy'), Person.new('Paul')]
people.sort_by{|p| p.name}
..
# Struct.new returns a class, so we can subclass it :)
class Person < Struct.new(:name)
include Comparable
def <=> other
@name <=> other.name
end
end
people = [Person.new('David'), Person.new('Amy'), Person.new('Paul')]
# A person has comparison methods now. > == < <= >= etc. can be used
now.
# The people Array has includes?, max, min etc.
# Everything just by adding include Comparable and def <=>.
if people[0] < people[1] && people.includes? Person.new('David')
people.sort!
end
-----
All untested.
Jules
people = %w{David Amy Paul}.map { |name| Person.new(name) }
# can someone point me to a ruby demo-script?
#
# I want to show a friend the beauty of ruby and am myself fear if
# would try my best would acchieve the opposite of what I want.
#
# It dont need to do anything sophisticated it only should show the
# Pros of programming in ruby...
Show "Hello, world." in 3 ways.
Way 1. print hello world on screen.
code: puts "Hello, world."
audience comment: hmmmm.... ok..
Way 2. print helloworld 100 times
codeA. puts "Hello, world.\n" * 100
or
codeB. 100.times{ puts "Hello, world."}
audience comment: coool..l :-)
Way 3. print helloworld to pdf.
code: require "pdf/writer"
pdf = PDF::Writer.new
pdf.select_font "Times-Roman"
100.times {pdf.text "Hello, world."}
pdf.save_as("hello.pdf")
audience comment: ..cannot comment... audience rotfl...
Imagine doing this in C! Even C++ this would be less "clean".
If anyone has any suggestions on taking more advantage of Ruby in the
code below, let me know! Of course, Fibs should inherit an array, and
so fourth (hey, I'm only on page fifty :-p ) , but I believe this gives
a great first look at the power of Ruby's semantics to someone curious
about Ruby.
class Fibs
@foundFibs
def initialize
@foundFibs = Array.new(2, 1)
end
def lastIndex
@foundFibs.size - 1
end
def nextFib
@foundFibs.push(@foundFibs[-2] + @foundFibs[-1])
self.largestFib
end
def largestFib
@foundFibs.last
end
def up_to_num(max)
self.nextFib while @foundFibs.last < max
@foundFibs.find_all{|x| x <= max}
end
def up_to_index(index)
(self.lastIndex + 1).upto(index) {self.nextFib} if index >
self.lastIndex
@foundFibs.first(index + 1)
end
def to_s
s = "["
@foundFibs.each {|name| s += name.to_s + ", "}
s[0..s.size-3] + "]"
end
def length
@foundFibs.length
end
def [](index)
self.up_to_index(index) if index > self.lastIndex
@foundFibs[index]
end
end