Learning From Ruby (Yapc Asia)
Upcoming SlideShare
Loading in...5
×
 

Learning From Ruby (Yapc Asia)

on

  • 4,358 views

Introducing several of my Perl5 hacks of bring some Ruby features into Perl5.

Introducing several of my Perl5 hacks of bring some Ruby features into Perl5.

Statistics

Views

Total Views
4,358
Views on SlideShare
4,344
Embed Views
14

Actions

Likes
4
Downloads
51
Comments
1

5 Embeds 14

http://coderwall.com 6
http://www.slideshare.net 4
http://jackeystaffy.blogspot.com 2
https://lti-examples.heroku.com 1
http://www.linkedin.com 1

Accessibility

Upload Details

Uploaded via as Apple Keynote

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

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

Learning From Ruby (Yapc Asia) Learning From Ruby (Yapc Asia) Presentation Transcript

  • 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