Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Printing why's (poignant) guide to ruby

Reply
Thread Tools

Printing why's (poignant) guide to ruby

 
 
Alexander Kellett
Guest
Posts: n/a
 
      02-14-2005
On Feb 14, 2005, at 7:10 PM, James G. Britt wrote:
> On Tue, 15 Feb 2005 02:39:08 +0900, Marcus Sharp
> <(E-Mail Removed)> wrote:
>> *putting on flame retardant pants*
>>
>> As a noob to Ruby who was convinced to try it after reading why's
>> wonderful work, i will say that i find Ruby "harder" than Python. To
>> call Python a "joke of a language" is pretty harsh by the way.

>
> It is not merely harsh; it is incorrect. Most people here prefer Ruby
> to Python, but don't simply dismiss Python either.


/me notes that he's writing a ruby parser using python's pyggy library
its a nice and easy language, just that ruby's blocks ain't complex, new
programs follow easy rules ("stick {} around the block"), and list
comprehensions required an explanation for me. and i'm not exactly a
newbie programmer. ruby's blocks just clicked, no tutorial needed, i
grabbed the stdlib read the syntax files typed in a few examples from
programming ruby and haven't stopped reading ri since.

Alex



 
Reply With Quote
 
 
 
 
gabriele renzi
Guest
Posts: n/a
 
      02-15-2005
Florian Gross ha scritto:


> On another note it might be interesting to have a look at concatenative
> languages like Joy. These have something very similar to blocks as well:
>
> [1 2 3] [2 *] map
>
> What does that code to?
>
> C:\dev.svn\ruby>irb -r joy
> irb(main):001:0> joy = Joy.new
> => #<Joy:0x2b90d00 ...>
> irb(main):002:0> joy.execute "[1 2 3] [2 *] map"; joy.stack
> => [[2, 4, 6]]


wait: you wrote a joy interpreter? that's cool

> It takes a list and applies the [2 *] predicate to it yielding a new
> list. Note that Joy is stack based and utilizes reverse polish notation
> so 2 * pushes a 2 to the stack and multiplies the item that was on top
> before with it.


I think even more mind boggling things relate to every function being
unary and to the implicit recursive operators
 
Reply With Quote
 
 
 
 
gabriele renzi
Guest
Posts: n/a
 
      02-15-2005
Navindra Umanee ha scritto:

> I honestly think things like blocks are not a trivial matter.
>
> Why:
>
> 5.times { print "hello " }
>
> instead of:
>
> 5 times print hello


just my two cents:
maybe the point is starting withouth using blocks.
I found them hard too, when I initially started learning ruby , but I
could write cute programs (the usual web scraping, irc bots and such)
without using them. As of current ruby you can write net applications,
XMLRPC clients, access SOAP funny things withouth ever seing a block.
Then, you have an "ah-ha!" moment and you become happyer

Is not like explaining why in python:
fun=lambda a: print(a)
does not work, or what
foo.bar=fun.__get__(foo)
is supposed to do is /that/ obvious

there is always a learning curve, sadly.


 
Reply With Quote
 
gabriele renzi
Guest
Posts: n/a
 
      02-15-2005
Brian McCallister ha scritto:
> If you have trouble explaining blocks, though, wait until you go for
> list comprehensions!

^^^^^^^^^^^^^^^^^^^
more fun with generator expressions
 
Reply With Quote
 
Florian Gross
Guest
Posts: n/a
 
      02-15-2005
gabriele renzi wrote:

>> On another note it might be interesting to have a look at
>> concatenative languages like Joy. These have something very similar to
>> blocks as well:
>>
>> [1 2 3] [2 *] map
>>
>> What does that code to?
>>
>> C:\dev.svn\ruby>irb -r joy
>> irb(main):001:0> joy = Joy.new
>> => #<Joy:0x2b90d00 ...>
>> irb(main):002:0> joy.execute "[1 2 3] [2 *] map"; joy.stack
>> => [[2, 4, 6]]

> wait: you wrote a joy interpreter? that's cool


Partially. See pseudo attachment.

>> It takes a list and applies the [2 *] predicate to it yielding a new
>> list. Note that Joy is stack based and utilizes reverse polish
>> notation so 2 * pushes a 2 to the stack and multiplies the item that
>> was on top before with it.

>
>
> I think even more mind boggling things relate to every function being
> unary and to the implicit recursive operators


Agreed, it's a very interesting language.

> # See http://www.latrobe.edu.au/philosophy/phimvt/joy.html
> # and http://factor.sourceforge.net/wiki/
>
> require 'set'
>
> class Joy
> attr_reader :stack, ptions
>
> Instructions = {
> :false => lambda { |env| env.stack << false },
> :true => lambda { |env| env.stack << true },
> :maxint => lambda { |env| env.stack << env.options[:maxint] },
> :setsize => lambda { |env| env.stack << env.options[:setsize] },
> :stack => lambda { |env| env.stack << env.stack },
> :conts => lambda { |env| callcc { |cc| env.stack << cc } }, # ?
> :time => lambda { |env| env.stack << Time.now.to_i },
> :rand => lambda { |env| env.stack << rand(env.options[:maxint]) },
> :stdin => lambda { |env| env.stack << env.options[:stdin] },
> :stdout => lambda { |env| env.stack << env.options[:stdout] },
> :stderr => lambda { |env| env.stack << env.options[:stderr] },
>
> :id => lambda { |env| },
> :dup => lambda { |env| env.stack << env.stack.last },
> :swap => lambda { |env| env.stack.insert(-2, env.stack.pop) },
> :rollup => lambda { |env| env.stack.insert(-3, env.stack.pop) },
> :rolldown => lambda { |env| env.stack << env.stack.slice!(-3) },
> :rotate => lambda do |env|
> env.stack[-1], env.stack[-3] = env.stack[-3], env.stack[-1]
> end,
> opd => lambda { |env| ?? },
> :dupd => lambda { |env| ?? },
> :swapd => lambda { |env| ?? },
> :rollupd => lambda { |env| ?? },
> :rolldownd => lambda { |env| ?? },
> :rotated => lambda { |env| ?? },
> op => lambda { |env| env.stack.pop },
> :choice => lambda do |env|
> condi, theni, elsei = env.stack.slice!(-3 .. -1)
> env.stack << (env.true?(condi) ? theni : elsei)
> end,
> r => lambda do |env|
> env.stack << env.stack.pop | env.stack.pop
> end,
> or => lambda do |env|
> env.stack << env.stack.pop ^ env.stack.pop
> end,
> :and => lambda do |env|
> env.stack << env.stack.pop & env.stack.pop
> end,
> :not => lambda do |env|
> # TODO: should be set complement for sets
> env.stack << !env.stack.pop
> end,
> :+ => lambda { |env| env.stack << env.stack.pop + env.stack.pop },
> :- => lambda { |env| env.stack << env.stack.pop - env.stack.pop },
> :* => lambda { |env| env.stack << env.stack.pop * env.stack.pop },
> :/ => lambda { |env| env.stack << env.stack.pop / env.stack.pop },
> :rem => lambda { |env| env.stack << env.stack.pop % env.stack.pop },
> :div => lambda { |env| env.stack += env.stack.pop.divmod(env.stack.pop) },
> :sign => lambda { |env| env.stack << 0 <=> env.stack.pop },
> :neg => lambda { |env| env.stack << -env.stack.pop },
> :concat => lambda do |env|
> x, y = env.stack.slice!(-2 .. -1)
> env.stack << x + y
> end,
> :i => lambda do |env|
> list = env.stack.pop
> env.interpret_list list
> end,
> :map => lambda do |env|
> list, handler = env.stack.slice!(-2 .. -1)
> old_stack = env.stack.slice!(0 .. -1)
> list.each do |item|
> env.stack << item
> env.interpret_list handler
> end
> env.stack.replace(old_stack + [env.stack.dup])
> end
> }
>
> def true?(obj) obj end
>
> def initialize(options = {}, stack = [], instructions = Instructions)
> @options = {
> :maxint => 2 ** 32,
> :setsize => 2 ** 32,
> :stdin => STDIN,
> :stdout => STDOUT,
> :stderr => STDERR
> }.merge(options)
> @stack, @instructions = stack, instructions
> end
>
> def string_unescape(string)
> string.gsub(/\\(?:\d{1,3}|.)/) do |match|
> escape = match[1 .. -1]
> case escape
> when "n" then "\n"
> when "t" then "\t"
> when "b" then "\b"
> when "r" then "\r"
> when "f" then "\f"
> when /^\d+$/ then escape.to_i(.chr
> end
> end
> end
>
> def parse(string)
> string = string.dup
> count = 0
> accum = nil
> states = [:code]
>
> lex_re = /^[$#].+$ # Comment or shell command
> |\(\*(?:\n|.)+?\*\) # Multi-line comment
> |\s+ # White space
> |-?\d[\d.\-\w]*\b # Numeric constant (lax)
> |\b[A-Za-z]+[A-Za-z0-9=_\-]\b # Atomic symbol (word)
> |==|.
> /x
>
> string.scan(lex_re) do |token|
> p [states, accum, token] if $DEBUG
>
> case states.last
> when :code then
> case token
> when /^[$#]|^\(\*|^\s+$/ then
> # Comments, shell commands and whitespace are all ignored for now.
> when /^-?\d/ then
> yield((Integer(token) rescue Float(token)))
> when "[" then
> states << :list
> accum = ""
> count = 1
> when "{" then
> states << :set
> accum = ""
> count = 1
> when '"' then
> states << :string
> accum = ""
> when "'" then
> states << :char
> else
> yield token.intern
> end
>
> when :list, :set then
> open = {:list => "[", :set => "{"}[states.last]
> close = {:list => "]", :set => "}"}[states.last]
> case token
> when open then
> accum << token
> count += 1
> when close then
> count -= 1
> if count != 0 then
> accum << token
> else
> list = {:list => [], :set => Set.new}[states.last]
> parse(accum) { |token| list << token }
> yield list
> states.pop
> end
> else
> accum << token
> end
>
> when :string then
> case token
> when "\\" then
> count = 1
> when '"' then
> if count == 0 then
> yield string_unescape(accum)
> states.pop
> else
> accum << token
> count = 0
> end
> else
> accum << token
> count = 0
> end
> end
> end
> end
>
> def interpret_token(token)
> case token
> when Numeric, Array, Set
> @stack << token
> when Symbol
> @instructions[token].call(self)
> end
> end
>
> def interpret_list(list)
> list.each { |token| interpret_token token }
> end
>
> def execute(string)
> parse(string) { |token| interpret_token token }
> end
> end

 
Reply With Quote
 
Ruth A. Kramer
Guest
Posts: n/a
 
      02-15-2005
William James wrote:
> Incorrect. Subtracting a from both sides leaves
>
> 0 = 1


Oops! (You're right, of course!)

Randy Kramer


 
Reply With Quote
 
Mark Hubbart
Guest
Posts: n/a
 
      02-15-2005
Hi,

On Tue, 15 Feb 2005 01:04:22 +0900, Gavri Fernandez
<(E-Mail Removed)> wrote:
> For someone who is new to programming
> "string".display
> wouldn't feel less natural than
> print "string"
>
> Really, think about that.
>
> And why should we try to teach them the procedural paradigm and then
> say 'Hey! forget all that stuff you learnt. It's not really a good way
> to program. Now i'll teach you the object-oriented way" ?


No, you don't go paradigm-swapping on them. When it gets to a point
where it makes a difference, let them know that "puts" is the short
form of "STDOUT.puts". Tell them that STDOUT is an object that is
created for you, which can recieve messages that, among other things,
help you write to the screen. Have them write a little code using
STDOUT. Then tell them that the short form is easier, and more
commonly used.

> Of course, all of this I say assuming that the goal is to create an
> object-oriented programmer, not a procedural one.


I suspect the goal is to create a Ruby programmer Which means that
there will be some amount of overlapping.

cheers,
Mark


 
Reply With Quote
 
Gavri Fernandez
Guest
Posts: n/a
 
      02-16-2005
On Wed, 16 Feb 2005 02:13:50 +0900, Mark Hubbart <(E-Mail Removed)> wrote:

> No, you don't go paradigm-swapping on them. When it gets to a point
> where it makes a difference, let them know that "puts" is the short
> form of "STDOUT.puts". Tell them that STDOUT is an object that is
> created for you, which can recieve messages that, among other things,
> help you write to the screen. Have them write a little code using
> STDOUT. Then tell them that the short form is easier, and more
> commonly used.


But why is this better than just starting with
STDOUT.puts
and later after object-oriented concepts have been completely
understood introduce
puts

why do you want to go this way?
puts -> STDOUT.puts -> puts

I really think doing it with hard-core object-orientation in the
beginning will help a lot. It's so natural.

> > Of course, all of this I say assuming that the goal is to create an
> > object-oriented programmer, not a procedural one.

>
> I suspect the goal is to create a Ruby programmer Which means that
> there will be some amount of overlapping.


Wasn't the goal just to create a programmer out of a non-programmer?
That was what this thread was about, right? Read all that i said above
with this assumption and you might agree with me

--
Gavri
---------------------------------------------------
I blog here: http://gavri.blogspot.com


 
Reply With Quote
 
Alexander Kellett
Guest
Posts: n/a
 
      02-16-2005
On Feb 16, 2005, at 4:56 PM, Gavri Fernandez wrote:
> On Wed, 16 Feb 2005 02:13:50 +0900, Mark Hubbart
> <(E-Mail Removed)> wrote:
>
>> No, you don't go paradigm-swapping on them. When it gets to a point
>> where it makes a difference, let them know that "puts" is the short
>> form of "STDOUT.puts". Tell them that STDOUT is an object that is
>> created for you, which can recieve messages that, among other things,
>> help you write to the screen. Have them write a little code using
>> STDOUT. Then tell them that the short form is easier, and more
>> commonly used.

>
> But why is this better than just starting with
> STDOUT.puts
> and later after object-oriented concepts have been completely
> understood introduce
> puts
>
> why do you want to go this way?
> puts -> STDOUT.puts -> puts
>
> I really think doing it with hard-core object-orientation in the
> beginning will help a lot. It's so natural.


personally i'd teach via p
this is not only the inspection
output of a return value for irb,
but its also a heck of a lot less
confusing in some corner cases.

i doubt that any user will be
impressed by having to type
STDOUT.puts whenever they want
to see something

Alex



 
Reply With Quote
 
Gavri Fernandez
Guest
Posts: n/a
 
      02-16-2005
On Thu, 17 Feb 2005 01:02:25 +0900, Alexander Kellett
<(E-Mail Removed)> wrote:
> personally i'd teach via p
> this is not only the inspection
> output of a return value for irb,
> but its also a heck of a lot less
> confusing in some corner cases.


With irb for beginners, it's a lot more "natural". There's no need for
the P's...print, puts or otherwise
--
Gavri
---------------------------------------------------
I blog here: http://gavri.blogspot.com


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 2 11-27-2010 12:12 PM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:52 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:50 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:28 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM



Advertisments