LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-28-2010, 11:07 AM   #1
CoderMan
Member
 
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375
Blog Entries: 24

Rep: Reputation: 43
Ruby Code Blocks


I was reading about Ruby code blocks, but it's all a bit hazy.
My questions:

1. Can you store executable code in the value part of a hash (err...
associative array)
2. If you did, how would you call the code?
3. If you executed this stored code, would it be possible when doing so
to pass in an object to the code that it could use?

I would appreciate any simple examples.
 
Old 05-28-2010, 10:39 PM   #2
CoderMan
Member
 
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375

Original Poster
Blog Entries: 24

Rep: Reputation: 43
Quote:
Originally Posted by CoderMan View Post
I was reading about Ruby code blocks, but it's all a bit hazy.
My questions:

1. Can you store executable code in the value part of a hash (err...
associative array)
2. If you did, how would you call the code?
3. If you executed this stored code, would it be possible when doing so
to pass in an object to the code that it could use?

I would appreciate any simple examples.
As usual, figured it out on my own first.

This seems to work:

Code:
$ cat proc.rb
class Interf
  def increment(n)
    n+1
  end
end

interf = Interf.new

proc = Proc.new {|obj| puts obj.increment(12).to_s}
proc2 = Proc.new {|obj| puts obj.increment(14).to_s}

hash = {'Bob' => proc,
  'Frank' => proc2
}

hash['Bob'].call(interf)
hash['Frank'].call(interf)
$ ruby proc.rb
13
15
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
install code::blocks blackcore Linux - Software 2 05-27-2010 07:32 AM
code::blocks elishac Linux - Software 40 01-25-2010 11:51 PM
Code::Blocks h3x0r Programming 2 04-13-2007 08:35 AM
code::blocks compiling sharapchi Programming 2 11-23-2006 12:50 PM
Smilies in Code Blocks Matir LQ Suggestions & Feedback 6 08-19-2005 04:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration