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

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Learning From Ruby (Yapc Asia)

2,622
views

Published on

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.

Published in: Technology, Education

1 Comment
5 Likes
Statistics
Notes
No Downloads
Views
Total Views
2,622
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
53
Comments
1
Likes
5
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Before we learn what we’re learning, let’s learn it first.
  • If you google “Perl 6 Logo”, you get this picture.
    Ruby is so awesome that people once think that we don’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 – It doesn’t always mean “the current object” like in java.
  • Those baz in there refers to different baz method.
  • The ‘yield’ 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’s nice not having to write semicolons sometimes.
  • Now you can define a class with keyword “class”
  • and define instance methods with keyword “def”
  • plus function prototype
  • See also that module
  • Export “Array” 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’t read nicely.

    And it doesn’t happen automagically.
  • You might notice that the “Hash” returns a hash ref but not a hash.
    This is required because the it’s a reference to a tied hash. So the magic
    under the table, is to use tie.
  • Let’s write more crazy stuffs for this namespace.
  • Hey! It’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’s over and the website basically shutdown...

    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..
  • Transcript

    • 1. Learning from Ruby Kang-min Liu <gugod@gugod.org> YAPC::Asia::2009
    • 2. Gugod->meta • • http://handlino.com/ Kang-min Liu gugod • http://gugod.org • http://search.cpan.org/ ~gugod/ • http://twitter.com/gugod • gugod@gugod.org
    • 3. Learning from Ruby
    • 4. Learning from Ruby
    • 5. Ruby is Lovely (The programming language)
    • 6. Perl 6 Logo
    • 7. Language
    • 8. rubyisms.pm by Simon Cozens
    • 9. rubyisms.pm • Class • new • self • super • yield
    • 10. Perl ~~ Ruby
    • 11. Ruby
    • 12. self
    • 13. class Foo def bar self.baz end def self.bar self.baz end end
    • 14. class Foo def bar object self.baz end def self.bar self.baz end end
    • 15. class Foo def bar object self.baz end Class def self.bar self.baz end end
    • 16. class Foo def bar baz end def self.bar baz end end
    • 17. block syntax
    • 18. def foo(arg1, arg2) yield(a, b) end foo(arg1, arg2) do |block_arg1, block_arg2| ... end
    • 19. co-routine
    • 20. callback
    • 21. call me after you done that.
    • 22. div(id => "content") do h1(id => "title") { "OHAI" } p(class => "block") { "Cheeze?" } end
    • 23. app do alert(42) end
    • 24. Good Built-in Classes
    • 25. Range
    • 26. Lazy
    • 27. irb> 1..2**100 => 1..1267650600228229401496703205376 irb> (1..2**100).class => Range
    • 28. (1..2**100).each do |i| puts 42 break if i >= 5 end
    • 29. autobox
    • 30. no Semicolon
    • 31. Rubyish.pm It’s all sugar, honey.
    • 32. Rubyish.pm • Kernel, Object, Module, Class • Array, Hash, String, TrueClass, NilClass, Enumerable • Syntax • def, nil, true, false
    • 33. class Foo { }
    • 34. def foo { # $self; }
    • 35. def foo($arg1, $arg2) { }
    • 36. see also: MooseX::Declare
    • 37. my $array = Array[0..3]; if ($array->any { $_ > 2 }) { ... }
    • 38. # Rubyish::Array $arr = Array[1..10]; # Rubyish::Hash $hash = Hash{ a => 42, b => "stuff" }; # Rubyish::String $str = String("FooBar");
    • 39. ref: Class::Builtin
    • 40. boolean • true – TrueClass • false - FalseClass • nli - NilClass
    • 41. sub true() { Rubyish::TrueClass->new }
    • 42. ref: boolean.pm and Acme::Boolean
    • 43. Hash is lazy too
    • 44. fib = Hash.new { |fib, k| fib[k] = fib[k-1] + fib[k-2] } fib[0] = 0 fib[1] = 1 puts fib[10] # => 55
    • 45. Functional programming -ish
    • 46. Memoize sub fib { my $n = shift; return $n if $n < 2; fib($n-1) + fib($n-2); } memoize('fib');
    • 47. Lazy stuffs • Scalar::Lazy • Variable::Lazy • Scalar::Defer • Data::Thunk
    • 48. Lazy stuffs • Tie::Array::Lazy • Tie::Array::Lazier • Hash + Lazy = ?
    • 49. Hash::Lazy
    • 50. 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
    • 51. Hash::Lazy • export “Hash” as the constructor • Hash::Lazy::Tie
    • 52. Learnt • I hate doing tie • Spiffy constructors can be good read • Hash • Rubyish.pm: Array, Hash, String • Class::Builtin: OO
    • 53. PerlX::MethodCall WithBlock Still more syntax sugar
    • 54. Schwartzian Transform Ruby (0..10) .map { |i| i * 13 } .map { |i| i % 17 } .sort { |a,b| a <=> b }
    • 55. Schwartzian Transform Perl5 sort { $a <=> $b } map { $_ % 17 } map { $_ * 13 } (0..10)
    • 56. Schwartzian Transform autobox Perl5, with [0..10] ->map(sub { $_ * 13 }) ->map(sub { $_ % 17 }) ->sort(sub { $a <=> $b })
    • 57. Schwartzian Transform Perl5, extended! [0..10]->map { $_ * 13 }->map { $_ % 17 }->sort { my ($a,$b) = @_; $a <=> $b };
    • 58. Schwartzian Transform Perl5, extended! [0..10] ->map { $_ * 13 } ->map { $_ % 17 } ->sort { $_[0] <=> $_[1] }
    • 59. PerlX :: MethodCallWithBlo ck
    • 60. ref • B::OPCheck • lineseq, pushmark, const • B::Hooks::EndOfScope • Devel::Declare
    • 61. ref • opcode.pl, opcode.h • B::Concise • B::Deparse
    • 62. PerlX
    • 63. Community
    • 64. Last month
    • 65. Sh*t happened
    • 66. Taiwan
    • 67. Morakot
    • 68.
    • 69. $WebSite.com
    • 70. xdite
    • 71.
    • 72. DisasterTW.com
    • 73. DisasterTW.com • Forum • Initial Coding: 1 hour*woman.
    • 74. 1 day later
    • 75. DisasterTW.com • Web 2.0 / Social-ish • Multi-Function • Mash-able
    • 76. DisasterTW.com ✖ • Web 2.0 / Social-ish • Multi-Function • Mash-able
    • 77. DisasterTW.com • Online ASAP • Robust (peak: 400k PV/day) • Well-known
    • 78. DisasterTW.com • Rapid Prototyping • Cloud Computing • SEO / Social Network Friendly
    • 79. DisasterTW.com • Ruby on Rails • Heroku + Panther CDN • Good HTML / Plurk button
    • 80. DisasterTW.com • 6 heroku instances • Thousands of tweets about #morakot • Total Cost: ~50 USD
    • 81. People are awesome
    • 82. Tools are awesome
    • 83. Problem →
    • 84. What if it was a Jifty site?
    • 85. @obra can haz dancing
    • 86. @obra can haz dancing, naked
    • 87. DisasterTW.com • Rapid Prototyping • Cloud Computing • SEO / Social Network Friendly
    • 88. DisasterTW.com • Jifty • Cloud Computing • Good HTML / Plurk button
    • 89. Heroku for Perl?
    • 90.
    • 91. Ruby is Lovely
    • 92. Ruby is a respectful friend of Perl
    • 93. Ruby programmers are respectful friends of Perl programmers
    • 94. Thanks for Listening