class: center, middle # The `%%` in Ruby .b[[@abelar_s](https://twitter.com/abelar_s) - [@ParisRB](https://twitter.com/parisrb) - [@RailsGirlsParis](https://twitter.com/railsgirlsparis)] [maitre-du-monde.fr](maitre-du-monde.fr) .contrib[[Contribute to this talk on GitHub!](https://github.com/abelards/abelards.github.com/edit/master/talks/C_in_Ruby.html)] ??? --- class: center # Strings ``` 'Hello %s' % 'world' # => "Hello world!" ``` --- class: center # c for Character ``` %c % 'Hello' # => ArgumentError: %c requires a character %c % 'A' # => 'A' ``` --- class: center # p for inspect (print) ```ruby Membre = Struct.new(:nom, :prenom) m = Membre.new('Abélard', 'Sylvain') '%p' % m # => "#
" ``` --- class: center # Alignments ``` '% 8s' % 'Test.' # => " Test." '%-8s' % 'Test.' # => "Test. " ``` --- class: center # Several args ``` "%s. %s %s est %i%% fan de Ruby." % ['M', 'Sylvain', 'Abélard', 100] # => "M. Sylvain Abélard est 100% fan de Ruby." ``` --- class: center # Several args ``` civ, nom, prenom, pourcent = ['M', 'Sylvain', 'Abélard', 100] "%s. %s %s est %i%% fan de Ruby." % civ, nom, prenom, pourcent # => "M. Sylvain Abélard est 100% fan de Ruby." ``` --- class: center # Strings ``` '%{sujet} %{verbe} %{complement}' % {sujet: 'printf', verbe: 'formate', complement: 'des chaînes'} # => "printf formate des chaînes" ``` --- class: center # _(GetText) ``` _('%{sujet} %{verbe} %{complement}') % {sujet: 'printf', verbe: _('formate'), complement: _('des chaînes')} # FR => "printf formate des chaînes" # EN => "printf formats strings" ``` --- class: center # Formats ``` '%d %i %u' % [1, 3.14, 0xF] # => "1 3 15" # entier '%o %#o' % [8, 010] # => "10 010" # octal '%b %#b' % [42, 0b101010] # => "101010 0b101010" # binaire '%x %X %#x' % [42, 0xDEADBEEF, 0xC0FFEE] # => "2a DEADBEEF 0xc0ffee" # hexa ``` --- class: center # Numbers ``` tests = [1, 2048, 3.14159, -42] tests.map {|test| pattern % test } ``` --- class: center # Numbers ``` '%04i' % x # 0001 # 2048 # 0003 # -042 ``` --- class: center # Numbers ``` '%.2f' % x # 1.00 # 2048.00 # 3.14 # -42.00 ``` --- class: center # Reading
@bbatsov prefers sprintf
``` 'format' % [a, b, c] sprintf('format', a, b, c) ```
http://ruby-doc.org/core-2.2.3/Kernel.html#method-i-sprintf
--- class: center, middle, happy # Questions? .b[[@abelar_s](https://twitter.com/abelar_s) - [@ParisRB](https://twitter.com/parisrb) - [@RailsGirlsParis](https://twitter.com/railsgirlsparis)] [maitre-du-monde.fr](maitre-du-monde.fr) .contrib[[Contribute to this talk on GitHub!](https://github.com/abelards/abelards.github.com/edit/master/talks/C_in_Ruby.html)] --- class: center, middle, happy # Thanks! .b[[@abelar_s](https://twitter.com/abelar_s) - [@ParisRB](https://twitter.com/parisrb) - [@RailsGirlsParis](https://twitter.com/railsgirlsparis)] [maitre-du-monde.fr](maitre-du-monde.fr) .contrib[[Contribute to this talk on GitHub!](https://github.com/abelards/abelards.github.com/edit/master/talks/C_in_Ruby.html)]