Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0713ebdfa290
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 3db6f3e2123c
Choose a head ref
  • 2 commits
  • 2 files changed
  • 2 contributors

Commits on Apr 5, 2015

  1. Implemented Kernel#format hash key sections

    >> "Hello, %{name}, top of the %{time_of_day} to ya!" % {name: '@meh', time_of_day: 'morning'}
    => "Hello, @meh, top of the morning to ya!"
    vais committed Apr 5, 2015
    Copy the full SHA
    88245d9 View commit details
  2. Merge pull request #791 from vais/format

    Implemented Kernel#format hash key sections
    meh committed Apr 5, 2015
    Copy the full SHA
    3db6f3e View commit details
Showing with 21 additions and 4 deletions.
  1. +21 −1 opal/corelib/kernel.rb
  2. +0 −3 spec/filters/bugs/string.rb
22 changes: 21 additions & 1 deletion opal/corelib/kernel.rb
Original file line number Diff line number Diff line change
@@ -192,10 +192,30 @@ def format(format, *args)
spec = '([cspdiubBoxXfgeEG])',
valid = '%' + idx_str + flags + width_str + prec_str + spec,
hash_key = '%\\{(\\w+)\\}',
escaped = '(%%)',
invalid = '(%[^\\n\\0])';
return format.replace(new RegExp(valid + '|' + escaped + '|' + invalid, 'g'), function(str, idx_str, flags, width_str, w_idx_str, prec_str, p_idx_str, spec, escaped, invalid) {
return format.replace(new RegExp(valid + '|' + hash_key + '|' + escaped + '|' + invalid, 'g'), function(
str,
idx_str,
flags,
width_str,
w_idx_str,
prec_str,
p_idx_str,
spec,
hash_key,
escaped,
invalid
) {
if (hash_key) {
if (args[0] !== undefined && args[0].$$is_hash) {
return #{`args[0]`.fetch(`hash_key`)};
}
#{raise ArgumentError, 'one hash required'}
}
if (escaped) {
return '%';
}
3 changes: 0 additions & 3 deletions spec/filters/bugs/string.rb
Original file line number Diff line number Diff line change
@@ -160,9 +160,6 @@
fails "String#% behaves as if calling Kernel#Float for %g arguments, when the passed argument is hexadecimal string"
fails "String#% behaves as if calling Kernel#Float for %G arguments, when the passed argument does not respond to #to_ary"
fails "String#% behaves as if calling Kernel#Float for %G arguments, when the passed argument is hexadecimal string"
fails "String#% when format string contains %{} sections replaces %{} sections with values from passed-in hash"
fails "String#% when format string contains %{} sections raises KeyError if key is missing from passed-in hash"
fails "String#% when format string contains %{} sections should raise ArgumentError if no hash given"
fails "String#% when format string contains %<> formats uses the named argument for the format's value"
fails "String#% when format string contains %<> formats raises KeyError if key is missing from passed-in hash"
fails "String#% when format string contains %<> formats should raise ArgumentError if no hash given"