Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8699729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:01:17+00:00 2026-06-13T02:01:17+00:00

In my script (for Ruby >= 1.9) below I defined a Table class, which

  • 0

In my script (for Ruby >= 1.9) below I defined a Table class, which responsability is to generate 2 to 10 addition or multiplication table (chosen with a parameter). Then I call table method from a new Table instance in order to print result in a file.

This is the script :

#!/usr/bin/env ruby

class Table

HEADER_LINE = "="*25
add_operation = lambda { |op1, op2| op1 + op2 }
mul_operation = lambda { |op1, op2| op1 * op2 }

def table(req_operation = :mul)

    operation, op_label =   case req_operation
                            when :add
                                [add_operation, "+"]
                            when :mul
                                [mul_operation, "*"]
                            else
                                raise "Unknown operation #{req_operation} !"
                            end


    (2..10).each do |op1|
        yield HEADER_LINE
        yield "Table de #{op1} (x#{op_label}y)"
        yield HEADER_LINE
        (1..10).each do |op2|
            yield line = "#{op1} #{op_label} #{op2} = #{operation.call(op1, op2)}"
        end
        yield HEADER_LINE
        yield
    end
end

end

File.open("MyFile", "w") do |file|
    Table.new.table do |line|
        file.write "#{line}\n"
    end
end

The parallel assignement on line 11 tries to set a lambda to operation and a string to op_label. Indeed, on line 26, I want to apply the lambda to op1 and op2 local variables.

But I get the following error :

./operation_table.rb:15:in `table': undefined local variable or method `mul_operation' for #<Table:0x00000000f1fc48> (NameError)
    from ./operation_table.rb:38:in `block in <main>'
    from ./operation_table.rb:37:in `open'
    from ./operation_table.rb:37:in `<main>'

Is there a way to correct it while keeping the parallel assignement ?
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:01:18+00:00Added an answer on June 13, 2026 at 2:01 am

    add_operation and mul_operation are local variables, declared outside the function, and cannot be accessed by the function table.

    You can declare it inside the table function, or store as class variables.

    def table(...)
      add_operation = lambda { |op1, op2| op1 + op2 }
      mul_operation = lambda { |op1, op2| op1 * op2 }
      ...
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ruby script below which infinitely prints numbers from 1 onward. How
I have the following Ruby script, in which class Foo includes module Baz, module
the Ruby code is below: <%= product.advert_text -%> What script can limit the amount
In a ruby script I am running a loop in which I am dynamically
I wrote a small ruby script that generates an array like the one below:
I am trying to install ruby 1.9.2 from a ruby script using rvm which
I wrote up the ruby script below to help my students understand public key
Original Question (long version below). Short version: Running hadoop streaming with a ruby script
The Ruby script i am writing is going to be run every morning and
I have Ruby script that creates a proxy so that I can make HTTP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.