Learning from
Ruby
Kang-min Liu <gugod@gugod.org>
YAPC::Asia::2009
Gugod->meta
•                              •   http://handlino.com/
    Kang-min Liu
    gugod


•   http://gugod.org


•   http://search.cpan.org/
    ~gugod/


•   http://twitter.com/gugod


•   gugod@gugod.org
Learning from
Ruby
Learning from
Ruby
Ruby is Lovely
(The programming language)
Perl 6 Logo
Language
rubyisms.pm
by Simon Cozens
rubyisms.pm

• Class
• new
• self
• super
• yield
Perl ~~ Ruby
Ruby
self
class Foo
 def bar
   self.baz
 end

 def self.bar
  self.baz
 end
end
class Foo
        def bar
object
          self.baz
        end

       def self.bar
        self.baz
       end
      end
class Foo
         def bar
 object
           self.baz
         end

Class    def self.bar
          self.baz
         end
        end
class Foo
 def bar
   baz
 end

 def self.bar
  baz
 end
end
block syntax
def foo(arg1, arg2)
 yield(a, b)
end



foo(arg1, arg2) do |block_arg1, block_arg2|
 ...
end
co-routine
callback
call me after you
done that.
div(id => "content") do
 h1(id => "title") { "OHAI" }
 p(class => "block") { "Cheeze?" }
end
app do
 alert(42)
end
Good Built-in
Classes
Range
Lazy
irb> 1..2**100
=>
1..1267650600228229401496703205376
irb> (1..2**100).class
=> Range
(1..2**100).each do |i|
 puts 42
 break if i >= 5
end
autobox
no Semicolon
Rubyish.pm
It’s all sugar, honey.
Rubyish.pm

• Kernel, Object, Module, Class
• Array, Hash, String, TrueClass,
  NilClass, Enumerable

• Syntax
 • def, nil, true, false
class Foo {

}
def foo {
  # $self;
}
def foo($arg1, $arg2) {

}
see also:
MooseX::Declare
my $array = Array[0..3];
if ($array->any { $_ > 2 }) {
    ...
}
# Rubyish::Array
$arr = Array[1..10];

# Rubyish::Hash
$hash = Hash{ a => 42, b => "stuff" };

# Rubyish::String
$str = String("FooBar");
ref:
Class::Builtin
boolean

• true – TrueClass
• false - FalseClass
• nli - NilClass
sub true() {
  Rubyish::TrueClass->new
}
ref:
boolean.pm
and Acme::Boolean
Hash is lazy too
fib = Hash.new { |fib, k| fib[k] = fib[k-1] + fib[k-2] }
fib[0] = 0
fib[1] = 1

puts fib[10]
# => 55
Functional
programming
-ish
Memoize

   sub fib {
     my $n = shift;
     return $n if $n < 2;
     fib($n-1) + fib($n-2);
   }
   memoize('fib');
Lazy stuffs

• Scalar::Lazy
• Variable::Lazy
• Scalar::Defer
• Data::Thunk
Lazy stuffs

• Tie::Array::Lazy
• Tie::Array::Lazier
• Hash + Lazy = ?
Hash::Lazy
Hash::Lazy
  my $fib = Hash {
    my ($h, $k)= @_;
    return $h->{$k-1} + $h->{$k-2}
  };
  $fib->{0} = 0;
  $fib->{1} = 1;

  say $fib->{10}; # 55
Hash::Lazy


• export “Hash” as the constructor
• Hash::Lazy::Tie
Learnt
• I hate doing tie
• Spiffy constructors can be good
  read

 • Hash
 • Rubyish.pm: Array, Hash, String
 • Class::Builtin: OO
PerlX::MethodCall
WithBlock
Still more syntax sugar
Schwartzian
Transform
        Ruby

     (0..10)
     .map { |i| i * 13 }
     .map { |i| i % 17 }
     .sort { |a,b| a <=> b }
Schwartzian
Transform
        Perl5

     sort { $a <=> $b }
     map { $_ % 17 }
     map { $_ * 13 }
     (0..10)
Schwartzian
Transform autobox
     Perl5, with

     [0..10]
     ->map(sub { $_ * 13 })
     ->map(sub { $_ % 17 })
     ->sort(sub { $a <=> $b })
Schwartzian
Transform
     Perl5, extended!


       [0..10]->map {
          $_ * 13
       }->map {
          $_ % 17
       }->sort {
          my ($a,$b) = @_;
          $a <=> $b
       };
Schwartzian
Transform
     Perl5, extended!

       [0..10]
       ->map { $_ * 13 }
       ->map { $_ % 17 }
       ->sort { $_[0] <=> $_[1] }
PerlX ::
MethodCallWithBlo
ck
ref

• B::OPCheck
 • lineseq, pushmark, const
• B::Hooks::EndOfScope
• Devel::Declare
ref

• opcode.pl, opcode.h
• B::Concise
• B::Deparse
PerlX
Community
Last month
Sh*t happened
Taiwan
Morakot
☹
$WebSite.com
xdite
☹
DisasterTW.com
DisasterTW.com


• Forum
• Initial Coding: 1 hour*woman.
1 day later
DisasterTW.com

• Web 2.0 / Social-ish
• Multi-Function
• Mash-able
DisasterTW.com




         ✖
• Web 2.0 / Social-ish
• Multi-Function
• Mash-able
DisasterTW.com

• Online ASAP
• Robust (peak: 400k PV/day)
• Well-known
DisasterTW.com

• Rapid Prototyping
• Cloud Computing
• SEO / Social Network Friendly
DisasterTW.com

• Ruby on Rails
• Heroku + Panther CDN
• Good HTML / Plurk button
DisasterTW.com

• 6 heroku instances
• Thousands of tweets about
  #morakot

• Total Cost: ~50 USD
People are
awesome
Tools are
awesome
Problem →
What if it was a
Jifty site?
@obra can haz
dancing
@obra can haz
dancing, naked
DisasterTW.com

• Rapid Prototyping
• Cloud Computing
• SEO / Social Network Friendly
DisasterTW.com

• Jifty
• Cloud Computing
• Good HTML / Plurk button
Heroku for Perl?
∴
Ruby is Lovely
Ruby is a
respectful friend
of Perl
Ruby programmers
are respectful friends
of Perl programmers
Thanks for Listening

Learning From Ruby (Yapc Asia)

Editor's Notes

  • #5 Before we learn what we&amp;#x2019;re learning, let&amp;#x2019;s learn it first.
  • #7 If you google &amp;#x201C;Perl 6 Logo&amp;#x201D;, you get this picture. Ruby is so awesome that people once think that we don&amp;#x2019;t need Perl 6 anymore.
  • #8 I want to start by telling you my learnings from the language aspect
  • #9 by Simon Cozens.
  • #11 After seeing rubyisms, I asked my self a question:
  • #12 Can Perl programs have the same look and feel of Ruby programs?
  • #13 There are several good parts of Ruby that I like...
  • #14 The self keyword &amp;#x2013; It doesn&amp;#x2019;t always mean &amp;#x201C;the current object&amp;#x201D; like in java.
  • #17 Those baz in there refers to different baz method.
  • #19 The &amp;#x2018;yield&amp;#x2019; there means to invoke call the given block as a method.
  • #23 One good use: Markaby
  • #24 Another one Hot cocoa.
  • #30 Ruby primitive data types are all Objects
  • #31 It requires the statements to be written nicely, but it&amp;#x2019;s nice not having to write semicolons sometimes.
  • #34 Now you can define a class with keyword &amp;#x201C;class&amp;#x201D;
  • #35 and define instance methods with keyword &amp;#x201C;def&amp;#x201D;
  • #36 plus function prototype
  • #37 See also that module
  • #38 Export &amp;#x201C;Array&amp;#x201D; method as the constructor of Rubyish::Array.
  • #41 boolean keywords returns the singleton object of their classes
  • #44 Ruby hash has a lovely way to lazily initialize its content.
  • #47 We have Memoize instead of being Lazy. But it doesn&amp;#x2019;t read nicely. And it doesn&amp;#x2019;t happen automagically.
  • #51 You might notice that the &amp;#x201C;Hash&amp;#x201D; returns a hash ref but not a hash. This is required because the it&amp;#x2019;s a reference to a tied hash. So the magic under the table, is to use tie.
  • #63 Let&amp;#x2019;s write more crazy stuffs for this namespace.
  • #79 Hey! It&amp;#x2019;s a Web-two-point-o-ey resource allocation exchange website!
  • #81 Volunteer added good styles
  • #82 Being a website that helps people in such a tragedy, the website should...
  • #83 The whole points for such website
  • #87 Now it&amp;#x2019;s over and the website basically shutdown... I looked back and thought..
  • #90 They see a problem/requirement and they uses the technologies they familiar with.
  • #96 Heroku is so convenient and cheap for RoR deployment. This makes me, as a Perl programmer for year, really jealous.
  • #97 So by the end of this talk, I conclude..