Newbie ahead!: Ruby-Demo

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

Meino Christian Cramer wrote:

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

You might want to try:

http://tryruby.hobix.com/

HTH,

Amr

···

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

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 :wink:

···

-----

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 :slight_smile:

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

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

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

people = [Person.new('David'), Person.new('Amy'), Person.new('Paul')]

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