Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Newbie ahead!: Ruby-Demo

2 views
Skip to first unread message

Meino Christian Cramer

unread,
Jan 20, 2006, 11:15:19 AM1/20/06
to
Hi,

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


Bill Welch

unread,
Jan 20, 2006, 11:24:51 AM1/20/06
to
Try running the tk demo, widget. It shows GUI screens and has buttons
to show the code that made the examples work.

Bill Welch

unread,
Jan 20, 2006, 11:26:29 AM1/20/06
to
Should have mentioned where the widget is:

C:\ruby\samples\tk\demos-en\widget.rbw

Amr Malik

unread,
Jan 20, 2006, 11:52:15 AM1/20/06
to

You might want to try:

http://tryruby.hobix.com/

HTH,

Amr

--
Posted via http://www.ruby-forum.com/.


Jules

unread,
Jan 20, 2006, 12:01:05 PM1/20/06
to
Show them: blocks, OOP, "the Ruby way".

(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

Matthew Moss

unread,
Jan 20, 2006, 12:27:42 PM1/20/06
to
> people = [Person.new('David'), Person.new('Amy'), Person.new('Paul')]

people = %w{David Amy Paul}.map { |name| Person.new(name) }


Peña, Botp

unread,
Jan 21, 2006, 12:22:54 AM1/21/06
to
Meino Christian Cramer [mailto:Meino....@gmx.de] :

# 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...


cadience

unread,
Jan 21, 2006, 12:14:30 PM1/21/06
to
Here is a class that I just whipped up while I am also learning (just
grabbed the pickaxe book about an hour ago). This code was inspired by
the fib_up_tp(max) function on page 50. While I can't claim it takes
full advantage of Ruby''s power it nonetheless demonstrates many
facets of the language.

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

0 new messages