Search Results

Search found 27 results on 2 pages for 'fixnum'.

Page 1/2 | 1 2  | Next Page >

  • In Ruby, how does coerce() actually work?

    - by Jian Lin
    It is said that when we have a class Point and knows how to perform point * 3 like the following: class Point def initialize(x,y) @x, @y = x, y end def *(c) Point.new(@x * c, @y * c) end end point = Point.new(1,2) p point p point * 3 Output: #<Point:0x336094 @x=1, @y=2> #<Point:0x335fa4 @x=3, @y=6> but then, 3 * point is not understood: Point can't be coerced into Fixnum (TypeError) So we need to further define an instance method coerce: class Point def coerce(something) [self, something] end end p 3 * point Output: #<Point:0x3c45a88 @x=3, @y=6> So it is said that 3 * point is the same as 3.*(point) that is, the instance method * takes an argument point and invoke on the object 3. Now, since this method * doesn't know how to multiply a point, so point.coerce(3) will be called, and get back an array: [point, 3] and then * is once again applied to it, is that true? point * 3 which is the same as point.*(3) and now, this is understood and we now have a new Point object, as performed by the instance method * of the Point class. The question is: 1) who invokes point.coerce(3) ? Is it Ruby automatically, or is it some code inside of * method of Fixnum by catching an exception? Or is it by case statement that when it doesn't know one of the known types, then call coerce? 2) Does coerce always need to return an array of 2 elements? Can it be no array? Or can it be an array of 3 elements? 3) And is the rule that, the original operator (or method) * will then be invoked on element 0, with the argument of element 1? (element 0 and element 1 are the two elements in that array returned by coerce) Who does it? Is it done by Ruby or is it done by code in Fixnum? If it is done by code in Fixnum, then it is a "convention" that everybody follows when doing a coerce? So could it be the code in * of Fixnum do something like this: if (something.typeof? ...) else if ... # other type else if ... # other type else # if it is not a type I know array = something.coerce(self) return array[0].*(array[1]) end

    Read the article

  • In Ruby, can the coerce() method know what operator it is that requires the help to coerce?

    - by Jian Lin
    In Ruby, it seems that a lot of coerce() help can be done by def coerce(something) [self, something] end that's is, when 3 + rational is needed, Fixnum 3 doesn't know how to handle adding a Rational, so it asks Rational#coerce for help by calling rational.coerce(3), and this coerce instance method will tell the caller: # I know how to handle rational + something, so I will return you the following: [self, something] # so that now you can invoke + on me, and I will deal with Fixnum to get an answer So what if most operators can use this method, but not when it is (a - b) != (b - a) situation? Can coerce() know which operator it is, and just handle those special cases, while just using the simple [self, something] to handle all the other cases where (a op b) == (b op a) ? (op is the operator).

    Read the article

  • migrating simple rails database to mysql

    - by joseph-misiti
    i am interested in creating a rails app with a mysql database. i am new to rails and am just trying to start creating something simple: rails -d mysql MyMoviesSQL cd MyMoviesSQL script/generate scaffold Movies title:string rating:integer rake db:migrate i am seeing the following error: rake aborted! NoMethodError: undefined method `ord' for 0:Fixnum: SET NAMES 'utf8' if i do a trace: ** Invoke db:migrate (first_time) ** Invoke environment (first_time) ** Execute environment ** Execute db:migrate rake aborted! NoMethodError: undefined method ord' for 0:Fixnum: SET NAMES 'utf8' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract_adapter.rb:219:inlog' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:323:in execute' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:599:inconfigure_connection' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:594:in connect' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:203:ininitialize' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:75:in new' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:75:inmysql_connection' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:223:in send' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:223:innew_connection' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:245:in checkout_new_connection' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:188:incheckout' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:184:in loop' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:184:incheckout' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in synchronize' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:183:incheckout' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:98:in connection' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:326:inretrieve_connection' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_specification.rb:123:in retrieve_connection' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_specification.rb:115:inconnection' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/migration.rb:435:in initialize' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/migration.rb:400:innew' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/migration.rb:400:in up' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/migration.rb:383:inmigrate' /Library/Ruby/Gems/1.8/gems/rails-2.3.5/lib/tasks/databases.rake:116 /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in call' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:inexecute' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in each' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:inexecute' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in invoke_with_call_chain' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:insynchronize' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in invoke_with_call_chain' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:ininvoke' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in invoke_task' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:intop_level' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in each' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:intop_level' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in standard_exception_handling' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:intop_level' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in run' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:instandard_exception_handling' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in run' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/bin/rake:31 /usr/bin/rake:19:inload' /usr/bin/rake:19 here are my versions: rails - 2.3.5 ruby - 1.8.6 gem list * LOCAL GEMS * actionmailer (2.3.5, 1.3.6) actionpack (2.3.5, 1.13.6) actionwebservice (1.2.6) activerecord (2.3.5, 1.15.6) activeresource (2.3.5) activesupport (2.3.5, 1.4.4) acts_as_ferret (0.4.1) capistrano (2.0.0) cgi_multipart_eof_fix (2.5.0) daemons (1.0.9) dbi (0.4.3) deprecated (2.0.1) dnssd (0.6.0) fastthread (1.0.1) fcgi (0.8.7) ferret (0.11.4) gem_plugin (0.2.3) highline (1.2.9) hpricot (0.6) libxml-ruby (0.9.5, 0.3.8.4) mongrel (1.1.4) needle (1.3.0) net-sftp (1.1.0) net-ssh (1.1.2) rack (1.0.1) rails (2.3.5) rake (0.8.7, 0.7.3) RedCloth (3.0.4) ruby-openid (1.1.4) ruby-yadis (0.3.4) rubygems-update (1.3.6) rubynode (0.1.3) sqlite3-ruby (1.2.1) termios (0.9.4) also, if i need to add a patch to FixNum, can someone please tell which file to add the patch to. thanks for your help

    Read the article

  • In Ruby, how to implement 20 - point and point - 20 using coerce() ?

    - by Jian Lin
    In Ruby, the operation of point - 20 20 - point are to be implemented. But the following code: class Point attr_accessor :x, :y def initialize(x,y) @x, @y = x, y end def -(q) if (q.is_a? Fixnum) return Point.new(@x - q, @y - q) end Point.new(@x - q.x, @y - q.y) end def -@ Point.new(-@x, -@y) end def *(c) Point.new(@x * c, @y * c) end def coerce(something) [self, something] end end p = Point.new(100,100) q = Point.new(80,80) p (-p) p p - q p q - p p p * 3 p 5 * p p p - 30 p 30 - p Output: #<Point:0x2424e54 @x=-100, @y=-100> #<Point:0x2424dc8 @x=20, @y=20> #<Point:0x2424d3c @x=-20, @y=-20> #<Point:0x2424cc4 @x=300, @y=300> #<Point:0x2424c38 @x=500, @y=500> #<Point:0x2424bc0 @x=70, @y=70> #<Point:0x2424b20 @x=70, @y=70> 30 - p will actually be taken as p - 30 by the coerce function. Can it be made to work? I am actually surprise that the - method won't coerce the argument this way: class Fixnum def -(something) if (/* something is unknown class */) a, b = something.coerce(self) return -(a - b) # because we are doing a - b but we wanted b - a, so it is negated end end end that is, the function returns a negated version of a - b instead of just returning a - b.

    Read the article

  • Ruby on Rail using MYSQL database

    - by Joseph Misiti
    Hey guys, New to rails, trying to figure out something simple. Seems as though I cannot migrate a very simple mysql database using "rake db:migrate" command. Here is the issue: I know rails defaults to sqllite right now, but I need to use mysql for a series of reasons. Use the following commands rails -d mysql MyMoviesSQL cd MyMoviesSQL script/generate scaffold Movies title:string rating:integer rake db:migrate never get past here because i see the following error: in /Users/user/websites/MyMovieSQL) rake aborted! NoMethodError: undefined method `ord' for 0:Fixnum: SET NAMES 'utf8' (See full trace by running task with --trace) using trace XXXXX-macbook-pro:MyMovieSQL user$ rake db:migrate --trace (in /Users/user/websites/MyMovieSQL) ** Invoke db:migrate (first_time) ** Invoke environment (first_time) ** Execute environment ** Execute db:migrate rake aborted! NoMethodError: undefined method ord' for 0:Fixnum: SET NAMES 'utf8' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract_adapter.rb:219:inlog' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:323:in execute' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:599:inconfigure_connection' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:594:in connect' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:203:ininitialize' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:75:in new' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:75:inmysql_connection' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:223:in send' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:223:innew_connection' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:245:in checkout_new_connection' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:188:incheckout' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:184:in loop' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:184:incheckout' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in synchronize' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:183:incheckout' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:98:in connection' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:326:inretrieve_connection' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_specification.rb:123:in retrieve_connection' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_specification.rb:115:inconnection' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/migration.rb:435:in initialize' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/migration.rb:400:innew' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/migration.rb:400:in up' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/migration.rb:383:inmigrate' /Library/Ruby/Gems/1.8/gems/rails-2.3.5/lib/tasks/databases.rake:116 /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in call' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:inexecute' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in each' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:inexecute' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in invoke_with_call_chain' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:insynchronize' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in invoke_with_call_chain' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:ininvoke' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in invoke_task' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:intop_level' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in each' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:intop_level' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in standard_exception_handling' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:intop_level' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in run' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:instandard_exception_handling' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in run' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/bin/rake:31 /usr/bin/rake:19:inload' /usr/bin/rake:19 no clue what is going on, if they want me to add a patch because the methods does not exist, please tell me which file to add it to, and also, how in the future do i figure out which file I need to patch (I see it looks like its a method in FixNum class) here is a patch to a problem that looks similar, but its a different version of ruby http://www.mail-archive.com/[email protected]/msg00250.html versions rails 2.3.5 ruby 1.8.6 gem list yeilds: * LOCAL GEMS * actionmailer (2.3.5, 1.3.6) actionpack (2.3.5, 1.13.6) actionwebservice (1.2.6) activerecord (2.3.5, 1.15.6) activeresource (2.3.5) activesupport (2.3.5, 1.4.4) acts_as_ferret (0.4.1) capistrano (2.0.0) cgi_multipart_eof_fix (2.5.0) daemons (1.0.9) dbi (0.4.3) deprecated (2.0.1) dnssd (0.6.0) fastthread (1.0.1) fcgi (0.8.7) ferret (0.11.4) gem_plugin (0.2.3) highline (1.2.9) hpricot (0.6) libxml-ruby (0.9.5, 0.3.8.4) mongrel (1.1.4) needle (1.3.0) net-sftp (1.1.0) net-ssh (1.1.2) rack (1.0.1) rails (2.3.5) rake (0.8.7, 0.7.3) RedCloth (3.0.4) ruby-openid (1.1.4) ruby-yadis (0.3.4) rubygems-update (1.3.6) rubynode (0.1.3) sqlite3-ruby (1.2.1) termios (0.9.4) thanks in advanced

    Read the article

  • After passing a reference to an method, any mods using that reference are not visible outside the me

    - by Jason
    I am passing the reference of name to *mod_name*, I modify the referenced object from within the method but the change is not visible outside of the method, if I am referring to the same object from all locations how come the value is different depending on where I reference it? name = "Jason" puts name.object_id #19827274 def mod_name(name) puts name.object_id #19827274 name = "JasonB" end puts name.object_id #19827274 puts name #Jason String might be a bad example, but I get the same result even if I use a Fixnum.

    Read the article

  • Problem using a method in Ruby

    - by Rafal
    I'm very new to Ruby (and OOP as well) and I don't know why the following thing doesn't work in Ruby I extended the String class with a new method. Easy enough. Now I want to extend the Fixnum class. A String object appears somewhere in the class, but I can't use the method that I defined earlier. Why? Is this normal?

    Read the article

  • Error on rake command

    - by user332219
    Hi, Have you an idea about this error message when i run the rake db:migrate command under Aptana ? rake aborted! NoMethodError: undefined method `ord' for 0:Fixnum: SET SQL_AUTO_IS_NULL=0 Thanks Here My Config : Windows XP PRO SP2 Wamp 2.0 Aptana 2.0.4 RubyGems 1.3.5 Ruby 1.8.6

    Read the article

  • Polymorphic association inserts 0 instead of raising error when column is misconfigured as Integer

    - by zetetic
    Here's one that stumped me for a while, though in retrospect it should have been obvious. I was getting the error message NoMethodError: undefined method `constantize' for 0:Fixnum when accessing a model through a polymorphic association. Turns out the table on the belongs_to side of the association had an integer type column instead of a string. Easily fixed, but it seems like Rails ought to raise an error in this situation -- instead it happily adds the row with 0 in the type column.

    Read the article

  • Cant use with clauses in find_by_sql

    - by PaulMurrayCbr
    Activerecord seems to be peeking at my sql and getting it wrong. I am finding that this: sql = " select etn.* from edittree_name etn where id = #{id}" Name.find_by_sql(sql) works, but this: sql = " with pp as ( select * from dual) select etn.* from edittree_name etn where id = #{id}" Name.find_by_sql(sql) Gives me a "undefined method `each' for 1:Fixnum". Any clues? Is there a "find_by_raw_sql (and don't try to understand it yourself)" method?

    Read the article

  • Watir with IronRuby!

    - by azamsharp
    Has anyone used Watir with IronRuby successfully? I am getting an error that the required file 'Watir' was not found. What path do I need to set to get this file to work in IronRuby? For some reason my igem command is not working: C:\DevTools\IronRuby\ironruby\Merlin\Main\Languages\Ruby\Scripts\binigem instal l watir '"C:\DevTools\IronRuby\ironruby\Merlin\Main\Languages\Ruby\Scripts\bin\ir.exe"' is not recognized as an internal or external command, operable program or batch file. I am using 0.9 version of Ironruby. I remember that in 0.9 you have to indicate the ir tool: I used the following and got the error again! C:\DevTools\IronRuby\ironruby\Merlin\Main\Languages\Ruby\Scripts\binir igem ins tall watir ERROR: While executing gem ... (RangeError) bignum too big to convert into Fixnum The current version of RubyGems is 1.3.5: C:\DevTools\IronRuby\ironruby\Merlin\Main\Languages\Ruby\Scripts\binir igem -v 1.3.5 I even tried using the full path: require File.dirname(__FILE__) + "C:/ruby/lib/ruby/gems/1.8/gems/commonwatir-1.6.2/lib/watir.rb"

    Read the article

  • Ruby On Rails : db:migrate do not run

    - by user332219
    Hi, When i run this command i have an error : rake db:migrate --trace rake aborted! NoMethodError: undefined method `ord' for 0:Fixnum: SET NAMES 'utf8' see the log file here : http://patxi.mayol.free.fr/public/trace.txt my database.yml file is here : MySQL. Versions 4.1 and 5.0 are recommended. # development: adapter: mysql encoding: utf8 reconnect: false database: annuaire_development pool: 5 username: root password: host: 127.0.0.1 test: adapter: mysql encoding: utf8 reconnect: false database: annuaire_test pool: 5 username: root password: host: 127.0.0.1 production: adapter: mysql encoding: utf8 reconnect: false database: annuaire_production pool: 5 username: root password: host: 127.0.0.1 Thanks My config : WampServer 2.0 with MySQL 5.0.51a Aptana 2.0.4 Ruby 1.8.5 Gems: actionmailer (2.3.4, 2.3.2) actionpack (2.3.4, 2.3.2) activerecord (2.3.4, 2.3.2) activeresource (2.3.4, 2.3.2) activesupport (2.3.4, 2.3.2) cgi_multipart_eof_fix (2.5.0) fastthread (1.0.1) gem_plugin (0.2.3) linecache (0.43) mongrel (1.1.5) mysql (2.8.1, 2.7.3) rack (1.0.0) rails (2.3.4, 2.3.2) rake (0.8.7) ruby-debug-base (0.10.3) ruby-debug-ide (0.4.5) sqlite3-ruby (1.2.1)

    Read the article

  • Rails easy shop

    - by ciss
    I have some question about data organization in my shop. So, after easy mind hacking i decide to create three models: Item, Property and PropertyType Item: id,property_id Property: id, data, property_type_id #(data, serialized object with something like what: {:color => "red", :price => 1000} PropertyType: id, data #(data, also serialized object with {:color => :string, :price => :fixnum}) So, does this good or bad idea? I predict what I can find some problems with validations. But I really need some fields created by user via admin-panel (now I'm talking about Item Properties, which can be changed in any time)

    Read the article

  • Ruby - overriding/enabling multiple assignment (e.g. `a, b, c = d, e, f`)

    - by nicholaides
    In ruby, you can do this: d = [1, 2, 3] a, b, c = d a, b, and c, will receive the values 1, 2, and 3, respectively. d, in this case in an Array and ruby knows to assign it's contents to a, b, and c. But, if d were a Fixnum, for example, only a would be assigned to the value of d while b and c would be assigned nil. What properties of d allow it to be used for multiple assignment? In my exploring so far, I've only been able to make instances of subclasses of Array behave in this way.

    Read the article

  • Why does ruby-debug say 'Saved frames may be incomplete'

    - by Chris McCauley
    From time-to-time I get this when a breakpoint is triggered. It looks like stack frames aren't getting saved so I can't step back through the call stack - a real pain. See below for an example --> #0 BatchProcess.add_failure_record(row_id#Fixnum, test#Struct::Test, message#String,...) at line server/processes/batch.rb:309 Warning: saved frames may be incomplete; compare with caller(0). (rdb:1) pp caller ["./server/processes/batch.rb:309:in `run_tests'", "./server/processes/common/generic_process.rb:219:in `each'", "./server/processes/common/generic_process.rb:219:in `run_tests'", "./server/processes/common/generic_process.rb:271:in `run_plan'", "./server/processes/common/corrections.rb:19:in `each_with_index'", "./server/processes/common/generic_process.rb:266:in `each'", "./server/processes/common/generic_process.rb:266:in `each_with_index'", "./server/processes/common/generic_process.rb:266:in `run_plan'", "./server/processes/batch.rb:202:in `run_engine'", "/usr/lib/ruby/1.8/benchmark.rb:293:in `measure'", "./server/processes/batch.rb:201:in `run_engine'", "./server/processes/common/generic_process.rb:88:in `run_dataset'", "./server/processes/batch.rb:210:in `run_dataset'", "/usr/lib/ruby/1.8/benchmark.rb:293:in `measure'", "./server/processes/batch.rb:209:in `run_dataset'", "./server/processes/common/generic_process.rb:159:in `run'", "./server/processes/common/generic_process.rb:158:in `each'", "./server/processes/common/generic_process.rb:158:in `run'", "./server/processes/batch.rb:350:in `run'", "/usr/lib/ruby/1.8/benchmark.rb:293:in `measure'", "./server/processes/batch.rb:349:in `run'", "server/processes/test_runs/run_tests.rb:55:in `run_one_process'", "server/processes/test_runs/run_tests.rb:81"] Any ideas on how to stop this happening?

    Read the article

  • Rails has_and_belongs_to_many relationship question

    - by Kevin Whitaker
    Hello all, I'm sure that this question has been asked somewhere before, as the habtm relationship seems to be very confusing. I have two models, users and promotions. The idea is that a promotion can have many users, and a user can have many promotions. class User < ActiveRecord::Base has_and_belongs_to_many :promotions end class Promotion < ActiveRecord::Base has_and_belongs_to_many :users end I also have a promotions_users table/model, with no id of its own. It references user_id and promotions_id class PromotionsUsers < ActiveRecord::Base end So, how do I add a user to a promotion? I've tried something like this: user = User.find(params[:id]) promotion = Promotion.find(params[:promo_id]) promo = user.promotions.new(promo) This results in the following error: NoMethodError: undefined method `stringify_keys!' for #<Promotion:0x10514d420> If I try this line instead: promo= user.promotions.new(promo.id) I get this error: TypeError: can't dup Fixnum I'm sure that there is a very easy solution to my problem, and I'm just not searching for the solution the right way. Thank you for your time, and any help you can provide.

    Read the article

  • Rails helper module undefined method `sort'

    - by Magicked
    I'm trying to create a simple helper module in rails, and I'm stumped on the following error message from my new person form (app/views/people/new.html.erb): undefined method `sort' for 97:Fixnum Extracted source (around line #17): 14: <p> 15: <% nations = { 'United States of America' => 'USA', 'Canada' => 'Canada', 'Mexico' => 'Mexico', 'United Kingdom' => 'UK' } %> 16: <%= f.label :country %><br /> 17: <%= radio_buttons(:person, :country, nations) %> 18: 19: </p> 20: <p> radio_buttons is a helper module I have created for my view. Here it is (app/helpers/people_helper.rb): module PeopleHelper def radio_buttons(model_name, target_property, button_source) html='' list = button_source.sort list.each do |x| html << radio_buttons(model_name, target_property, x[1]) html << h(x[0]) html << '<br />' end return html end end The problem appears to be on the "list = button_source.sort", but I'm not sure why it says the method is undefined. I have been able to use it directly within my view code. Am I not able to use methods like this within helper modules? Do I need to include something? Thanks for any help!

    Read the article

  • Rails: creating a custom data type, to use with generator classes and a bunch of questions related t

    - by Shyam
    Hi, After being productive with Rails for some weeks, I learned some tricks and got some experience with the framework. About 10 days ago, I figured out it is possible to build a custom data type for migrations by adding some code in the Table definition. Also, after learning a bit about floating points (and how evil they are) vs integers, the money gem and other possible solutions, I decided I didn't WANT to use the money gem, but instead try to learn more about programming and finding a solution myself. Some suggestions said that I should be using integers, one for the whole numbers and one for the cents. When playing in script/console, I discovered how easy it is to work with calculations and arrays. But, I am talking to much (and the reason I am, is to give some sufficient background). Right now, while playing with the scaffold generator (yes, I use it, because I like they way I can quickly set up a prototype while I am still researching my objectives), I like to use a DRY method. In my opinion, I should build a custom "object", that can hold two variables (Fixnum), one for the whole, one for the cents. In my big dream, I would be able to do the following: script/generate scaffold Cake name:string description:text cost:mycustom Where mycustom should create two integer columns (one for wholes, one for cents). Right now I could do this by doing: script/generate scaffold Cake name:string description:text cost_w:integer cost_c:integer I had also had an idea that would be creating a "cost model", which would hold two columns of integers and create a cost_id column to my scaffold. But wouldn't that be an extra table that would cause some kind of performance penalty? And wouldn't that be defy the purpose of the Cake model in the first place, because the costs are an attribute of individual Cake entries? The reason why I would want to have such a functionality because I am thinking of having multiple "costs" inside my rails application. Thank you for your feedback, comments and answers! I hope my message got through as understandable, my apologies for incorrect grammar or weird sentences as English is not my native language.

    Read the article

  • Why are symbols not frozen strings?

    - by Alex Chaffee
    I understand the theoretical difference between Strings and Symbols. I understand that Symbols are meant to represent a concept or a name or an identifier or a label or a key, and Strings are a bag of characters. I understand that Strings are mutable and transient, where Symbols are immutable and permanent. I even like how Symbols look different from Strings in my text editor. What bothers me is that practically speaking, Symbols are so similar to Strings that the fact that they're not implemented as Strings causes a lot of headaches. They don't even support duck-typing or implicit coercion, unlike the other famous "the same but different" couple, Float and Fixnum. The mere existence of HashWithIndifferentAccess, and its rampant use in Rails and other frameworks, demonstrates that there's a problem here, an itch that needs to be scratched. Can anyone tell me a practical reason why Symbols should not be frozen Strings? Other than "because that's how it's always been done" (historical) or "because symbols are not strings" (begging the question). Consider the following astonishing behavior: :apple == "apple" #=> false, should be true :apple.hash == "apple".hash #=> false, should be true {apples: 10}["apples"] #=> nil, should be 10 {"apples" => 10}[:apples] #=> nil, should be 10 :apple.object_id == "apple".object_id #=> false, but that's actually fine All it would take to make the next generation of Rubyists less confused is this: class Symbol < String def initialize *args super self.freeze end (and a lot of other library-level hacking, but still, not too complicated) See also: http://onestepback.org/index.cgi/Tech/Ruby/SymbolsAreNotImmutableStrings.red http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol Why does my code break when using a hash symbol, instead of a hash string? Why use symbols as hash keys in Ruby? What are symbols and how do we use them? Ruby Symbols vs Strings in Hashes Can't get the hang of symbols in Ruby

    Read the article

  • rails - when using Group_by - How to get an Index?

    - by AnApprentice
    I have the following: sets = DataSet.all.group_by{ |data| [data.project_id, "-", data.thread_id].join(" ") } <% sets.each do |range, datas| %> <p><%= range %>:</p> <% datas.each do |data| %> <%=data%> <p>Last Post<%= data.last.created_at %></p> <% end %> <% end %> Problem is that I need an index. So i updated the above with: <% sets.each_with_index do |range, datas, i| %> <p><%= range %>:</p> <% datas.each do |data| %> <%= i %> <%=data%> <p>Last Post<%= data.last.created_at %></p> <% end %> <% end %> That then breaks, with the error: undefined method `last' for 0:Fixnum Ideas? thank you

    Read the article

  • Heroku: Postgres type operator error after migrating DB from MySQL

    - by sevennineteen
    This is a follow-up to a question I'd asked earlier which phrased this as more of a programming problem than a database problem. http://stackoverflow.com/questions/2935985/postgres-error-with-sinatra-haml-datamapper-on-heroku I believe the problem has been isolated to the storage of the ID column in Heroku's Postgres database after running db:push. In short, my app runs properly on my original MySQL database, but throws Postgres errors on Heroku when executing any query on the ID column, which seems to have been stored in Postgres as TEXT even though it is stored as INT in MySQL. My question is why the ID column is being created as INT in Postgres on the data transfer to Heroku, and whether there's any way for me to prevent this. Here's the output from a heroku console session which demonstrates the issue: Ruby console for myapp.heroku.com >> Post.first.title => "Welcome to First!" >> Post.first.title.class => String >> Post.first.id => 1 >> Post.first.id.class => Fixnum >> Post[1] PostgresError: ERROR: operator does not exist: text = integer LINE 1: ...", "title", "created_at" FROM "posts" WHERE ("id" = 1) ORDER... ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. Query: SELECT "id", "name", "email", "url", "title", "created_at" FROM "posts" WHERE ("id" = 1) ORDER BY "id" LIMIT 1 Thanks!

    Read the article

  • Problem in mutiple :dependent=> :destroy when multiple polymorphic is true

    - by piemesons
    I have four models question, answer, comment and vote.Consider it same as stackoverflow. Question has_many comments Answers has_many comments Questions has_many votes answers has_many votes comments has_many votes Here are the models (only relevant things) class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true has_many :votes, :as => :votable, :dependent => :destroy end class Question < ActiveRecord::Base has_many :comments, :as => :commentable, :dependent => :destroy has_many :answers, :dependent => :destroy has_many :votes, :as => :votable, :dependent => :destroy end class Vote < ActiveRecord::Base belongs_to :votable, :polymorphic => true end class Answer < ActiveRecord::Base belongs_to :question, :counter_cache => true has_many :comments, :as => :commentable , :dependent => :destroy end Now the problem is whenever i am trying to delete any question/answer/comment its giving me an error NoMethodError in QuestionsController#destroy undefined method `each' for 0:Fixnum if i remove this line from any of the model (question/answer/comment) has_many :votes, :as => :votable, :dependent => :destroy then it works perfectly. It seems there is a problem while deleting the records active record is not able to find out the proper path because of multiple joins within the tables.

    Read the article

1 2  | Next Page >