Forum: Ruby Few Questions on Ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B9b67897840ad4292c6499583520f2c5?d=identicon&s=25 Anil Bhat (anilbhat21)
on 2008-12-29 07:37
Given this module definition:

    module Greeting
      def say_hello
        puts "Hello, my name is #{self.name}"
      end
    end

1.) How to implement a class Foo using the Greeting module so that the
following code example generates the expected output. Do not implement a
say_hello method.
    foo = Foo.new("Fred")
    foo.say_hello
    >> Hello, my name is Fred

2.) How to Implement a class Bar using the Greeting module so that the
following code example generates the expected output. Do not implement a
say_hello method.

    Bar.say_hello
    >> Hello, my name is Bar


3.) Given this class:

    class Example
      @name
    end

    How would you describe the @name variable?  Assume you have access
to this source code but can't change it.  How would you add methods to
the Example class to set and get the value of @name?
4.) In a Rails application, if it takes a long time for a page to load.
If there is an action that dynamically generates a large binary file and
sends this to clients via send_data.  How is this related to the slow
response time?  What change can we make to avoid this?
7864d809993744583d15b0f0a2f8dded?d=identicon&s=25 Marius Žilėnas (kronidas)
on 2008-12-29 09:39
Hello:),

1)
module Greeting
  def say_hello
    puts "Hello, my name is #{self.name}"
  end
end

class Foo
  attr_accessor :name
  include Greeting

  def initialize(name)
    @name = name
  end

end

2)
class Bar
  extend Greeting
end

3)

class Example

  attr_accessor :name

end

4) Client waits until request finishes even if client doesn't download
any data. Implement a background service to generate large files,
implement user notification service to notify user after file is
generated and ready to use.


Marius Žilėnas
mzilenas@gmail.com

Anil Bhat wrote:
> Given this module definition:
>
>     module Greeting
>       def say_hello
>         puts "Hello, my name is #{self.name}"
>       end
>     end
>
> 1.) How to implement a class Foo using the Greeting module so that the
> following code example generates the expected output. Do not implement a
> say_hello method.
>     foo = Foo.new("Fred")
>     foo.say_hello
>     >> Hello, my name is Fred
>
> 2.) How to Implement a class Bar using the Greeting module so that the
> following code example generates the expected output. Do not implement a
> say_hello method.
>
>     Bar.say_hello
>     >> Hello, my name is Bar
>
>
> 3.) Given this class:
>
>     class Example
>       @name
>     end
>
>     How would you describe the @name variable?  Assume you have access
> to this source code but can't change it.  How would you add methods to
> the Example class to set and get the value of @name?
> 4.) In a Rails application, if it takes a long time for a page to load.
> If there is an action that dynamically generates a large binary file and
> sends this to clients via send_data.  How is this related to the slow
> response time?  What change can we make to avoid this?
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-12-29 12:40
(Received via mailing list)
On 29.12.2008 07:37, Anil Bhat wrote:
> Given this module definition:

...

This looks suspiciously like homework or interview questions.  What is
your motivation for posting this seemingly unrelated list of questions /
tasks?

  robert
B09f4659460545e38ece34ddd0d96b46?d=identicon&s=25 Yaser Sulaiman (Guest)
on 2008-12-29 13:03
(Received via mailing list)
On Mon, Dec 29, 2008 at 2:39 PM, Robert Klemme
<shortcutter@googlemail.com>
wrote:
>
> This looks suspiciously like homework or interview questions.  What is
your motivation for posting this seemingly unrelated list of questions /
tasks?
>

I can has homework solutionz! :)
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2008-12-29 17:36
(Received via mailing list)
On Mon, Dec 29, 2008 at 2:39 AM, Marius Þilënas <mzilenas@gmail.com> wrote:
>  attr_accessor :name
>  extend Greeting
> end

I like the fact you can extend single instances, too...

f = Foo.new
f.extend MyModule

Todd
This topic is locked and can not be replied to.