Keys and Values printed on different lines

Hello,

I'm trying to get this code to output like the example below. How can I
update the translate method to have it in tables like that and have it like
this

input input
translate translate

Do you see how it works? When I do the each_with_index and a for index 1..7
do {print "#{symbol} \t"} its like it doesnt see the index. :frowning:

=begin
WakaWaka # translate, print WakaWaka translated and formatted code as
in the following example:

#=>
< > ! * ' ' #
Waka waka bang splat tick tick hash,
^ " ` $ $ -
Caret quote back-tick dollar dollar dash,
! * = @ $ _
Bang splat equal at dollar underscore,
% * < > ~ # 4
Percent splat Waka waka tilde hash four,

=end

class WakaWaka

聽聽def initialize(code)
聽聽聽聽@code = code
聽聽end

聽聽MAPPINGS = {
聽聽聽聽聽聽'<' => 'Waka',
聽聽聽聽聽聽'>' => 'waka',
聽聽聽聽聽聽'!' => 'bang',
聽聽聽聽聽聽"'" => 'tick',
聽聽聽聽聽聽'#' => 'hash',
聽聽聽聽聽聽'^' => 'Caret',
聽聽聽聽聽聽'"' => 'quote',
聽聽聽聽聽聽'`' => 'back-tick',
聽聽聽聽聽聽'$' => 'dollar',
聽聽聽聽聽聽'-' => 'dash',
聽聽聽聽聽聽'!' => 'Bang',
聽聽聽聽聽聽'=' => 'equal',
聽聽聽聽聽聽'@' => 'at',
聽聽聽聽聽聽'_' => 'underscore',
聽聽聽聽聽聽'%' => 'Percent',
聽聽聽聽聽聽'~' => 'tilde',
聽聽聽聽聽聽'4' => 'four',
聽聽聽聽聽聽'*' => 'splat',
聽聽}

聽聽def translate
聽聽聽聽@code.each_with_index do |symbol, index|
聽聽聽聽聽聽print "#{symbol} \t"
聽聽聽聽聽聽print "#{MAPPINGS.fetch(symbol, "unknown symbol")} \t"

聽聽聽聽聽聽# puts "#{symbol} \t"
聽聽聽聽聽聽# puts index
聽聽聽聽聽聽# puts "#{MAPPINGS.fetch(symbol, "unknown symbol")} \t"
聽聽聽聽end
聽聽end

end

waka_code = WakaWaka.new(["<", ">", "!", "*", "'", "'", "#", "^", '"',
"`", "$", "$", "-", "!", "*", "=", "@", "$", "_", "%", "*", "<", ">",
"~", "#", "4"])

waka_code.translate

Here is a video of me explaining the code and what I need to do:
http://quick.as/prdyi4bgy

路路路

On Wed, Aug 10, 2016 at 8:04 PM, Joseph Chambers < joseph@michael-chambers.com> wrote:

Hello,

I'm trying to get this code to output like the example below. How can I
update the translate method to have it in tables like that and have it like
this

input input
translate translate

Do you see how it works? When I do the each_with_index and a for index
1..7 do {print "#{symbol} \t"} its like it doesnt see the index. :frowning:

=begin
WakaWaka # translate, print WakaWaka translated and formatted code as in the following example:

#=>
< > ! * ' ' #
Waka waka bang splat tick tick hash,
^ " ` $ $ -
Caret quote back-tick dollar dollar dash,
! * = @ $ _
Bang splat equal at dollar underscore,
% * < > ~ # 4
Percent splat Waka waka tilde hash four,

=end

class WakaWaka

  def initialize(code)
    @code = code
  end

  MAPPINGS = {
      '<' => 'Waka',
      '>' => 'waka',
      '!' => 'bang',
      "'" => 'tick',
      '#' => 'hash',
      '^' => 'Caret',
      '"' => 'quote',
      '`' => 'back-tick',
      '$' => 'dollar',
      '-' => 'dash',
      '!' => 'Bang',
      '=' => 'equal',
      '@' => 'at',
      '_' => 'underscore',
      '%' => 'Percent',
      '~' => 'tilde',
      '4' => 'four',
      '*' => 'splat',
  }

  def translate
    @code.each_with_index do |symbol, index|
      print "#{symbol} \t"
      print "#{MAPPINGS.fetch(symbol, "unknown symbol")} \t"

      # puts "#{symbol} \t"
      # puts index
      # puts "#{MAPPINGS.fetch(symbol, "unknown symbol")} \t"
    end
  end

end

waka_code = WakaWaka.new(["<", ">", "!", "*", "'", "'", "#", "^", '"', "`", "$", "$", "-", "!", "*", "=", "@", "$", "_", "%", "*", "<", ">", "~", "#", "4"])

waka_code.translate

One way:

Iterate over chunks of the "code" array of a certain size,
e.g. 7 (hint: you could use each_slice(7) for that),
then for each slice first print the line of symbols
and then the line of translations.

Regards,
Marcus

路路路

Am 11.08.2016 um 03:04 schrieb Joseph Chambers:

< > ! * ' ' #
Waka waka bang splat tick tick hash,
^ " ` $ $ -
Caret quote back-tick dollar dollar dash,
! * = @ $ _
Bang splat equal at dollar underscore,
% * < > ~ # 4
Percent splat Waka waka tilde hash four,

--
GitHub: stomar (Marcus Stollsteimer) 路 GitHub
PGP: 0x6B3A101A

Yes,

def translate
  @code.each_slice(7) do |symbol|
    puts symbol.map{ |i| ("#{i}")}.join("\t")
    # puts "#{symbol} \t"
    # puts "#{MAPPINGS.fetch(symbol, "unknown symbol")} \t"
    puts "#{MAPPINGS.fetch(symbol, "unknown symbol")} \t"
  end
end

The issue is now the constraint is fetching to match the symbol, but
now that there are 7 how can I map to each one. argh, this is hard lol

路路路

On Thu, Aug 11, 2016 at 8:13 AM, <sto.mar@web.de> wrote:

Am 11.08.2016 um 03:04 schrieb Joseph Chambers:
> < > ! * ' ' #
> Waka waka bang splat tick tick hash,
> ^ " ` $ $ -
> Caret quote back-tick dollar dollar dash,
> ! * = @ $ _
> Bang splat equal at dollar underscore,
> % * < > ~ # 4
> Percent splat Waka waka tilde hash four,

One way:

Iterate over chunks of the "code" array of a certain size,
e.g. 7 (hint: you could use each_slice(7) for that),
then for each slice first print the line of symbols
and then the line of translations.

Regards,
Marcus

--
GitHub: stomar (Marcus Stollsteimer) 路 GitHub
PGP: 0x6B3A101A

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

def translate
@code.each_slice(7) do |symbol|
    puts symbol.map{ |i| ("#{i}")}.join("\t")

Unnecessarily complicated ("#{i}" for string i does nothing),
you can drop the map completely.

And note that "symbol" actually refers to an array
of 7 symbols, so "symbols" would be a better name.

    # puts "#{symbol} \t"
# puts "#{MAPPINGS.fetch(symbol, "unknown symbol")} \t"
puts "#{MAPPINGS.fetch(symbol, "unknown symbol")} \t"
end
end

The issue is now the constraint is fetching to match the symbol, but
now that there are 7 how can I map to each one. argh, this is hard

You're almost there -- hint: you can map by ... using map :slight_smile:

def translate
  @code.each_slice(7) do |symbols|
    puts symbols.join("\t")
    puts symbols.map {|sym| convert_sym_to_translation_here }.join("\t")
  end
end

You certainly can figure out the small missing part by yourself.

Regards,
Marcus

路路路

Am 11.08.2016 um 17:45 schrieb Joseph Chambers:

--
GitHub: stomar (Marcus Stollsteimer) 路 GitHub
PGP: 0x6B3A101A

YOU'RE AWESOME! wow, why couldnt I think of that.

路路路

On Thu, Aug 11, 2016 at 11:56 AM, <sto.mar@web.de> wrote:

Am 11.08.2016 um 17:45 schrieb Joseph Chambers:
> def translate
> @code.each_slice(7) do |symbol|
> puts symbol.map{ |i| ("#{i}")}.join("\t")

Unnecessarily complicated ("#{i}" for string i does nothing),
you can drop the map completely.

And note that "symbol" actually refers to an array
of 7 symbols, so "symbols" would be a better name.

> # puts "#{symbol} \t"
> # puts "#{MAPPINGS.fetch(symbol, "unknown symbol")} \t"
> puts "#{MAPPINGS.fetch(symbol, "unknown symbol")} \t"
> end
> end
>
> The issue is now the constraint is fetching to match the symbol, but
> now that there are 7 how can I map to each one. argh, this is hard

You're almost there -- hint: you can map by ... using map :slight_smile:

def translate
  @code.each_slice(7) do |symbols|
    puts symbols.join("\t")
    puts symbols.map {|sym| convert_sym_to_translation_here }.join("\t")
  end
end

You certainly can figure out the small missing part by yourself.

Regards,
Marcus

--
GitHub: https://github.com/stomar/
PGP: 0x6B3A101A

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;