Learning From Ruby (Yapc Asia)

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

1 comments

Comments 1 - 1 of 1 previous next Post a comment

  • + chorny chorny 2 months ago
    Memoize.pm was in Perl’s core for many years. So it may be that Ruby copied it. And there are several attribute-based memoizers on CPAN.
Post a comment
Embed Video
Edit your comment Cancel

Notes on slide 1

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

3 Favorites, 1 Group & 1 Event

Learning From Ruby (Yapc Asia) - Presentation 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. $WebSite.com
  69. xdite
  70. DisasterTW.com
  71. DisasterTW.com • Forum • Initial Coding: 1 hour*woman.
  72. 1 day later
  73. DisasterTW.com • Web 2.0 / Social-ish • Multi-Function • Mash-able
  74. DisasterTW.com ✖ • Web 2.0 / Social-ish • Multi-Function • Mash-able
  75. DisasterTW.com • Online ASAP • Robust (peak: 400k PV/day) • Well-known
  76. DisasterTW.com • Rapid Prototyping • Cloud Computing • SEO / Social Network Friendly
  77. DisasterTW.com • Ruby on Rails • Heroku + Panther CDN • Good HTML / Plurk button
  78. DisasterTW.com • 6 heroku instances • Thousands of tweets about #morakot • Total Cost: ~50 USD
  79. People are awesome
  80. Tools are awesome
  81. Problem →
  82. What if it was a Jifty site?
  83. @obra can haz dancing
  84. @obra can haz dancing, naked
  85. DisasterTW.com • Rapid Prototyping • Cloud Computing • SEO / Social Network Friendly
  86. DisasterTW.com • Jifty • Cloud Computing • Good HTML / Plurk button
  87. Heroku for Perl?
  88. Ruby is Lovely
  89. Ruby is a respectful friend of Perl
  90. Ruby programmers are respectful friends of Perl programmers
  91. Thanks for Listening

+ Kang-min LiuKang-min Liu, 2 months ago

custom

1134 views, 3 favs, 0 embeds more stats

Introducing several of my Perl5 hacks of bring some more

More info about this document

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Go to text version

  • Total Views 1134
    • 1134 on SlideShare
    • 0 from embeds
  • Comments 1
  • Favorites 3
  • Downloads 24
Most viewed embeds

more

All embeds

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories

Groups / Events